一、数据处理
1.1数据进行可视化
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
iris_data = pd.read_csv('iris.csv', na_values=['NA'])
iris_data.head(5).append(iris_data.tail())
iris_data.describe()
sns.pairplot(iris_data.dropna(),hue="class")
1.2检查,清理数据
处理异常值,观察数据,可以看到有个别数据突出
iris_data = iris_data.loc[(iris_data['class'] != 'setosa') | (iris_data['sepal_width_cm'] >= 2.5)]
二、用scikit-learn来预测数据
2.1选出特征(输入变量)和标记(输出变量)
iris_data_clean = pd.read_csv('iris-data-clean.csv')
all_inputs = iris_data_clean[['sepal_length_cm', 'sepal_width_cm',
'petal_length_cm', 'petal_width_cm']].values
all_classes = iris_data_clean['class'].values
2.2划分训练集和数据集
(training_inputs, test_inputs, training_classes, test_classes) = train_test_split(all_inputs, all_classes, train_size=0.75, random_state=1)
2.3利用决策树模型进行学习
decision_tree_classifier = DecisionTreeClassifier()
decision_tree_classifier.fit(training_inputs, training_classes)
print(decision_tree_classifier.score(test_inputs, test_classes))
plt.show()
2.4结果
|