SUSHMA KUMARI
SUSHMA KUMARI

Reputation: 95

Unable to load data from numpy array for SVM Classification

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

Upvotes: 2

Views: 1285

Answers (2)

Gautam Shahi
Gautam Shahi

Reputation: 455

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))

Upvotes: 1

kederrac
kederrac

Reputation: 17322

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.

Upvotes: 0

Related Questions