Khadija Saeed
Khadija Saeed

Reputation: 27

"How to fix: 'only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices'?

I'm trying to predict heart disease of patients using liner regression algorithm in machine learning and I have this error(only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices) can anyone please help me to solve it?

  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)

Error shown below:

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

Upvotes: 0

Views: 15251

Answers (1)

geoph9
geoph9

Reputation: 387

You have i=0.0 which means that i is a float. You cannot index a numpy aray with a float number.

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

Upvotes: 1

Related Questions