本文介绍了如何在scikit-learn中创建自己的数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自己的数据集,并在scikit-learn中使用它. Scikit-learn有一些数据集,例如"The Boston Housing Dataset"(.csv),用户可以通过以下方式使用它:

I want to create my own datasets, and use it in scikit-learn. Scikit-learn has some datasets like 'The Boston Housing Dataset' (.csv), user can use it by:

from sklearn import datasets
boston = datasets.load_boston()

和下面的代码可以获取此数据集的datatarget:

and codes below can get the data and target of this dataset:

X = boston.data
y = boston.target

问题是如何创建自己的数据集并可以这种方式使用?任何答案表示赞赏,谢谢!

The question is how to create my own dataset and can be used in that way?Any answers is appreciated, Thanks!

推荐答案

这是实现目标的一种快速而肮脏的方法:

Here's a quick and dirty way to achieve what you intend:

import numpy as np
import csv
from sklearn.datasets.base import Bunch

def load_my_fancy_dataset():
    with open('my_fancy_dataset.csv') as csv_file:
        data_file = csv.reader(csv_file)
        temp = next(data_file)
        n_samples = int(temp[0])
        n_features = int(temp[1])
        data = np.empty((n_samples, n_features))
        target = np.empty((n_samples,), dtype=np.int)

        for i, sample in enumerate(data_file):
            data[i] = np.asarray(sample[:-1], dtype=np.float64)
            target[i] = np.asarray(sample[-1], dtype=np.int)

    return Bunch(data=data, target=target)

my_fancy_dataset.csv

5,3,first_feat,second_feat,third_feat
5.9,1203,0.69,2
7.2,902,0.52,0
6.3,143,0.44,1
-2.6,291,0.15,1
1.8,486,0.37,0

演示

In [12]: import my_datasets

In [13]: mfd = my_datasets.load_my_fancy_dataset()

In [14]: X = mfd.data

In [15]: y = mfd.target

In [16]: X
Out[16]:
array([[  5.90000000e+00,   1.20300000e+03,   6.90000000e-01],
       [  7.20000000e+00,   9.02000000e+02,   5.20000000e-01],
       [  6.30000000e+00,   1.43000000e+02,   4.40000000e-01],
       [ -2.60000000e+00,   2.91000000e+02,   1.50000000e-01],
       [  1.80000000e+00,   4.86000000e+02,   3.70000000e-01]])

In [17]: y
Out[17]: array([2, 0, 1, 1, 0])

这篇关于如何在scikit-learn中创建自己的数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:02