数据集下载地址(一此常见的数据科学 Python 库,都集成了鸢尾花(iris)数据集)
https://github.com/jackgo2080/dataOpen/blob/main/iris.data
数据集详情
鸢尾花数据集共收集了三类鸢尾花,即Setosa鸢尾花、Versicolour鸢尾花和Virginica鸢尾花,每一类鸢尾花收集了50条样本记录,共计150条。
数据集包括4个属性,分别为花萼的长、花萼的宽、花瓣的长和花瓣的宽。对花瓣我们可能比较熟悉,花萼是什么呢?花萼是花冠外面的绿色被叶,在花尚未开放时,保护着花蕾。四个属性的单位都是cm,属于数值变量,四个属性均不存在缺失值的情况,字段如下:
sepal length(萼片长度)
sepal width(萼片宽度)
petal length(花瓣长度)
petal width (花瓣宽度)
Species(品种类别):分别是:Setosa、Versicolour、Virginica
单位都是厘米。
当然,以下是一个使用Python和scikit-learn库的机器学习模型编程示例,这个例子涵盖了数据预处理、模型选择、训练和评估的全过程。
假设我们正在处理一个分类问题,我们将使用著名的鸢尾花(Iris)数据集来演示这个过程。
1. 数据预处理
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
# 加载数据
iris = load_iris()
X = iris.data
y = iris.target
# 将数据集分割为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 数据清洗:这里我们假设数据集已经比较干净,不需要额外处理
# 缺失值处理
imputer = SimpleImputer(strategy='mean') # 用均值填充缺失值
X_train = imputer.fit_transform(X_train)
X_test = imputer.transform(X_test)
# 特征选择:这里我们暂时使用所有特征,不进行特征选择
# 数据标准化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
2. 选择模型
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
# 决策树模型
dt = DecisionTreeClassifier(random_state=42)
# 随机森林模型
rf = RandomForestClassifier(random_state=42)
# 支持向量机模型
svm = SVC(random_state=42)
3. 训练模型
from sklearn.model_selection import GridSearchCV
# 决策树参数调优
param_grid_dt = {
'criterion': ['gini', 'entropy'],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10]
}
grid_search_dt = GridSearchCV(dt, param_grid_dt, cv=5)
grid_search_dt.fit(X_train, y_train)
# 选择最佳参数的决策树模型
best_dt = grid_search_dt.best_estimator_
4. 评估模型
from sklearn.metrics import accuracy_score, recall_score, f1_score, classification_report
# 使用测试集评估模型
y_pred_dt = best_dt.predict(X_test)
# 计算评估指标
accuracy = accuracy_score(y_test, y_pred_dt)
recall = recall_score(y_test, y_pred_dt, average='macro')
f1 = f1_score(y_test, y_pred_dt, average='macro')
print(f"Accuracy: {accuracy}")
print(f"Recall: {recall}")
print(f"F1 Score: {f1}")
# 打印分类报告
print(classification_report(y_test, y_pred_dt, target_names=iris.target_names))
这个例子中,我们首先对数据进行了预处理,包括缺失值处理和数据标准化。然后,我们选择了三种不同的机器学习模型:决策树、随机森林和支持向量机。为了优化决策树模型,我们使用了网格搜索(GridSearchCV)进行参数调优。最后,我们使用准确率、召回率和F1值作为评估指标来评估模型的性能,并打印了分类报告。
请注意,这只是一个简单的例子,实际应用中可能需要更复杂的数据预处理、特征工程、模型选择和参数调优步骤。此外,根据问题的具体情况,可能还需要考虑其他评估指标和模型。
完整代码
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
# 加载数据
iris = load_iris()
X = iris.data
y = iris.target
# 将数据集分割为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 数据清洗:这里我们假设数据集已经比较干净,不需要额外处理
# 缺失值处理
imputer = SimpleImputer(strategy='mean') # 用均值填充缺失值
X_train = imputer.fit_transform(X_train)
X_test = imputer.transform(X_test)
# 特征选择:这里我们暂时使用所有特征,不进行特征选择
# 数据标准化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
print(X_train, X_test)
#------------------------选择模型---------------------------
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
# 决策树模型
dt = DecisionTreeClassifier(random_state=42)
# 随机森林模型
rf = RandomForestClassifier(random_state=42)
# 支持向量机模型
svm = SVC(random_state=42)
#------------------------训练模型---------------------------
from sklearn.model_selection import GridSearchCV
# 决策树参数调优
param_grid_dt = {
'criterion': ['gini', 'entropy'],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10]
}
grid_search_dt = GridSearchCV(dt, param_grid_dt, cv=5)
grid_search_dt.fit(X_train, y_train)
# 选择最佳参数的决策树模型
best_dt = grid_search_dt.best_estimator_
#--------------------评估模型------------------------
from sklearn.metrics import accuracy_score, recall_score, f1_score, classification_report
# 使用测试集评估模型
y_pred_dt = best_dt.predict(X_test)
# 计算评估指标
accuracy = accuracy_score(y_test, y_pred_dt)
recall = recall_score(y_test, y_pred_dt, average='macro')
f1 = f1_score(y_test, y_pred_dt, average='macro')
print(f"Accuracy: {accuracy}")
print(f"Recall: {recall}")
print(f"F1 Score: {f1}")
# 打印分类报告
print(classification_report(y_test, y_pred_dt, target_names=iris.target_names))
运行结果
[[-1.47393679 1.20365799 -1.56253475 -1.31260282]
[-0.13307079 2.99237573 -1.27600637 -1.04563275]
[ 1.08589829 0.08570939 0.38585821 0.28921757]
[-1.23014297 0.75647855 -1.2187007 -1.31260282]
[-1.7177306 0.30929911 -1.39061772 -1.31260282]
[ 0.59831066 -1.25582892 0.72969227 0.95664273]
[ 0.72020757 0.30929911 0.44316389 0.4227026 ]
[-0.74255534 0.98006827 -1.27600637 -1.31260282]
[-0.98634915 1.20365799 -1.33331205 -1.31260282]
[-0.74255534 2.32160658 -1.27600637 -1.44608785]
[-0.01117388 -0.80864948 0.78699794 0.95664273]
[ 0.23261993 0.75647855 0.44316389 0.55618763]
[ 1.08589829 0.08570939 0.55777524 0.4227026 ]
[-0.49876152 1.87442714 -1.39061772 -1.04563275]
[-0.49876152 1.4272477 -1.27600637 -1.31260282]
[-0.37686461 -1.47941864 -0.01528151 -0.24472256]
[ 0.59831066 -0.58505976 0.78699794 0.4227026 ]
[ 0.72020757 0.08570939 1.01622064 0.8231577 ]
[ 0.96400139 -0.13788033 0.38585821 0.28921757]
[ 1.69538284 1.20365799 1.3600547 1.75755292]
[-0.13307079 -0.36147005 0.27124686 0.15573254]
[ 2.18297047 -0.13788033 1.64658307 1.22361279]
[-0.2549677 -0.13788033 0.44316389 0.4227026 ]
[-0.86445224 0.98006827 -1.33331205 -1.31260282]
[ 2.30486738 -0.58505976 1.70388875 1.09012776]
[-0.01117388 -0.80864948 0.21394119 -0.24472256]
[-0.74255534 0.75647855 -1.33331205 -1.31260282]
[-0.98634915 0.98006827 -1.39061772 -1.17911778]
[-0.86445224 1.65083742 -1.04678367 -1.04563275]
[-0.98634915 -2.37377751 -0.12989286 -0.24472256]
[ 0.59831066 -0.80864948 0.67238659 0.8231577 ]
[-1.23014297 0.75647855 -1.04678367 -1.31260282]
[-0.98634915 -0.13788033 -1.2187007 -1.31260282]
[-0.86445224 0.53288883 -1.16139502 -0.91214772]
[-0.2549677 -0.80864948 0.27124686 0.15573254]
[-0.86445224 0.75647855 -1.27600637 -1.31260282]
[-0.13307079 -0.13788033 0.27124686 0.02224751]
[ 2.30486738 1.65083742 1.70388875 1.35709783]
[-1.47393679 0.30929911 -1.33331205 -1.31260282]
[ 0.47641375 -0.36147005 0.32855254 0.15573254]
[-0.13307079 -1.25582892 0.72969227 1.09012776]
[-0.37686461 2.5451963 -1.33331205 -1.31260282]
[ 0.23261993 -0.13788033 0.61508092 0.8231577 ]
[-0.01117388 -0.80864948 0.78699794 0.95664273]
[ 0.23261993 -1.92659808 0.15663551 -0.24472256]
[-0.49876152 -0.13788033 0.44316389 0.4227026 ]
[ 0.47641375 0.75647855 0.95891497 1.49058286]
[-0.37686461 -1.70300836 0.15663551 0.15573254]
[-0.49876152 1.87442714 -1.16139502 -1.04563275]
[-0.98634915 -1.70300836 -0.24450422 -0.24472256]
[ 0.72020757 -0.80864948 0.90160929 0.95664273]
[-0.98634915 0.53288883 -1.33331205 -1.31260282]
[-0.98634915 0.30929911 -1.4479234 -1.31260282]
[-0.37686461 -1.47941864 0.04202416 -0.11123753]
[ 1.08589829 -0.13788033 0.72969227 0.68967267]
[-1.10824606 0.08570939 -1.27600637 -1.31260282]
[-0.01117388 -0.58505976 0.78699794 1.62406789]
[-0.98634915 0.75647855 -1.27600637 -1.31260282]
[-0.98634915 0.98006827 -1.2187007 -0.77866269]
[ 0.11072303 0.30929911 0.61508092 0.8231577 ]
[-0.86445224 -1.25582892 -0.41642124 -0.11123753]
[ 1.32969211 0.30929911 1.130832 1.49058286]
[ 0.23261993 -0.80864948 0.78699794 0.55618763]
[ 0.35451684 -1.0322392 1.07352632 0.28921757]
[ 2.30486738 -0.13788033 1.3600547 1.49058286]
[-0.37686461 -1.25582892 0.15663551 0.15573254]
[-1.7177306 -0.36147005 -1.33331205 -1.31260282]
[-1.83962751 -0.13788033 -1.50522907 -1.44608785]
[ 0.23261993 -1.92659808 0.72969227 0.4227026 ]
[ 1.69538284 0.30929911 1.30274902 0.8231577 ]
[-1.47393679 0.08570939 -1.27600637 -1.31260282]
[-0.86445224 0.98006827 -1.33331205 -1.17911778]
[-1.7177306 -0.13788033 -1.39061772 -1.31260282]
[ 0.59831066 -1.25582892 0.67238659 0.4227026 ]
[ 0.59831066 0.75647855 1.07352632 1.62406789]
[-1.47393679 0.75647855 -1.33331205 -1.17911778]
[ 1.2077952 -0.13788033 1.01622064 1.22361279]
[ 0.59831066 0.53288883 1.30274902 1.75755292]
[-1.35203988 0.30929911 -1.39061772 -1.31260282]
[ 0.35451684 -0.36147005 0.55777524 0.28921757]
[ 0.84210448 -0.58505976 0.50046957 0.4227026 ]
[ 0.47641375 -0.58505976 0.61508092 0.8231577 ]
[ 1.45158902 0.30929911 0.55777524 0.28921757]
[ 0.72020757 0.30929911 0.90160929 1.49058286]
[-0.86445224 1.65083742 -1.2187007 -1.31260282]
[ 1.32969211 0.08570939 0.95891497 1.22361279]
[ 0.11072303 -0.13788033 0.27124686 0.4227026 ]
[ 0.84210448 -0.13788033 0.84430362 1.09012776]
[-0.13307079 -1.0322392 -0.12989286 -0.24472256]
[-0.74255534 -0.80864948 0.09932984 0.28921757]
[ 0.35451684 -0.13788033 0.50046957 0.28921757]
[-1.5958337 -1.70300836 -1.39061772 -1.17911778]
[ 0.96400139 -0.36147005 0.50046957 0.15573254]
[-0.37686461 -1.0322392 0.38585821 0.02224751]
[-0.62065843 1.4272477 -1.27600637 -1.31260282]
[-0.2549677 -0.13788033 0.21394119 0.15573254]
[ 1.81727975 -0.36147005 1.47466605 0.8231577 ]
[ 1.08589829 0.53288883 1.130832 1.22361279]
[-0.86445224 1.4272477 -1.27600637 -1.04563275]
[-1.10824606 -1.47941864 -0.24450422 -0.24472256]
[ 1.08589829 0.53288883 1.130832 1.75755292]
[ 1.69538284 -0.13788033 1.18813767 0.55618763]
[-1.10824606 1.20365799 -1.33331205 -1.44608785]
[ 1.08589829 0.08570939 1.07352632 1.62406789]
[-1.10824606 -0.13788033 -1.33331205 -1.31260282]
[ 1.32969211 0.08570939 0.67238659 0.4227026 ]
[ 1.93917666 -0.58505976 1.3600547 0.95664273]
[ 0.59831066 -0.36147005 1.07352632 0.8231577 ]
[-0.13307079 -0.58505976 0.21394119 0.15573254]
[ 0.84210448 -0.13788033 1.01622064 0.8231577 ]
[ 0.59831066 -1.70300836 0.38585821 0.15573254]
[ 0.72020757 -0.36147005 0.32855254 0.15573254]
[-0.2549677 -0.58505976 0.67238659 1.09012776]
[ 0.11072303 -0.13788033 0.78699794 0.8231577 ]
[-0.49876152 0.75647855 -1.16139502 -1.31260282]
[ 0.35451684 -0.58505976 0.15663551 0.15573254]
[-1.10824606 -1.25582892 0.44316389 0.68967267]
[-0.01117388 2.09801686 -1.4479234 -1.31260282]
[-0.01117388 -1.0322392 0.15663551 0.02224751]
[ 1.57348593 -0.13788033 1.24544335 1.22361279]] [[ 0.35451684 -0.58505976 0.55777524 0.02224751]
[-0.13307079 1.65083742 -1.16139502 -1.17911778]
[ 2.30486738 -1.0322392 1.8185001 1.49058286]
[ 0.23261993 -0.36147005 0.44316389 0.4227026 ]
[ 1.2077952 -0.58505976 0.61508092 0.28921757]
[-0.49876152 0.75647855 -1.27600637 -1.04563275]
[-0.2549677 -0.36147005 -0.07258719 0.15573254]
[ 1.32969211 0.08570939 0.78699794 1.49058286]
[ 0.47641375 -1.92659808 0.44316389 0.4227026 ]
[-0.01117388 -0.80864948 0.09932984 0.02224751]
[ 0.84210448 0.30929911 0.78699794 1.09012776]
[-1.23014297 -0.13788033 -1.33331205 -1.44608785]
[-0.37686461 0.98006827 -1.39061772 -1.31260282]
[-1.10824606 0.08570939 -1.27600637 -1.44608785]
[-0.86445224 1.65083742 -1.27600637 -1.17911778]
[ 0.59831066 0.53288883 0.55777524 0.55618763]
[ 0.84210448 -0.13788033 1.18813767 1.35709783]
[-0.2549677 -1.25582892 0.09932984 -0.11123753]
[-0.13307079 -0.58505976 0.44316389 0.15573254]
[ 0.72020757 -0.58505976 1.07352632 1.35709783]
[-1.35203988 0.30929911 -1.2187007 -1.31260282]
[ 0.35451684 -0.13788033 0.67238659 0.8231577 ]
[-0.98634915 0.75647855 -1.2187007 -1.04563275]
[ 0.72020757 -0.58505976 1.07352632 1.22361279]
[ 2.5486612 1.65083742 1.53197172 1.09012776]
[ 1.08589829 -0.13788033 0.84430362 1.49058286]
[ 1.08589829 -1.25582892 1.18813767 0.8231577 ]
[ 1.2077952 0.30929911 1.24544335 1.49058286]
[-1.23014297 -0.13788033 -1.33331205 -1.17911778]
[-1.23014297 0.08570939 -1.2187007 -1.31260282]]
Accuracy: 1.0
Recall: 1.0
F1 Score: 1.0
precision recall f1-score support
setosa 1.00 1.00 1.00 10
versicolor 1.00 1.00 1.00 9
virginica 1.00 1.00 1.00 11
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30