2

I have images in the numpy format, I have downloaded the data from the internet(https://github.com/ichatnun/spatiospectral-densenet-rice-classification/blob/master/x.npy). Example of data (1, 34, 23, 100), Here 1 is the image number, 34x23 is pixel value, 100 is the channel.

I wanted to load the data for the training of a machine learning model, I looked at the other sources, their data is in the format only 34x23

#my code till now
dataset1 = np.load('x.npy', encoding='bytes')
print("shape of dataset1")
print(dataset1.shape, dataset1.dtype)
#data shape
shape of dataset1
(3, 50, 170, 110) float64

#my code
data1 = dataset1[:, :, :, -1]
data1.shape

If I use SVM like,

from sklearn.svm import SVC
clf = SVC(gamma='auto')
clf.fit(datasset1, y) 

I got the error

ValueError: Found array with dim 4. Estimator expected <= 2

I wanted to load the data as a dataframe or another format for train and split, but I am not able to remove the first value.

Sample data

print(dataset1)

[[[[0.17807601 0.15946769 0.20311266 ... 0.48133529 0.48742528
0.47095974]
[0.18518101 0.18394045 0.19093267 ... 0.45889252 0.44987031
0.46464419]
[0.19600767 0.18845156 0.18506823 ... 0.47558362 0.47738807
0.45821586]
... 

My expected output is how to pass the data to the svm for classification

1
  • Have a look at the readme.md. It meens the file is composed by 3-dimensional 50x170x110 tensor e.g. a data with shape : 3x50x170x110. Commented Aug 3, 2019 at 10:06

2 Answers 2

1

the issue is that the SVM accept only 2d array, your data is in the format(numberof sample, rows, column, channel)

Try this, it works for me

dataset1 = np.load('x.npy', encoding='bytes')
dataset2 = np.load('labels.npy', encoding='bytes')

nsamples, nx, ny, nz = dataset1.shape
X = dataset1.reshape((nsamples,nx*ny*nz))

y = numpy.argmax(dataset2, axis=1)

from sklearn import svm
clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X, y) 

#repalce X with your test data
print(clf.predict(X))
Sign up to request clarification or add additional context in comments.

Comments

0

pay attention to your data source, your x.npy doesn't have images

x.npy contains example datacubes of the processed rice dataset that can be used for training/testing. Each datacube is a three-dimensional 50x170x110 tensor: two spatial dimensions and one spectral dimension.

5 Comments

Why am I getting 3 here? the shape of dataset1 as (3, 50, 170, 110), How interpreter will take 3 different data
if you look here : github.com/ichatnun/spatiospectral-densenet-rice-classification/… you will find : num_training = x_training.shape[0] N_spatial = x_training.shape[1:3] N_bands = x_training.shape[3]
Thank you, so here in N_spatial, we are getting the rows and column of the image, am I right?
you don't have images... you have tensors, "PROCESSED rice dataset"
okay, but I am getting error, while passing it to svm, I have update my question, please suggest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.