from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
1、模型准确率
<------------随机森林模型运行结果-------------->
读取数据
df = pd.read_csv(‘C:\Users\31425\Desktop\新建文件夹\序列\蒿属1.csv’)
分离特征和目标变量
X = df.iloc[:, 1:-1]
y = df.iloc[:, -1]
将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, 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)