在程序中,我正在扫描一系列大脑样本,这些样本是以40 x 64 x 64的图像形式每2.5秒采集一次的。每张图像中的“体素”(3D像素)数量大约为168,000左右(40 * 64 * 64),每个体素都是图像样本的一个“特征”。
由于特征数量极高,我考虑使用主成分分析(PCA)来进行降维处理。接着使用递归特征消除(RFE)进一步处理。
需要预测的类别有9个,因此这是一个多类分类问题。下面,我将这个9类分类问题转换为二元分类问题,并将模型存储在一个名为models的列表中。
models = []model_count = 0for i in range(0,DS.nClasses): for j in range(i+1,DS.nClasses): binary_subset = sample_classes[i] + sample_classes[j] print 'length of combined = %d' % len(binary_subset) X,y = zip(*binary_subset) print 'y = ',y estimator = SVR(kernel="linear") rfe = RFE(estimator , step=0.05) rfe = rfe.fit(X, y) #save the model models.append(rfe) model_count = model_count + 1 print '%d model fitting complete!' % model_count
现在遍历这些模型并进行预测。
predictions = []for X,y in test_samples: Votes = np.zeros(DS.nClasses) for mod in models: #X = mod.transform(X) label = mod.predict(X.reshape(1,-1)) #这里出了问题 print 'label is type',type(label),' and value ',label Votes[int(label)] = Votes[int(label)] + 1 prediction = np.argmax(Votes) predictions.append(prediction) print 'Votes Array = ',Votes print "We predicted %d , actual is %d" % (prediction,y)
标签应该是0到8之间的数字,表示9种可能的结果。我打印了label的值,得到的结果如下:
label is type <type 'numpy.ndarray'> and value [ 0.87011103]label is type <type 'numpy.ndarray'> and value [ 2.09093105]label is type <type 'numpy.ndarray'> and value [ 1.96046739]label is type <type 'numpy.ndarray'> and value [ 2.73343935]label is type <type 'numpy.ndarray'> and value [ 3.60415663]label is type <type 'numpy.ndarray'> and value [ 6.10577602]label is type <type 'numpy.ndarray'> and value [ 6.49922691]label is type <type 'numpy.ndarray'> and value [ 8.35338294]label is type <type 'numpy.ndarray'> and value [ 1.29765466]label is type <type 'numpy.ndarray'> and value [ 1.60883217]label is type <type 'numpy.ndarray'> and value [ 2.03839272]label is type <type 'numpy.ndarray'> and value [ 2.03794106]label is type <type 'numpy.ndarray'> and value [ 2.58830013]label is type <type 'numpy.ndarray'> and value [ 3.28811133]label is type <type 'numpy.ndarray'> and value [ 4.79660621]label is type <type 'numpy.ndarray'> and value [ 2.57755697]label is type <type 'numpy.ndarray'> and value [ 2.72263461]label is type <type 'numpy.ndarray'> and value [ 2.58129428]label is type <type 'numpy.ndarray'> and value [ 3.96296151]label is type <type 'numpy.ndarray'> and value [ 4.80280219]label is type <type 'numpy.ndarray'> and value [ 7.01768046]label is type <type 'numpy.ndarray'> and value [ 3.3720926]label is type <type 'numpy.ndarray'> and value [ 3.67517869]label is type <type 'numpy.ndarray'> and value [ 4.52089242]label is type <type 'numpy.ndarray'> and value [ 4.83746684]label is type <type 'numpy.ndarray'> and value [ 6.76557315]label is type <type 'numpy.ndarray'> and value [ 4.606097]label is type <type 'numpy.ndarray'> and value [ 6.00243346]label is type <type 'numpy.ndarray'> and value [ 6.59194317]label is type <type 'numpy.ndarray'> and value [ 7.63559593]label is type <type 'numpy.ndarray'> and value [ 5.8116106]label is type <type 'numpy.ndarray'> and value [ 6.37096926]label is type <type 'numpy.ndarray'> and value [ 7.57033285]label is type <type 'numpy.ndarray'> and value [ 6.29465433]label is type <type 'numpy.ndarray'> and value [ 7.91623641]label is type <type 'numpy.ndarray'> and value [ 7.79524801]Votes Array = [ 1. 3. 8. 5. 5. 1. 7. 5. 1.]We predicted 2 , actual is 8
我不知道为什么label值是浮点数。它们应该是0到8之间的数字。
我正确地加载了数据。在执行predict()
时出了问题,但我仍然找不到问题出在哪里。
回答:
你得到浮点值是因为你使用了SVR:支持向量回归。你需要的是SVC,支持向量分类。