Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法从numpy数组加载数据以进行SVM分类_Python_Python 3.x_Numpy_Numpy Ndarray - Fatal编程技术网

Python 无法从numpy数组加载数据以进行SVM分类

Python 无法从numpy数组加载数据以进行SVM分类,python,python-3.x,numpy,numpy-ndarray,Python,Python 3.x,Numpy,Numpy Ndarray,我有numpy格式的图像,我从internet()下载了数据。数据示例(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

我有numpy格式的图像,我从internet()下载了数据。数据示例(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
我的预期输出是如何将数据传递给svm进行分类

注意,你的x.npy没有图像

x、 npy包含处理过的大米数据集的示例数据立方体 可用于培训/测试。每个数据立方体都是三维的 50x170x110张量:两个空间维度和一个光谱维度


问题是SVM只接受2d数组,数据的格式是(样本数、行、列、通道)

试试这个,它对我有用

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

看一看这张照片。文件由三维50x170x110张量组成,例如,一个形状为3x50x170x110的数据。为什么我在这里得到3?数据集1的形状为(3,50,170,110),解释器将如何获取3个不同的数据如果你看这里:你会发现:num_training=x_training.shape[0]N_spatial=x_training.shape[1:3]N_bands=x_training.shape[3]谢谢,在N_spatial中,我们得到了图像的行和列,对吗?你没有图像。。。你们有张量,“处理过的大米数据集”好的,但我得到了错误,当传递给支持向量机时,我更新了我的问题,请建议
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))