我已经用数据训练好决策树和随机森林,现在需要用输入数据,用数据预测这组数据描述的病人是否患有心脏病,请给我测试的代码。下面是我的训练过程import pandas as pd from sklearn import tree from sklearn.ensemble import RandomForestClassifier import matplotlib.pyplot as plt import numpy as np heart=pd.read_csv("D:\Anaconda1\heart.csv") heart.head() heart.info() clf_tree = tree.DecisionTreeClassifier() from sklearn.model_selection import train_test_split x = heart.drop('target',axis = 1) y = heart['target'] x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=0) clf_tree.fit(x_train,y_train) print(clf_tree.score(x_train,y_train)) print(clf_tree.score(x_test,y_test)) tree.export_graphviz(clf, out_file='tree.dot') score_list=[] for i in range(10,100,10): clf_forest = RandomForestClassifier(n_estimators = i, random_state = 0) clf_forest.fit(x_train,y_train) score_list.append(clf_forest.score(x_test,y_test)) plt.plot(range(10,100,10), score_list) plt.show() print(np.argmax(score_list)) print(np.max(score_list))
时间: 2023-12-24 20:22:45 浏览: 86
基于随机森林实现的心脏病分类预测python源码(数据挖掘作业).zip
好的,给您提供一份用训练好的决策树和随机森林模型进行预测的代码:
```
import pandas as pd
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
# 读入测试数据
test_data = pd.read_csv("test_data.csv")
# 加载训练好的决策树模型
clf_tree = tree.DecisionTreeClassifier()
clf_tree.load("tree_model.pkl")
# 加载训练好的随机森林模型
clf_forest = RandomForestClassifier()
clf_forest.load("forest_model.pkl")
# 预测决策树模型结果
tree_result = clf_tree.predict(test_data)
# 预测随机森林模型结果
forest_result = clf_forest.predict(test_data)
# 输出预测结果
print("决策树模型预测结果:", tree_result)
print("随机森林模型预测结果:", forest_result)
```
需要将上面的代码中的 `test_data.csv` 替换为你要预测的测试数据的文件路径,并且需要将 `tree_model.pkl` 和 `forest_model.pkl` 替换为训练好的决策树模型和随机森林模型的文件路径。
阅读全文