[Stay Sharp]决策树sklearn实践

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     tf_decision_tree
   Description :   
   Author :        Yalye
   date:          2018/12/21
-------------------------------------------------
"""


import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from pprint import pprint

# step 1: load data
data_path = "./data/"
names = ('animal_name', 'hair', 'feathers', 'eggs', 'milk',
         'airbone', 'aquatic', 'predator', 'toothed', 'backbone',
         'breathes', 'venomous', 'fins', 'legs', 'tail', 'domestic', 'catsize', 'class',)
formats = ('S1',) + (('i4',) * 17)
zoo_data = np.loadtxt(data_path + 'zoo.data', delimiter=',', dtype={'names': names,
                                                                    'formats': formats}, skiprows=0)
zoo_data_df = pd.DataFrame(zoo_data)
zoo_data_df = zoo_data_df.drop('animal_name', axis=1)
sample_number = zoo_data.shape[0]
print(sample_number)
print(zoo_data_df)

train_features = zoo_data_df.iloc[:80,:-1]
test_features = zoo_data_df.iloc[80:,:-1]
train_targets = zoo_data_df.iloc[:80,-1]
test_targets = zoo_data_df.iloc[80:,-1]

tree = DecisionTreeClassifier(criterion = 'entropy').fit(train_features,train_targets)

prediction = tree.predict(test_features)

print("The prediction accuracy is: ", tree.score(test_features,test_targets)*100,"%")

结果:

The prediction accuracy is:  80.95238095238095 %

代码

Yaley Github

参考

https://www.python-course.eu/Decision_Trees.php

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    小迈克阅读 3,067评论 1 3
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,616评论 2 45
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 6,385评论 1 9
  • 2015年6月23号是一个非常特殊的日子。对于一个高三学生的家庭来说是一个非常特殊的日子,这天是高考成绩出来...
    秋殇别恋丶阅读 356评论 0 0
  • 这是沪渝高速的起点,我就从这里出发了、一路向西。 天气预报说出发那天有雨,所以天有些灰蒙蒙的,但即便这样 它也没有...
    義默客阅读 330评论 0 0