我在使用机器学习中的线性回归算法尝试预测患者的心脏病时遇到了这个错误(只有整数、切片(:
)、省略号(...
)、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)