Decision


# -*-coding: utf-8-*-
import numpy as np
data =np. array([[ 0.3, 5, 2, 0],
[0.4, 6, 0, 0], 
[0.5, 6.5, 1, 1],
[0.6, 6, 0, 0],
[0.7, 9, 2, 1],
[0.5, 7, 1, 0],
[0.4, 6, 0, 0],
[0.6, 8.5, 0, 1],
[0.3, 5.5, 2, 0],
[0.9, 10, 0, 1],
[1, 12, 1, 0],
[0.6, 9, 1, 0]])


Y = data[:, -1]
X = data[:, 0:-1]
from sklearn.model_selection import train_test_split
from sklearn import tree

from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
X = cancer['data']
Y = cancer['target']
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)

# 概率分布,信息熵衡量信息量的大小,也就是对随机变量不确定度的一个衡量。熵越大,不确定性越大entropy gini#比起基尼系数,信息熵对不纯度更加敏感,对不纯度的惩罚最强。但是在实际使用中,信息烟和基尼系数的效果基本相同。
# 信息熵的计算比基尼系数缓慢-些,因为基尼系数的计算不涉及对数。另外,因为信息熵对不纯度更加敏感
# 剪枝参数max_depth

clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=4)
clf.fit(x_train, y_train)
# print('特征所占的权重是:', clf.feature_importances_)
answer = clf.predict(x_test)
# print('测试数据使用模型预测对应的类是:', answer)
# print('测试数据对应的类是:', y_test)
from collections import Counter 
print(clf.score(x_train, y_train))
print(clf.score(x_test, y_test)) 
from sklearn. metrics import precision_recall_curve
from sklearn. metrics import classification_report

# 准确率与召回率!!!!!!

print(np.mean(answer==y_test))
precision, recall, thresholds = precision_recall_curve(y_train, clf. predict(x_train))
answer = clf.predict_proba(X)[:, 1] 
answer = np.where(answer<0.1,0, 1)
a =np.zeros(Y.shape)
a[Y==answer] =1
print(Counter(Y), Counter(a))
print(classification_report(Y, answer))


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值