Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I am a newbie to machine learning. I have been struggling with a problem for a few weeks now and I hope someone can help here:

I have a data set with one continuous variable and the rest are categorical. I managed to encode the categorical variables and would like to build a multi output classifier.

Here is my data set: Snapshot of my data set I have these features : A, B I would like to predict : C,D,E,F, G

The data set looks like this: A,B,C,D,E,F,G

I spent days on the documentation on multi-output classifiers on the scikitlearn and here but none of the documentation seems clear to me.

Can anyone please point me in the right direction to find some sample code on how to create the classifier and predict with some sample data?

Thank you in advance P.S: I am not using TensorFlow and would appreciate your help for sklearn.

1 Answer

0 votes
by (41.4k points)
edited by

Refer to the below code that does multi-target classification.

import numpy as np

from sklearn.ensemble import RandomForestClassifier

from sklearn.multioutput import MultiOutputClassifier

# The data from your screenshot

#  A      B   C    D    E   F    G

train_data = np.array([

  [5, 133.5, 27, 284, 638, 31, 220],

  [5, 111.9, 27, 285, 702, 36, 230],

  [5,  99.3, 25, 310, 713, 39, 227],

  [5, 102.5, 25, 311, 670, 34, 218],

  [5, 114.8, 25, 312, 685, 34, 222],

])

# These I just made up

test_data_a = np.array([

  [5, 100.0],

  [5, 105.2],

  [5, 102.7],

  [5, 103.5],

  [5, 120.3],

  [5, 132.5],

  [5, 152.5],

])

a = train_data[:, :2]

b = train_data[:, 2:]

forest = RandomForestClassifier(n_estimators=100, random_state=1)

classifier = MultiOutputClassifier(forest, n_jobs=-1)

classifier.fit(a, b)

print classifier.predict(test_data_a)

Output 

[[  25.  310.  713.   39.  227.]

 [  25.  311.  670.   34.  218.]

 [  25.  311.  670.   34.  218.]

 [  25.  311.  670.   34.  218.]

 [  25.  312.  685.   34.  222.]

 [  27.  284.  638.   31.  220.]

 [  27.  284.  638.   31.  220.]]

If you want to know more about Machine Learning then watch this video:

If you want to learn data science in-depth then enroll for best data science training.

Browse Categories

...