机器学习:决策树cart算法

2024-04-03 11:48

本文主要是介绍机器学习:决策树cart算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 代码如下:

from sklearn.feature_extraction import DictVectorizer
from sklearn import preprocessing
import csvDtree = open(r'西瓜数据集3.0.csv', 'r')
reader = csv.reader(Dtree)"""
色泽 1-3代表 浅白 青绿 乌黑 
根蒂 1-3代表 稍蜷 蜷缩 硬挺 
敲声 1-3代表 清脆 浊响 沉闷 
纹理 1-3代表 清晰 稍糊 模糊 
脐部 1-3代表 平坦 稍凹 凹陷 
好瓜 1代表 是 0 代表 不是
"""# 获取第一行数据
headers = reader.__next__()
print(headers)
# 特征和标签列表
featureList = []
featureList2 = []
labelList = []for row in reader:labelList.append(row[-1])rowDict = {}rowDict2 = {}for i in range(1, len(row)-3):rowDict[headers[i]] = row[i]for j in range(7, 9):rowDict2[headers[j]] = row[j]featureList.append(rowDict)featureList2.append(rowDict2)
x2 = []
for i in range(17):x_data2 = featureList2[i].values()x2.append(x_data2)print(x2)
print(featureList)
print(featureList2)
# 将特征列表转换为01表示
vec = DictVectorizer()
x_data = vec.fit_transform(featureList).toarray()
print("x_data: " + str(x_data))# 将标签列表转换为01表示
lb = preprocessing.LabelBinarizer()
y_data = lb.fit_transform(labelList)
print("y_data: " + str(y_data))

西瓜数据集3.0请看前文https://blog.csdn.net/tianhai12/article/details/120905622?spm=1001.2014.3001.5501

最终打印结果:

