-2

i am trying to run this code.

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

while i am trying run this code it is giving me this error Traceback (most recent call last): ** IDLE Internal Exception: File "C:\Python34\lib\idlelib\run.py", line 353, in runcode exec(code, self.locals) File "C:\Python34\run.py", line 20, in trainData = trainData.astype(np.float)/255.0 ValueError: setting an array element with a sequence.

GoBusto
  • 4,632
  • 6
  • 28
  • 45

1 Answers1

1

Remove the del statements and replace what comes after by a sliced version of the list. Ie

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

becomes

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

Then check line length consistency:

np.unique(map(len, trainData))

should give you only one value. If there are several, then some of your lines are longer than others and you will have to correct this.

eickenberg
  • 14,152
  • 1
  • 48
  • 52
  • it is still giving the same error please run my code on your compiler and tell me its urgent – rohan kumar Oct 15 '14 at 12:18
  • your example is not self-contained or minimal. Nobody but you can run it, or help you, without extra information. My suggestion was based on the common reason for this error. If you are sure your csv lines all have the same length, then the error is somewhere else, and I am unwilling to spend time placing bets. – eickenberg Oct 15 '14 at 13:19