ndexError: допустимыми являются только целые числа, кусочки (`:`), многоточие (`...`), numpy.newaxis (`None`) и целые или логические массивы. - PullRequest
0 голосов
/ 21 января 2019

Программа запускает график, однако не выводит результаты вывода в консоль, поскольку обнаруживает ошибку, обнаруженную в строке 103, которая говорит: «IndexError: только целые числа, срезы (:), многоточие (...), numpy.newaxis (None) и целые или логические массивы являются допустимыми индексами «Как это исправить?

Этот код выполняет классификацию заболеваний сердца, разделяя прогнозируемые значения на два набора, а именно 0 для отсутствия и 1 для присутствия, где все прогнозируемые значения от 1 до 4 заменяются на 1 для проверки производительности модели

from numpy import genfromtxt
import numpy as np
import matplotlib
matplotlib.use('TKAgg')
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
from sklearn.decomposition import PCA
import pylab as pl
from itertools import cycle
from sklearn import cross_validation
from sklearn.svm import SVC 

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

#Loading and pruning the data
dataset = genfromtxt('cleveland_data.csv',dtype = float, delimiter=',')
#print dataset
X = dataset[:,0:12] #Feature Set
y = dataset[:,13]   #Label Set

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

#Method to plot the graph for reduced Dimesions
def plot_2D(data, target, target_names):
     colors = cycle('rgbcmykw')
     target_ids = range(len(target_names))
     plt.figure()
     for i, c, label in zip(target_ids, colors, target_names):
         plt.scatter(data[target == i, 0], data[target == i, 1],
                    c=c, label=label)
     plt.legend()
     plt.savefig('Problem 2 Graph')

# Classifying the data using a Linear SVM and predicting the probability of disease belonging to a particular class
modelSVM = LinearSVC(C=0.001)
pca = PCA(n_components=5, 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.001)
modelSVMRaw = modelSVMRaw.fit(X_new, y)
cnt = 0
for i in modelSVMRaw.predict(X_new):
    if i == y[i]:
       cnt = cnt+1
print("Linear SVC score without split")
print(int(cnt)//303)

#Applying the Principal Component Analysis on the data features
modelSVM2 = SVC(C=0.001,kernel='rbf')

#Applying cross validation on the training and test set for validating our Linear SVM Model
X_train1, X_test1, y_train1, y_test1 = cross_validation.train_test_split(X_new, y, test_size=0.2, train_size=0.8, random_state=0)
modelSVM2 = modelSVM2.fit(X_train1, y_train1)
print("RBF score with split")
print(modelSVM2.score(X_test1, y_test1))

modelSVM2Raw = SVC(C=0.001,kernel='rbf')
modelSVM2Raw = modelSVM2Raw.fit(X_new, y)
cnt1 = 0
for i in modelSVM2Raw.predict(X_new):
        if i == y[i]:
           cnt1 = cnt1+1
print("RBF score without split")
print(float(cnt1)//303)

#Using Stratified K Fold
skf = cross_validation.StratifiedKFold(y, n_folds=5)
for train_index, test_index in skf:
   # print("TRAIN:", train_index, "TEST:", test_index)
    X_train3, X_test3 = X[train_index], X[test_index]
    y_train3, y_test3 = y[train_index], y[test_index]
modelSVM3 = SVC(C=0.001,kernel='rbf')
modelSVM3 = modelSVM3.fit(X_train3, y_train3)
print("Stratified K fold score")
print(modelSVM3.score(X_test3, y_test3))

modelSVM3Raw = SVC(C=0.001,kernel='rbf')
modelSVM3Raw = modelSVM3Raw.fit(X_new, y)
cnt2 = 0
for i in modelSVM3Raw.predict(X_new):
        if i == y[i]:
           cnt2 = cnt2+1
print("On PCA valued X_new")
print(float(cnt2)//303)

        #Text interpretation
fig = plt.figure(figsize=(5, 1.5))
t = fig.text(0.5, 0.5, 'Problem 1 \nTesting Linear SVC values using Split \n0.5491803278688525 \nTesting with RBF using split \n0.4918032786885246 \nTesting using stratified with K folds \n0.5423728813559322',
ha='center', va='center', size=15)
text.set_path_effects([path_effects.path_2d()])
plt.show()

Программа только запускает график, но не показывает его результаты.Я ожидаю, что консоль напечатает результаты / интерпретацию моделей SVM и их значений.

...