机器学习:决策树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

相关文章

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

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 使用时