如何修复:’只有整数、切片(`:`)、省略号(`…`)、numpy.newaxis(`None`)以及整数或布尔数组是有效的索引’?

我在使用机器学习中的线性回归算法尝试预测患者的心脏病时遇到了这个错误(只有整数、切片(:)、省略号(...)、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:        # 我们用来训练算法的预测变量。          train_predictors = (heart[predictors].iloc[train,:])        print(train_predictors)        # 我们用来训练算法的目标变量。        train_target = heart["heartpred"].iloc[train]        print(train_target)        # 使用预测变量和目标变量训练算法。        alg.fit(train_predictors, train_target)        # 现在我们可以对测试集进行预测        test_predictions = alg.predict(heart[predictors].iloc[test,:])        predictions.append(test_predictions)    # 预测结果存储在三个单独的numpy数组中。将它们连接成一个。      # 我们沿轴0进行连接,因为它们只有一个轴。    predictions = np.concatenate(predictions, axis=0)    # 将预测映射到结果(可能的结果只有1和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("线性回归结果:-")    print("准确率 = ")    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

回答:

您有i=0.0,这意味着i是一个浮点数。您不能用浮点数来索引numpy数组。

    # 将预测映射到结果(可能的结果只有1和0)    predictions[predictions > .5] = 1    predictions[predictions <=.5] = 0    # 更改为整数    i = 0    count = 0    for hpred in heart["heartpred"]:        if hpred == predictions[i]:            count += 1        i+=1    accuracy=count/i    print("线性回归结果:-")    print("准确率 = ")    print(accuracy*100)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注