无法从 numpy 数组加载数据以进行 SVM 分类
Unable to load data from numpy array for SVM Classification
我有numpy格式的图片,我已经从网上下载了数据(https://github.com/ichatnun/spatiospectral-densenet-rice-classification/blob/master/x.npy)。数据示例(1, 34, 23, 100),这里1是图像编号,34x23是像素值,100是通道。
我想加载机器学习模型训练的数据,我查看了其他来源,他们的数据格式仅为 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
如果我像这样使用 SVM,
from sklearn.svm import SVC
clf = SVC(gamma='auto')
clf.fit(datasset1, y)
我收到错误消息
ValueError: Found array with dim 4. Estimator expected <= 2
我想将数据加载为数据帧或其他格式以进行训练和拆分,但我无法删除第一个值。
示例数据
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]
...
我的预期输出是如何将数据传递给 svm 进行分类
注意你的data source,你的x.npy没有图片
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.
问题是 SVM 只接受二维数组,您的数据格式为(样本数、行、列、通道)
试试这个,对我有用
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))
我有numpy格式的图片,我已经从网上下载了数据(https://github.com/ichatnun/spatiospectral-densenet-rice-classification/blob/master/x.npy)。数据示例(1, 34, 23, 100),这里1是图像编号,34x23是像素值,100是通道。
我想加载机器学习模型训练的数据,我查看了其他来源,他们的数据格式仅为 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
如果我像这样使用 SVM,
from sklearn.svm import SVC
clf = SVC(gamma='auto')
clf.fit(datasset1, y)
我收到错误消息
ValueError: Found array with dim 4. Estimator expected <= 2
我想将数据加载为数据帧或其他格式以进行训练和拆分,但我无法删除第一个值。
示例数据
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]
...
我的预期输出是如何将数据传递给 svm 进行分类
注意你的data source,你的x.npy没有图片
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.
问题是 SVM 只接受二维数组,您的数据格式为(样本数、行、列、通道)
试试这个,对我有用
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))