本文主要是介绍数据挖掘——决策分类/回归树(好苹果分类、鸢尾花数据分类、手写数字数据集分类、波士顿房价预测、泰坦尼卡号生存预测),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
极客时间——数据挖掘——决策树:
```python
#1、决策树上作业——好苹果的决策树
from sklearn import tree
import sys
import os
import graphviz
import numpy as np
#创建数据
data=np.array([[1,1],[1,0],[0,1],[0,0]])
target=np.array([1,1,0,0])
clf=tree.DecisionTreeClassifier()
clf=clf.fit(data,target)
dot_data=tree.export_graphviz(clf,out_file=None)
graph=graphviz.Source(dot_data)
print(graph.view())
#2、基于鸢尾花构造决策分类树
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
#准备数据集
iris=load_iris()
#获取特征集和分类标识
features=iris.data
labels=iris.target
#随机抽取33%的数据集作为测试集,其余为训练集
train_features,test_features,train_labels,test_labels=train_test_split(features,labels,test_size=0.33,random_state=0)
#创建CART分类树
clf=DecisionTreeClassifier(criterion="gini")
#拟合构造CART分类树
clf=clf.fit(train_features,train_labels)
#用CART分类树做预测
test_predict=clf.predict(test_features)
#将预测结果与测试集结果 作对比
score=accuracy_score(test_labels,test_predict)
print("CART分类树准确率%.4lf"%score)
#画CART分类树
dot_data=tree.export_graphviz(clf,out_file=None)
graph=graphviz.Source(dot_data)
print(graph.view())
输出:
CART分类树准确率0.9600
#3、波士顿房价预测
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.datasets import load_boston
#准备数据集
boston=load_boston()
#探索数据
print(boston.feature_names)
#获取特征集和房价
features=boston.data
prices=boston.target
#随机抽取33%的数据作为测试集,其余为训练集;
train_features,test_features,train_price,test_price=train_test_split(features,prices,test_size=0.33)
#创建CART回归树
dtr=DecisionTreeRegressor()
#拟合构造CART回归树
dtr.fit(train_features,train_price)
#预测测试集中的房价
predict_price=dtr.predict(test_features)
#测试集的结果评价
print("回归树二乘偏差均值:",mean_squared_error(test_price,predict_price))
print("回归树绝对值偏差均值:",mean_absolute_error(test_price,predict_price))dot_data=tree.export_graphviz(dtr,out_file=None)
graph=graphviz.Source(dot_data)
print(graph.view())
输出:
[‘CRIM’ ‘ZN’ ‘INDUS’ ‘CHAS’ ‘NOX’ ‘RM’ ‘AGE’ ‘DIS’ ‘RAD’ ‘TAX’ ‘PTRATIO’
‘B’ ‘LSTAT’]
回归树二乘偏差均值: 41.10365269461077
回归树绝对值偏差均值: 3.7467065868263476
#4、手写数字数据集——练习题:
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn.metrics import accuracy_score
import graphviz
# 准备手写数字数据集
digits = datasets.load_digits()
# 获取特征和标识
features = digits.data
labels = digits.target
# 选取数据集的33%为测试集,其余为训练集
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size=0.33)
# 创建CART分类树
clf = tree.DecisionTreeClassifier()
# 拟合构造CART分类树
clf.fit(train_features, train_labels)
# 预测测试集结果
test_predict = clf.predict(test_features)
# 测试集结果评价
print('CART分类树准确率:', accuracy_score(test_labels, test_predict))
# 画决策树
dot_data = tree.export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data)
graph.render('CART//CART_practice_digits')
print(graph.view())
so long ……
泰坦尼克号生存预测:
#泰坦尼克号生存预测:
import pandas as pd
#数据加载
train_data=pd.read_csv("./train.csv")
test_data=pd.read_csv("./test.csv")
#数据探索
print(train_data.info())
print("-"*30)
print(train_data.describe())
print("-"*50)
print(train_data.head())
print("-"*50)
print(train_data.tail())
#数据清洗
#使用平均年龄来填充年龄中的缺失值;数据填充
train_data["Age"].fillna(train_data["Age"].mean(),inplace=True)
test_data["Age"].fillna(test_data["Age"].mean(),inplace=True)
train_data["Embarked"].fillna("S",inplace=True)
test_data["Embarked"].fillna("S",inplace=True)
train_data['Fare'].fillna(train_data['Fare'].mean(), inplace=True)
test_data['Fare'].fillna(test_data['Fare'].mean(),inplace=True)
#特征选择:将Pclass\Sex\Age等作为特征,放到特征向量中features中;features=["Pclass","Sex","Age","SibSp","Parch","Fare","Embarked"]
train_features=train_data[features]
train_labels=train_data["Survived"]
test_features=test_data[features]#特征值中有一些字符串,这样不方便运算,需要转换成数值类型,例如性别变成0、1abs等;
#使用DictVectorizer类可以处理符号化对象;
from sklearn.feature_extraction import DictVectorizer
dvec=DictVectorizer(sparse=False)
train_features=dvec.fit_transform(train_features.to_dict(orient="record"))#他将特征向量化转换为特征值矩阵
print (dvec.feature_names_)
#决策树模型
from sklearn.tree import DecisionTreeClassifier#构造ID3决策树
clf=DecisionTreeClassifier(criterion="entropy")
#决策树训练
clf.fit(train_features,train_labels)
test_features=dvec.transform(test_features.to_dict(orient="record"))
#决策树预测
predict_labels=clf.predict(test_features)
#得到决策树准确率:
acc_decision_tree=round(clf.score(train_features,train_labels))
print(u"score准确率为%.4lf"%acc_decision_tree)import numpy as np
from sklearn.model_selection import cross_val_score
# 使用K折交叉验证 统计决策树准确率
print(u'cross_val_score准确率为 %.4lf' % np.mean(cross_val_score(clf, train_features, train_labels, cv=10)))
#可视化:
from sklearn import tree
import graphviz dot_data=tree.export_graphviz(clf,out_file=None)
graph=graphviz.Source(dot_data)
print(graph.view())
输出:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
PassengerId 891 non-null int64
Survived 891 non-null int64
Pclass 891 non-null int64
Name 891 non-null object
Sex 891 non-null object
Age 714 non-null float64
SibSp 891 non-null int64
Parch 891 non-null int64
Ticket 891 non-null object
Fare 891 non-null float64
Cabin 204 non-null object
Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.6+ KB
None
------------------------------PassengerId Survived Pclass Age SibSp \
count 891.000000 891.000000 891.000000 714.000000 891.000000
mean 446.000000 0.383838 2.308642 29.699118 0.523008
std 257.353842 0.486592 0.836071 14.526497 1.102743
min 1.000000 0.000000 1.000000 0.420000 0.000000
25% 223.500000 0.000000 2.000000 20.125000 0.000000
50% 446.000000 0.000000 3.000000 28.000000 0.000000
75% 668.500000 1.000000 3.000000 38.000000 1.000000
max 891.000000 1.000000 3.000000 80.000000 8.000000 Parch Fare
count 891.000000 891.000000
mean 0.381594 32.204208
std 0.806057 49.693429
min 0.000000 0.000000
25% 0.000000 7.910400
50% 0.000000 14.454200
75% 0.000000 31.000000
max 6.000000 512.329200
--------------------------------------------------PassengerId Survived Pclass \
0 1 0 3
1 2 1 1
2 3 1 3
3 4 1 1
4 5 0 3 Name Sex Age SibSp \
0 Braund, Mr. Owen Harris male 22.0 1
1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1
2 Heikkinen, Miss. Laina female 26.0 0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen, Mr. William Henry male 35.0 0 Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.2500 NaN S
1 0 PC 17599 71.2833 C85 C
2 0 STON/O2. 3101282 7.9250 NaN S
3 0 113803 53.1000 C123 S
4 0 373450 8.0500 NaN S
--------------------------------------------------PassengerId Survived Pclass Name \
886 887 0 2 Montvila, Rev. Juozas
887 888 1 1 Graham, Miss. Margaret Edith
888 889 0 3 Johnston, Miss. Catherine Helen "Carrie"
889 890 1 1 Behr, Mr. Karl Howell
890 891 0 3 Dooley, Mr. Patrick Sex Age SibSp Parch Ticket Fare Cabin Embarked
886 male 27.0 0 0 211536 13.00 NaN S
887 female 19.0 0 0 112053 30.00 B42 S
888 female NaN 1 2 W./C. 6607 23.45 NaN S
889 male 26.0 0 0 111369 30.00 C148 C
890 male 32.0 0 0 370376 7.75 NaN Q
['Age', 'Embarked=C', 'Embarked=Q', 'Embarked=S', 'Fare', 'Parch', 'Pclass', 'Sex=female', 'Sex=male', 'SibSp']
score准确率为1.0000
cross_val_score准确率为 0.7768
这篇关于数据挖掘——决策分类/回归树(好苹果分类、鸢尾花数据分类、手写数字数据集分类、波士顿房价预测、泰坦尼卡号生存预测)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!