我正在尝试运行此代码。
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()
当我尝试运行此代码时,它给了我这个错误追踪(最近一次通话):**IDLE内部异常:运行代码中的文件"C:\Python34\lib\idlelib\run.py",第353行exec(代码,self.locals)文件"C:\Python34\run.py",第20行,位于trainData=trainData.astype(np.foat)/25.50ValueError:设置具有序列的数组元素。
删除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))
应该只给你一个值。如果有几个,那么你的一些线条比其他线条长,你必须纠正这一点。