如何计算scikit-learn MLPClassifier中的分数。获取numpy.float64是不可迭代的

时间:2018-05-04 16:51:08

标签: python scikit-learn

我正在使用scikit-learn MLPClassifier(神经网络)对某些图像进行分类。 图像数据作为浮动的多维数组返回。如果我不将1个热编码标签转换为浮点数,我在尝试训练模型时会出现标签不匹配的情况,因此我将其转换。然而,当我去预测得分时,现在我得到了#34; ' numpy.float64'对象不可迭代"。关于如何使其发挥作用的任何建议?

import numpy as np
import sys
import pandas as pd
from skimage import io
from skimage import transform as trans
from sklearn.neural_network import MLPClassifier as NN
from sklearn.model_selection import train_test_split

#Get the data
print ("Reading CSV...")
data = pd.read_csv(filepath_or_buffer="hot_dog_data.csv", nrows=30)
X = data.values[1:,0]
Y = data.values[1:,1:8]

#convert the images to RGB number arrays
print ('Converting Images...')
img_converts = []
for line in X:
  img = io.imread("./images/"+line)
  img = trans.resize(img,(300,400), mode='constant')
  img_converts.append(img)
X = np.array(img_converts)

# Split into train and test vars
trainX, testX, trainY, testY = train_test_split(X,Y, test_size=0.17)

# Reshape the image arrays into 2-D arrays so it will fit the model
xa, xb, xc, xd = trainX.shape
d2_trainX = trainX.reshape((xa, xb*xc*xd))

xe, xf, xg, xh = testX.shape
d2_testX = testX.reshape((xe, xf*xg*xh))

clf = NN(solver='lbfgs',hidden_layer_sizes=(5, 2), random_state=1)

# Recast the Y data so the fit won't get a label mismatch
trainY = np.asarray(trainY, dtype=np.float)
testY = np.asarray(testY, dtype=np.float)

print ('The machine is learning...')
clf.fit(d2_trainX, trainY)

print ('Predicting...')
count = 1 
for line in clf.predict(d2_testX):
  print (count, line )
  count += 1

print 'Calculating Accuracy...'
count = 1
for x,line in clf.score(d2_testX, testY):
  print (count, line)

sys.exit()

1 个答案:

答案 0 :(得分:0)

在第

for x,line in clf.score(d2_testX, testY): 

您正在尝试迭代score()返回的浮点值。

  

得分(X,y,sample_weight =无)

     

...

     

返回:得分:浮动

See the documentation for score for more details.