How Do I Create My Own Datasets To Work With Sklearn

Viewed 322

I have found a piece of sklearn code that I am finding relatively straightforward to run the example Iris Dataset, but how do I create my own dataset similar to this?

iris.data - contains the data measurements of three types of flower
iris.target - contains the labels of three types of flower

e.g. rather than analysing the three types of flower in the Iris Dataset, I would like to make my own datasets that follow this format and that I can pass through the code.

example_sports.data - contains the data measurements of three types of sports players
example_sports.target - contains the labels of three types of sport

from sklearn.datasets import load_iris #load inbuilt dataset from sklearn
iris = load_iris() #assign variable name iris to inbuilt dataset

iris.data # contains the four numeric variables, sepal length, sepal width, petal length, petal width

print(iris.data) #printing the measurements of the Iris Dataset below


iris.target # relates to the different species shown as 0,1,2 for the three different   
            # species of Iris, a categorical variable, basically a label


print(iris.target)

The full code can be found at https://www.youtube.com/watch?v=asW8tp1qiFQ

2 Answers

sklearn datasets are stored in Bunch which is basically just a type of dict. Sklearn data and targets are basically just NumPy arrays and can be fed into the fit() method of the sklearn estimator you are interested in. But if you want to make your own data as a Bunch, you can do something like the following:

from sklearn.utils import Bunch
import numpy as np

example_sports = Bunch(data = np.array([[1.0,1.2,2.1],[3.0,2.3,1.0],[4.5,3.4,0.5]]), target = np.array([3,2,1]))
print(example_sports.data)
print(example_sports.target)

Naturally, you can read your own custom lists into the data and target entries of the Bunch. Pandas is a good tool if you have the data in Excel/CSV files.

Try using type() command whenever you are stuck. In this case it shows you that it is a Bunch object. Then you can search documentations of that class on the web and understand how to use them.

The following will help you.

from sklearn.utils import Bunch
b = Bunch(a=1, b="textt", c = pd.Series(np.arange(5)), d = np.asarray([0, 8, 9]))
b.c
Related