我试图在机器学习中使用线性回归算法来预测患者的心脏病,但我有这样的错误(只有整数、切片(:
)、省略号(...
),新轴(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)
错误如下:
^{pr2}$
你有
i=0.0
,这意味着我是一个浮点数。你不能用浮点数来索引numpy aray。在相关问题 更多 >
编程相关推荐