首页 > 其他分享 >机器学习笔记(1) -- 决策树

机器学习笔记(1) -- 决策树

时间:2022-12-01 17:15:09浏览次数:44  
标签:rating no -- age 笔记 credit income yes 决策树

csv的内容为:

 

 

运行的代码为:

from sklearn.feature_extraction import DictVectorizer
import csv
from sklearn import tree,preprocessing
from six import StringIO

allElectronicsData = open(r'/Users/zhangsanfeng/Documents/jupyterNotebook/AllElectronics.csv')
reader = csv.reader(allElectronicsData) 
headers = next(reader)  # 获取表头

print(headers)

featureList = []
labelList = []
for row in reader:
    labelList.append(row[len(row)-1])  # 取该行最后一个值
    rowDict = {}
    for i in range(1,len(row)-1):  # 从第二个值开始轮询该行,到倒数第二个值结束
        rowDict[headers[i]] = row[i]  # 用headers里面的值和该行的值组装成一个键值对
    featureList.append(rowDict)  # 将键值对拼接到列表里面

print(featureList)
print(labelList)

vec = DictVectorizer()
dummyX = vec.fit_transform(featureList).toarray()  # 将键值对中的值转换为坐标值
print("dummyX: " + str(dummyX))  # 打印转换后的结果
print(vec.get_feature_names())  # 打印每一项标识的含义
print("labelList: " + str(labelList))  # labelList尚未转换

lb = preprocessing.LabelBinarizer()
dummyY = lb.fit_transform(labelList)  # 转换labelList
print("dummyY: " + str(dummyY))

# 使用决策树进行分类
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(dummyX, dummyY)
print("clf: " + str(clf))

# 可视化模型,将结果写入到文件
with open("/Users/zhangsanfeng/Documents/jupyterNotebook/allElectronicInformationGainOri.dot", 'w') as f:
    f = tree.export_graphviz(clf, feature_names=vec.get_feature_names(), out_file=f)
oneRowX = dummyX[0, :]
print("oneRowX: " + str(oneRowX))


newRowX = oneRowX
newRowX[0] = 1
newRowX[2] = 0
newRowX = newRowX.reshape(1, -1)  # 这里需要将1维数组转换为2维数组,不然后面会报错

print("newRowX: " + str(newRowX))

predictedY = clf.predict(newRowX)  # 验证测试结果
print("predictedY: " + str(predictedY))

输出结果为:

['RID', 'age', 'income', 'student', 'credit_rating', 'class_buys_computer']
[{'age': 'youth', 'income': 'high', 'student': 'no', 'credit_rating': 'fair'}, {'age': 'youth', 'income': 'high', 'student': 'no', 'credit_rating': 'excellent'}, {'age': 'middle_aged', 'income': 'high', 'student': 'no', 'credit_rating': 'fair'}, {'age': 'senior', 'income': 'medium', 'student': 'no', 'credit_rating': 'fair'}, {'age': 'senior', 'income': 'low', 'student': 'yes', 'credit_rating': 'fair'}, {'age': 'senior', 'income': 'low', 'student': 'yes', 'credit_rating': 'excellent'}, {'age': 'middle_aged', 'income': 'low', 'student': 'yes', 'credit_rating': 'excellent'}, {'age': 'youth', 'income': 'medium', 'student': 'no', 'credit_rating': 'fair'}, {'age': 'youth', 'income': 'low', 'student': 'yes', 'credit_rating': 'fair'}, {'age': 'senior', 'income': 'medium', 'student': 'yes', 'credit_rating': 'fair'}, {'age': 'youth', 'income': 'medium', 'student': 'yes', 'credit_rating': 'excellent'}, {'age': 'middle_aged', 'income': 'medium', 'student': 'no', 'credit_rating': 'excellent'}, {'age': 'middle_aged', 'income': 'high', 'student': 'yes', 'credit_rating': 'fair'}, {'age': 'senior', 'income': 'medium', 'student': 'no', 'credit_rating': 'excellent'}]
['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']
dummyX: [[0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]
 [0. 0. 1. 1. 0. 1. 0. 0. 1. 0.]
 [1. 0. 0. 0. 1. 1. 0. 0. 1. 0.]
 [0. 1. 0. 0. 1. 0. 0. 1. 1. 0.]
 [0. 1. 0. 0. 1. 0. 1. 0. 0. 1.]
 [0. 1. 0. 1. 0. 0. 1. 0. 0. 1.]
 [1. 0. 0. 1. 0. 0. 1. 0. 0. 1.]
 [0. 0. 1. 0. 1. 0. 0. 1. 1. 0.]
 [0. 0. 1. 0. 1. 0. 1. 0. 0. 1.]
 [0. 1. 0. 0. 1. 0. 0. 1. 0. 1.]
 [0. 0. 1. 1. 0. 0. 0. 1. 0. 1.]
 [1. 0. 0. 1. 0. 0. 0. 1. 1. 0.]
 [1. 0. 0. 0. 1. 1. 0. 0. 0. 1.]
 [0. 1. 0. 1. 0. 0. 0. 1. 1. 0.]]
['age=middle_aged', 'age=senior', 'age=youth', 'credit_rating=excellent', 'credit_rating=fair', 'income=high', 'income=low', 'income=medium', 'student=no', 'student=yes']
labelList: ['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']
dummyY: [[0]
 [0]
 [1]
 [1]
 [1]
 [0]
 [1]
 [0]
 [1]
 [1]
 [1]
 [1]
 [1]
 [0]]
clf: DecisionTreeClassifier(criterion='entropy')
oneRowX: [0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]
newRowX: [[1. 0. 0. 0. 1. 1. 0. 0. 1. 0.]]
predictedY: [1]

  

标签:rating,no,--,age,笔记,credit,income,yes,决策树
From: https://www.cnblogs.com/tortoise512/p/16942008.html

相关文章

  • Microsoft Excel 文件处理之xlwt库(Python)
    MicrosoftExcel文件处理之xlwt库(Python)前言博主想将格式化数据:csv、json、xml转换成excel表格,且利用C语言实现。查看了很多资料,没有找到excel操作库,就放弃了。虽然C语......
  • JSON端口操作实例
    JSON端口可直接实现在JSON和XML之间进行转换。端口会自动检测输入文件是JSON还是XML,然后将文件在两种格式间相互转换。该端口较多的是运用在API接口调用集成方案......
  • 中文乱码怎么办
    ①settings-->editor  -->codestyle--> fileencodings中IEDEncoding和projectEncodingdefultencodingforpropertiesfiles三个设置为utf-8 ......
  • NSMutableAttributedString
    1、Key值介绍1.1详细介绍NSFontAttributeName字体:该属性所对应的值是一个UIFont对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-pointHelvet......
  • Python学习(三):基本的数据结构——列表及常用方法
    1.列表的创建:list或者使用[];a='dawt'list(a)['d','a','w','t']a=['d','a','w','t']a['d','a','w','t']注意:使用list可以将其他类......
  • 什么是Mbps、Kbps、bps、KB、MB及其换算和区别
    Mbps即Milionbitprosecond(百万位每秒);Kbps即Kilobitprosecond(千位每秒);bps即bitprosecond(位每秒);速度单位,bit即比特,通常用b(小写)表示,指一位二进制位,M......
  • 纷享销客2022新增长系列之《高科技行业橙皮书》重磅发布
     二十大报告进一步提出建设数字中国,加快发展数字经济。这意味着,对于各行业而言,充分运用数字化技术推动业务变革、效率变革、流程变革,是各行各业发展的必经之路。高科技......
  • Ansible入门
    Ansible入门https://www.jianshu.com/p/c82737b5485chttps://blog.csdn.net/pushiqiang/article/details/78126063......
  • Spring Boot中@Import三种使用方式
     转载于:公众号:后端元宇宙需要注意的是:ImportSelector、ImportBeanDefinitionRegistrar这两个接口都必须依赖于@Import一起使用,而@Import可以单独使用。@Import......
  • Java常见面试题及答案整理
    Java常见面试题及答案,每道都是认真筛选出的高频面试题,助力大家能找到满意的工作!下载链接:全部面试题及答案PDF其他互联网大厂面试题1:阿里巴巴Java面试题2:阿里云Java面试......