今天完成机器学习B的实验,以下为实验部分代码:
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split, cross_val_score from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report, confusion_matrix # 加载iris数据集 iris = load_iris() X, y = iris.data, iris.target # 使用留出法留出1/3的样本作为测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=42) # 初始化决策树分类器,设置预剪枝参数 # 注意:scikit-learn中的DecisionTreeClassifier没有直接的后剪枝参数,但我们可以通过设置max_depth来控制树的深度 dt = DecisionTreeClassifier( criterion='gini', # 选择分割质量的测量标准,默认为'gini' splitter='best', # 选择属性的分割策略,默认为'best',可选'random' max_depth=3, # 树的最大深度,这里设置为3来进行预剪枝 min_samples_split=2, # 节点划分时所需的最小样本数,默认为2 min_samples_leaf=1, # 叶节点所需的最小样本数,默认为1 random_state=42 # 随机数生成器的种子 ) # 训练模型 dt.fit(X_train, y_train) # 预测训练集和测试集 y_train_pred = dt.predict(X_train) y_pred = dt.predict(X_test) # 评估训练集和测试集的性能 train_accuracy = accuracy_score(y_train, y_train_pred) test_accuracy = accuracy_score(y_test, y_pred) train_precision = precision_score(y_train, y_train_pred, average='macro') test_precision = precision_score(y_test, y_pred, average='macro') train_recall = recall_score(y_train, y_train_pred, average='macro') test_recall = recall_score(y_test, y_pred, average='macro') train_f1 = f1_score(y_train, y_train_pred, average='macro') test_f1 = f1_score(y_test, y_pred, average='macro') # 打印性能评估结果 print("Training Set Performance:") print(f"Accuracy: {train_accuracy:.4f}") print(f"Precision: {train_precision:.4f}") print(f"Recall: {train_recall:.4f}") print(f"F1 Score: {train_f1:.4f}\n") print("Test Set Performance:") print(f"Accuracy: {test_accuracy:.4f}") print(f"Precision: {test_precision:.4f}") print(f"Recall: {test_recall:.4f}") print(f"F1 Score: {test_f1:.4f}\n") # 使用五折交叉验证评估模型性能 cross_val_scores = cross_val_score(dt, X_train, y_train, cv=5, scoring='accuracy') print(f"Cross-validation scores: {cross_val_scores}") print(f"Mean accuracy: {cross_val_scores.mean():.4f}") print(f"Standard deviation: {cross_val_scores.std():.4f}\n") # 打印分类报告和混淆矩阵 print("Classification Report:\n", classification_report(y_test, y_pred, target_names=iris.target_names)) print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
标签:11.29,score,日报,pred,print,train,4f,test From: https://www.cnblogs.com/lijianlongCode13/p/18604381