Deeksha
Deeksha

Reputation: 213

Python error - only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

I'm trying to implement SVM in python, but I can't figure out the error in this line: if i == y[i] :

dataset = genfromtxt('E:\cleveland.csv',dtype= float,delimiter=',')
#print dataset
X = dataset[:,0:12] #labels
y = dataset[:,13]  #target

#replace 1 to 4 by 1 label
for index,item in enumerate(y):
    if not(item==0.0):
        print(y)
target_names = ['0', '1']

modelSVM = LinearSVC(C=0.1)
pca = PCA(n_components=2, whiten=True).fit(X)
X_new = pca.transform(X)

#calling plot_2D
plot_2D(X_new,y,target_names)

#Applying cross validation on the training and test set for validating our Linear SVM model
X_train, X_test, y_train, y_test=cross_validation.train_test_split(X_new,y,test_size=0.2,train_size=0.8,random_state=0) 
modelSVM = modelSVM.fit(X_train,y_train)
print ("Linear SVC values with split")
print (modelSVM.score(X_test,y_test))
modelSVMRaw = LinearSVC(C=0.1)
modelSVMRaw = modelSVMRaw.fit(X_new,y)
cnt=0
for i in modelSVMRaw.predict(X_new):
    if i == y[i]:
        cnt=cnt+1

What is the slicing error there? Or any error for that matter

Upvotes: 3

Views: 8914

Answers (1)

Umar Dastgir
Umar Dastgir

Reputation: 724

The value returned by modelSVMRaw.predict(X_new) is not an integer, and you are using that to index the array y.

Upvotes: 2

Related Questions