Python 随机森林机器学习代码示例:最佳模型训练、保存及预测
from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split from sklearn.datasets import load_iris from sklearn.externals import joblib
加载鸢尾花数据集
iris = load_iris()
将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
创建随机森林模型
rf = RandomForestClassifier(n_estimators=100, random_state=42)
训练模型
rf.fit(X_train, y_train)
预测测试集
y_pred = rf.predict(X_test)
计算准确率
accuracy = accuracy_score(y_test, y_pred) print('随机森林模型的准确率为:', accuracy)
保存最佳模型
joblib.dump(rf, 'best_model.pkl')
加载最佳模型
best_model = joblib.load('best_model.pkl')
加载新的数据
new_data = [[5.1, 3.5, 1.4, 0.2], [6.2, 3.4, 5.4, 2.3], [4.9, 2.5, 4.5, 1.7]]
使用最佳模型进行预测
new_pred = best_model.predict(new_data)
print('新数据的预测结果为:', new_pred)
原文地址: https://www.cveoy.top/t/topic/nwl4 著作权归作者所有。请勿转载和采集!