从 sklearn 数据集中随机采样数据 [英] Randomly sample data from sklearn dataset

查看:109
本文介绍了从 sklearn 数据集中随机采样数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自 sklearn 的对象,看起来像这样.

I have a bunches object from sklearn that looks like this.

from sklearn.datasets import load_boston
import scipy
import numpy as np

boston = load_boston()
n_samples = boston.data.shape[0]

print(boston.keys())
dict_keys(['data', 'target', 'feature_names', 'DESCR', 'filename'])

我想从数据和目标键中随机抽取 30 个样本和 30 个目标.

I want to randomly sample 30 samples and 30 targets from the data and target keys.

    X, y = [np.array([boston.data[i]]), np.array([boston.target[i]) for i in np.random(choice(n_samples, 30)])
                                                            ^
SyntaxError: invalid syntax

这就是我可以使用第一个特征绘制回归图

This is all so I can plot a regression using the first feature

slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(X[:][0], y)
regression = intercept + slope*X[:][0]

boston.databoston.target 都是 numpy 数组.我怎样才能做到这一点?

boston.data and boston.target are both numpy arrays. How can I accomplish this?

print(type(boston.data))
<class 'numpy.ndarray'>

print(type(boston.target))
<class 'numpy.ndarray'>

推荐答案

您有几个拼写错误(例如,它是 random.choice)并且您还覆盖了数组.这应该有效:

You have a couple of typos (e.g. it's random.choice) and you're also overwriting your arrays. This should work:

x = []
y = []
for i in np.random.choice(n_samples, 30):
    x.append(boston.data[i])
    y.append(boston.target[i])

这篇关于从 sklearn 数据集中随机采样数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