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

Я пытаюсь предсказать болезнь сердца пациентов, используя алгоритм регрессии лайнера в машинном обучении, и у меня есть эта ошибка (только целые числа, срезы (:), многоточие (...), numpy.newaxis (None) ) и целые или логические массивы являются действительными индексами) Может кто-нибудь, пожалуйста, помогите мне решить это?

  import pandas
    import numpy as np
    from sklearn.linear_model import LinearRegression
    from sklearn.cross_validation import KFold
    heart = pandas.read_csv("pc.csv")
    heart.loc[heart["heartpred"]==2,"heartpred"]=1
    heart.loc[heart["heartpred"]==3,"heartpred"]=1
    heart.loc[heart["heartpred"]==4,"heartpred"]=1
    heart["slope"] = heart["slope"].fillna(heart["slope"].median())
    heart["thal"] = heart["thal"].fillna(heart["thal"].median())
    heart["ca"] = heart["ca"].fillna(heart["ca"].median())
    print(heart.describe())
    predictors=["age","sex","cp","trestbps","chol","fbs","restecg","thalach","exang","oldpeak","slope","ca","thal"]
    alg=LinearRegression()
    kf=KFold(heart.shape[0],n_folds=3, random_state=1)
    predictions = []
    for train, test in kf:
        # The predictors we're using the train the algorithm.  
        train_predictors = (heart[predictors].iloc[train,:])
        print(train_predictors)
        # The target we're using to train the algorithm.
        train_target = heart["heartpred"].iloc[train]
        print(train_target)
        # Training the algorithm using the predictors and target.
        alg.fit(train_predictors, train_target)
        # We can now make predictions on the test fold
        test_predictions = alg.predict(heart[predictors].iloc[test,:])
        predictions.append(test_predictions)
    # The predictions are in three separate numpy arrays.  Concatenate them into one.  
    # We concatenate them on axis 0, as they only have one axis.
    predictions = np.concatenate(predictions, axis=0)

    # Map predictions to outcomes (only possible outcomes are 1 and 0)
    predictions[predictions > .5] = 1
    predictions[predictions <=.5] = 0
    i=0.0
    count=0
    for each in heart["heartpred"]:
        if each==predictions[i]:
            count+=1
        i+=1
    accuracy=count/i
    print("Linear Regression Result:-")
    print("Accuracy = ")
    print(accuracy*100)

Ошибка, показанная ниже:

File "C:\Users\Khadeej\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", 
line 705, in runfile execfile(filename, namespace) File "C:\Users\Khadeej\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", 
line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Khadeej/Desktop/Heart-Disease-Prediction-master/linear.py", 
line 39, in <module> if each==predictions[i]: 
IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

1 Ответ

0 голосов
/ 15 июня 2019

У вас есть i=0.0, что означает, что я поплавок. Вы не можете индексировать numpy aray с помощью числа с плавающей точкой.

    # Map predictions to outcomes (only possible outcomes are 1 and 0)
    predictions[predictions > .5] = 1
    predictions[predictions <=.5] = 0
    # Change to an integer
    i = 0
    count = 0
    for hpred in heart["heartpred"]:
        if hpred == predictions[i]:
            count += 1
        i+=1
    accuracy=count/i
    print("Linear Regression Result:-")
    print("Accuracy = ")
    print(accuracy*100)
...