本文主要是介绍机器学习:决策树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. 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