['number', 'colour and lustre', 'root and base', 'Knock', 'venation', 'umbilical region', 'touch', 'density', 'sugar content', 'good']
[dict_values(['0.697', '0.46']), dict_values(['0.744', '0.376']), dict_values(['0.634', '0.264']), dict_values(['0.608', '0.318']), dict_values(['0.556', '0.215']), dict_values(['0.403', '0.237']), dict_values(['0.481', '0.149']), dict_values(['0.437', '0.211']), dict_values(['0.666', '0.091']), dict_values(['0.243', '0.267']), dict_values(['0.245', '0.057']), dict_values(['0.343', '0.099']), dict_values(['0.639', '0.161']), dict_values(['0.657', '0.198']), dict_values(['0.36', '0.37']), dict_values(['0.593', '0.042']), dict_values(['0.719', '0.103'])]
[{'colour and lustre': '2', 'root and base': '2', 'Knock': '2', 'venation': '1', 'umbilical region': '3', 'touch': '1'}, {'colour and lustre': '3', 'root and base': '2', 'Knock': '3', 'venation': '1', 'umbilical region': '3', 'touch': '1'}, {'colour and lustre': '3', 'root and base': '2', 'Knock': '2', 'venation': '1', 'umbilical region': '3', 'touch': '1'}, {'colour and lustre': '2', 'root and base': '2', 'Knock': '3', 'venation': '1', 'umbilical region': '3', 'touch': '1'}, {'colour and lustre': '1', 'root and base': '2', 'Knock': '2', 'venation': '1', 'umbilical region': '3', 'touch': '1'}, {'colour and lustre': '2', 'root and base': '1', 'Knock': '2', 'venation': '1', 'umbilical region': '2', 'touch': '2'}, {'colour and lustre': '3', 'root and base': '1', 'Knock': '2', 'venation': '2', 'umbilical region': '2', 'touch': '2'}, {'colour and lustre': '3', 'root and base': '1', 'Knock': '2', 'venation': '1', 'umbilical region': '2', 'touch': '1'}, {'colour and lustre': '3', 'root and base': '1', 'Knock': '3', 'venation': '2', 'umbilical region': '2', 'touch': '1'}, {'colour and lustre': '2', 'root and base': '3', 'Knock': '1', 'venation': '1', 'umbilical region': '1', 'touch': '2'}, {'colour and lustre': '1', 'root and base': '3', 'Knock': '1', 'venation': '3', 'umbilical region': '1', 'touch': '1'}, {'colour and lustre': '1', 'root and base': '2', 'Knock': '2', 'venation': '3', 'umbilical region': '1', 'touch': '2'}, {'colour and lustre': '2', 'root and base': '1', 'Knock': '2', 'venation': '2', 'umbilical region': '3', 'touch': '1'}, {'colour and lustre': '1', 'root and base': '1', 'Knock': '3', 'venation': '2', 'umbilical region': '3', 'touch': '1'}, {'colour and lustre': '3', 'root and base': '1', 'Knock': '2', 'venation': '1', 'umbilical region': '2', 'touch': '2'}, {'colour and lustre': '1', 'root and base': '2', 'Knock': '2', 'venation': '3', 'umbilical region': '1', 'touch': '1'}, {'colour and lustre': '2', 'root and base': '2', 'Knock': '3', 'venation': '2', 'umbilical region': '2', 'touch': '1'}]
[{'density': '0.697', 'sugar content': '0.46'}, {'density': '0.744', 'sugar content': '0.376'}, {'density': '0.634', 'sugar content': '0.264'}, {'density': '0.608', 'sugar content': '0.318'}, {'density': '0.556', 'sugar content': '0.215'}, {'density': '0.403', 'sugar content': '0.237'}, {'density': '0.481', 'sugar content': '0.149'}, {'density': '0.437', 'sugar content': '0.211'}, {'density': '0.666', 'sugar content': '0.091'}, {'density': '0.243', 'sugar content': '0.267'}, {'density': '0.245', 'sugar content': '0.057'}, {'density': '0.343', 'sugar content': '0.099'}, {'density': '0.639', 'sugar content': '0.161'}, {'density': '0.657', 'sugar content': '0.198'}, {'density': '0.36', 'sugar content': '0.37'}, {'density': '0.593', 'sugar content': '0.042'}, {'density': '0.719', 'sugar content': '0.103'}]
x_data: [[0. 1. 0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 0. 1. 1. 0. 0.]
 [0. 0. 1. 0. 0. 1. 0. 1. 0. 1. 0. 0. 0. 1. 1. 0. 0.]
 [0. 1. 0. 0. 0. 1. 0. 1. 0. 1. 0. 0. 0. 1. 1. 0. 0.]
 [0. 0. 1. 0. 1. 0. 0. 1. 0. 1. 0. 0. 0. 1. 1. 0. 0.]
 [0. 1. 0. 1. 0. 0. 0. 1. 0. 1. 0. 0. 0. 1. 1. 0. 0.]
 [0. 1. 0. 0. 1. 0. 1. 0. 0. 0. 1. 0. 1. 0. 1. 0. 0.]
 [0. 1. 0. 0. 0. 1. 1. 0. 0. 0. 1. 0. 1. 0. 0. 1. 0.]
 [0. 1. 0. 0. 0. 1. 1. 0. 0. 1. 0. 0. 1. 0. 1. 0. 0.]
 [0. 0. 1. 0. 0. 1. 1. 0. 0. 1. 0. 0. 1. 0. 0. 1. 0.]
 [1. 0. 0. 0. 1. 0. 0. 0. 1. 0. 1. 1. 0. 0. 1. 0. 0.]
 [1. 0. 0. 1. 0. 0. 0. 0. 1. 1. 0. 1. 0. 0. 0. 0. 1.]
 [0. 1. 0. 1. 0. 0. 0. 1. 0. 0. 1. 1. 0. 0. 0. 0. 1.]
 [0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 1. 0. 1. 0.]
 [0. 0. 1. 1. 0. 0. 1. 0. 0. 1. 0. 0. 0. 1. 0. 1. 0.]
 [0. 1. 0. 0. 0. 1. 1. 0. 0. 0. 1. 0. 1. 0. 1. 0. 0.]
 [0. 1. 0. 1. 0. 0. 0. 1. 0. 1. 0. 1. 0. 0. 0. 0. 1.]
 [0. 0. 1. 0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0. 1. 0.]]
y_data: [[1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]]

进程已结束,退出代码为 0
 

这篇关于机器学习:决策树cart算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/872834

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1