通过使用sklearn决策树,简单练习案例分析


# 根据电影中的类型,票房,产地,预测去不去看电影
import csv
from sklearn.feature_extraction import DictVectorizer
from sklearn import preprocessing

from sklearn import tree

# 导入数据集,
film_data = open('film.csv', 'rt')
reader = csv.reader(film_data)

# 表头数据
headers = next(reader)
# 打印一下数据集的头部
print(headers)

# 预测数据列表
feature_list = []
# 结果集列表
result_list = []
# 便利打印数据,组装成新的数据
for row in reader:
    # print(row[1:-1])
    # print(headers[1:-1])
    result_list.append(row[-1])
    feature_list.append(dict(zip(headers[1:-1], row[1:-1])))
print(result_list, feature_list)
# ['yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no']
# [{'type': 'anime', 'country': 'Janan', 'gross': 'low'}, {'type': 'science', 'country': 'America', 'gross': 'low'}, {'type': 'anime', 'country': 'America', 'gross': 'low'}, {'type': 'action', 'country': 'America', 'gross': 'high'}, {'type': 'action', 'country': 'China', 'gross': 'high'}, {'type': 'anime', 'country': 'China', 'gross': 'low'}, {'type': 'science', 'country': 'France', 'gross': 'low'}, {'type': 'action', 'country': 'China', 'gross': 'low'}]

# 调用sklearn 自带的特征提取类
vec = DictVectorizer()
# 传入训练数据模型
dummyX = vec.fit_transform(feature_list).toarray()
"生成的这个二维数组 是根据传入数据特征值进行分类的,如果特征值比比较多的话,二维数组会比较大,此二维数组前四位是代表国家,中间两位票房,最后三位数据代表电影类型,测试数据也是一样"
"""
[[0. 0. 0. 1. 0. 1. 0. 1. 0.]
 [1. 0. 0. 0. 0. 1. 0. 0. 1.]
 [1. 0. 0. 0. 0. 1. 0. 1. 0.]
 [1. 0. 0. 0. 1. 0. 1. 0. 0.]
 [0. 1. 0. 0. 1. 0. 1. 0. 0.]
 [0. 1. 0. 0. 0. 1. 0. 1. 0.]
 [0. 0. 1. 0. 0. 1. 0. 0. 1.]
 [0. 1. 0. 0. 0. 1. 1. 0. 0.]]
"""
#标签二值化
dummyY = preprocessing.LabelBinarizer().fit_transform(result_list)
"""
[[1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [0]
 [0]]
"""
print(dummyX)
print(dummyY)
# 调用sklearn 的 决策树 训练模型
clf = tree.DecisionTreeClassifier(criterion='entropy', random_state=0)
clf = clf.fit(dummyX, dummyY)
# print('clf:' + str(clf))



A = ([[0, 0, 0, 1, 0, 1, 0, 1, 0]])
B = ([[0, 0, 1, 0, 0, 1, 0, 1, 0]])
C = ([[1, 0, 0, 0, 1, 0, 1, 0, 0]])
# 传入数据 验证训练模型
predict_result = clf.predict(A)
print('预测结果' + str(predict_result))



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值