【机器学习】(四)一些数据集:forge、wave、cancer、bosten、two-moons

头文件

import numpy as np
import matplotlib.pyplot as plt
import mglearn
import pandas

from sklearn.model_selection import train_test_split

forge数据集

可以用于模拟二分类问题,数据集包含26个数据点和2个特征,两种输出。

from sklearn.model_selection import train_test_split

# 生成数据集
X, y = mglearn.datasets.make_forge() # 输入,目标
X_train, x_test, y_train, y_test = train_test_split(X, y, random_state=0)

# 数据集绘图
mglearn.discrete_scatter(X[:, 0], X[:, 1], y) # 第一个特征为x轴,第二特征为y轴,不同的点是输出y
plt.legend(['Class 0', 'Class 1'], loc=4) # 给图像加图例(文字说明),loc参数指向图例位置
plt.xlabel('First feature')
plt.ylabel('Second feature')
print('X.shape:{}'.format(X.shape))

在这里插入图片描述

wave数据集

用来测试回归算法,数据集只有一个输入特征,一个连续的目标变量/响应。

x, y = mglearn.datasets.make_wave(n_samples=40)
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0)

plt.plot(x, y, 'o') # 圆点表示
plt.ylim(-3, 3)
plt.xlabel('Feature')
plt.ylabel('Target')

在这里插入图片描述

cancer数据集

威斯康星州乳腺癌数据集,二分类问题,569个数据点,30个特征,两个目标(良性/恶性)

包含在scikit_learn中的数据集通常保存为Bunch对象
Bunch对象类似字典,并且可以用点操作符来访问对象的值。bunch.ket = bunch['key']

from sklearn.datasets import load_breast_cancer

cancer = load_breast_cancer() # Bunch对象
x_train, x_test, y_train, y_test = train_test_split(
    cancer.data, cancer.target, stratify=cancer.target, random_state=66)

print('Cancer Keys:\n{}'.format(cancer.keys()))
print('Target:\n{}'.format({n: v for n, v in 
                           zip(cancer.target_names, np.bincount(cancer.target))}))

np.bincount()=函数衡量权重(出现了多少次)
zip()函数将可迭代对象封装成列表,列表的元素为元组。

np.bincount()用法介绍

boston数据集

波士顿房价数据集,回归问题,506个数据点,13个特征。
普通bosten

from sklearn.datasets import load_boston
boston = load_boston()
print(boston.data.shape)

含有交互项的bosten:13个特征两两组合成91个特征,共有104个特征

x, y = mglearn.datasets.load_extended_boston()
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0)
lr = LinearRegression().fit(x_train, y_train)

print(x.shape)

two-moons数据集

二分类数据集,想两个月亮一样(太极)

from sklearn.datasets import make_moons

# two_moons数据集
x, y = make_moons(n_samples=100,  noise=0.25, random_state=3)
x_train, x_test, y_train, y_test = train_test_split(x, y, stratify=y, random_state=42)
  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值