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

相关文章

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

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

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig