此篇主要对比下不同算法(KNN、Logistic Regression、SVM)的分类结果。
1.代码:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
data_file = './data/Iris.csv'
SPECIES_LABEL_DICT = {
'Iris-setosa': 0, # 山鸢尾
'Iris-versicolor': 1, # 变色鸢尾
'Iris-virginica': 2 # 维吉尼亚鸢尾
}
# 使用的特征列
FEAT_COLS = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']
def main():
"""
主函数
"""
# 读取数据
iris_data = pd.read_csv(data_file)
iris_data['Label'] = iris_data['Species'].map(SPECIES_LABEL_DICT)
# 获取数据集特征
X = iris_data[FEAT_COLS].values
# 获取数据集标签
y = iris_data['Label'].values
# 分割数据集
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=1/3,random_state=10)
# 模型字典
model_dict = {'KNN':KNeighborsClassifier(n_neighbors=5), # KNN
'Logistic Regression':LogisticRegression(C=100), # 逻辑回归
'SVM':SVC(C=100)} # 支持向量机
for model_name,model in model_dict.items():
# 训练模型
model.fit(X_train,y_train)
# 验证模型
accuracy = model.score(X_test,y_test)
print('{}模型的预测准确率为{:.2f}%'.format(model_name,accuracy*100))
if __name__ == '__main__':
main()
2.输出结果:
KNN模型的预测准确率为96.00%
Logistic Regression模型的预测准确率为96.00%
SVM模型的预测准确率为98.00%