【问题标题】:Randomly sample data from sklearn dataset从 sklearn 数据集中随机采样数据
【发布时间】:2019-10-02 21:22:33
【问题描述】:

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

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 个目标。

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

这就是我可以使用第一个特征绘制回归的全部内容

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

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

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

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

【问题讨论】:

    标签: python scikit-learn scipy


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 2012-08-09
      • 2018-06-18
      • 1970-01-01
      相关资源
      最近更新 更多