创建2分类数据集

创建2分类数据集 并进行简单决策树分类

代码示例:

import numpy as np
from pandas import DataFrame
import matplotlib.pyplot as plt
from scipy.stats import norm
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split

# 以y1 = x**2 曲线为分隔线进行分类
x = np.linspace(0,1,100)
z = norm.rvs(loc=0, size=100, scale=1) 
y1 = x**2
y = x**2 + z 
df = DataFrame({"x":x,"y":y,"class":z})
df["class"][df["class"]>=0] = 1
df["class"][df["class"]<0] = 0
xx = df[["x","y"]]
yy = df["class"]

# 分割训练与测试集
X_train,X_test,y_train,y_test = train_test_split(xx,yy,test_size=0.3)

# 选用决策树进行学习及预测,并画图
cf = DecisionTreeClassifier(max_leaf_nodes=10)
cf.fit(X_train,y_train)
xxx = DataFrame(X_test)['x']
yyy = DataFrame(X_test)['y']
zzz = cf.predict(X_test)
print cf.score(X_test,y_test)
plt.plot(x,y1)
plt.scatter(xxx,yyy,c=zzz)
plt.show()

分割效果图:
在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值