Back

Explore Courses Blog Tutorials Interview Questions
+5 votes
2 views
in Data Science by (17.6k points)

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

4 Answers

+6 votes
by (41.4k points)

Here i=0.0 means that i is a float. So, we cannot index a numpy array 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)

If you wish to learn more about Data Science, visit Data Science tutorial and Data Science courses by Intellipaat.

by (108k points)
Yup, it worked for me!!! Thank You so much :)
by (19.7k points)
Very well explained!
by (33.1k points)
It also solved my problem.
Thanks.
by (19.9k points)
This worked for me. Thanks.
+3 votes
by (29.5k points)
I think shlok's answer is correct as you are trying to index a numpy array with float that is not correct change that and i think you are good to go
by (44.4k points)
Yes, that's true
+1 vote
by (47.2k points)

I think that problem is your while loop, n is divided by 2, but never cast as an integer again, so it becomes a float at some point. It is then added onto y, which is then a float too, and that gives you the warning.

by (29.3k points)
Thanks! this worked for me.
+1 vote
by (106k points)

To convert int directly into float Python has a nice property where you can use // instead of single /.

Browse Categories

...