-2

我正在尝试运行此代码。

import csv
import numpy as np
from sklearn import svm, datasets, cross_validation
from sklearn.grid_search import GridSearchCV

###Load Training Data
trainTargetArray = []
trainDataArray = []
with open('ocp_training.csv', 'r') as trainFile:
trainReader = csv.reader(trainFile, delimiter = ',')
for row in trainReader:
    trainTargetArray.append(row[0])     
    trainDataArray.append(row[1:])

#Delete column headers      
del trainTargetArray[0]
del trainDataArray[0]
trainData = np.array(trainDataArray)
trainTarget = np.array(trainTargetArray)
trainData = trainData.astype(np.float)/255.0
trainTarget = trainTarget.astype(np.float)

###Load Testing Data
testDataArray = []
with open('ocp_testing.csv', 'r') as testFile:
testReader = csv.reader(testFile, delimiter = ',')
for row in testReader:
    testDataArray.append(row)

  #Delete column headers        
del testDataArray[0]
testData = np.array(testDataArray)
testData = testData.astype(np.float)/255.0

#Set up classification and fit the model data
svc = svm.SVC(gamma=0.128, C=1)
svc.fit(trainData, trainTarget)

#Predict/Determine Value of New Images
prediction = svc.predict(testData)

#Save output to file
output = open('./output.csv', 'w')
for x, value in np.ndenumerate(prediction):
   output.write(str(int(value)))
   output.write("\n")
output.close()

当我尝试运行此代码时,它给了我这个错误 Traceback(最近一次调用最后一次):** IDLE Internal Exception: File "C:\Python34\lib\idlelib\run.py", line 353, in runco​​de exec(代码,self.locals) 文件“C:\Python34\run.py”,第 20 行,在 trainData = trainData.astype(np.float)/255.0 ValueError: 设置具有序列的数组元素。

4

1 回答 1

1

删除del语句并用列表的切片版本替换后面的内容。IE

del trainTargetArray[0]
del trainDataArray[0]
trainData = np.array(trainDataArray)
trainTarget = np.array(trainTargetArray)

变成

trainData = np.array(trainDataArray[1:])
trainTarget = np.array(trainTargetArray[1:])

然后检查行长一致性:

np.unique(map(len, trainData))

应该只给你一个值。如果有多个,那么您的某些行比其他行长,您将不得不更正这一点。

于 2014-10-15T11:35:08.890 回答