【发布时间】:2021-03-30 01:03:18
【问题描述】:
我正在尝试做心脏病的机器学习实践问题,来自 kaggle 的数据集。 然后我尝试将数据拆分为训练集和测试集,然后将模型组合成单个函数并进行预测,这个错误出现在 jupyter notebook 中。
这是我的代码:
# Split data into X and y
X = df.drop("target", axis=1)
y = df["target"]
分割
# Split data into train and test sets
np.random.seed(42)
# Split into train & test set
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
预测函数
# Put models in a dictionary
models = {"Logistic Regression": LogisticRegression(),
"KNN": KNeighborsClassifier(),
"Random Forest": RandomForestClassifier()}
# Create a function to fit and score models
def fit_and_score(models, X_train, X_test, y_train, y_test):
"""
Fits and evaluates given machine learning models.
models : a dict of differetn Scikit-Learn machine learning models
X_train : training data (no labels)
X_test : testing data (no labels)
y_train : training labels
y_test : test labels
"""
# Set random seed
np.random.seed(42)
# Make a dictionary to keep model scores
model_scores = {}
# Loop through models
for name, model in models.items():
# Fit the model to the data
model.fit(X_train, y_train)
# Evaluate the model and append its score to model_scores
model_scores[name] = model.score(X_test, y_test)
return model_scores
当我运行此代码时,会出现该错误
model_scores = fit_and_score(models=models,
X_train=X_train,
X_test=X_test,
y_train=y_train,
y_test=y_test)
model_scores
【问题讨论】:
-
哪里出错了?
-
在我上一个代码之后
-
请重新阅读 How to ask,因为您第一次阅读时似乎错过了一些关键点,即“DO NOT发布代码、数据、错误消息等的图像。 - 将文本复制或输入到问题中”(强调原文)。
标签: python pandas machine-learning jupyter-notebook kaggle