2
$\begingroup$

I have a dataset which is as follows, (it's taken from an article online and I have been trying to Naive Bayesian algorithm on it)

Original Dataset

y attribute

After having done some manipulations (following the article), these are my new datasets for training and testing,

X Train

y Train

Now, it contains a multilabel and I have been asked to look at Multioutput classification for the problem. I have been trying to understand this classification and tried to implement it myself too, but I couldn't get it to done. First of all, I tried following this sample code given on the website,

from sklearn.datasets import make_classification
from sklearn.multioutput import MultiOutputClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.utils import shuffle
import numpy as np
X, y1 = make_classification(n_samples=10, n_features=100, n_informative=30, n_classes=3, random_state=1)
y2 = shuffle(y1, random_state=1)
y3 = shuffle(y1, random_state=2)
Y = np.vstack((y1, y2, y3)).T
n_samples, n_features = X.shape # 10,100
n_outputs = Y.shape[1] # 3
n_classes = 3
forest = RandomForestClassifier(n_estimators=100, random_state=1)
multi_target_forest = MultiOutputClassifier(forest, n_jobs=-1)
multi_target_forest.fit(X, Y).predict(X)

But, since, I am new to all this, I didn't understand anything at all.. I didn't understand why he did the make_classification call, and then shuffled the data and etc. I tried to implement it on my y_train variable and then placed it in my model.fit for Naive-Baysen algorithm,

from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
Yt = np.vstack(y_train).T
n_samples, n_features = X_train.shape # 10,100
n_outputs = Yt.shape[1] # 3
n_classes = 3
forest = RandomForestClassifier(n_estimators=100, random_state=1)
multi_target_forest = MultiOutputClassifier(forest, n_jobs=-1)
model.fit(X_train, multi_target_forest)

But it gave the same error which I was receiving previously, which meant that I didn't do the multioutputclassification properly,

ValueError: y should be a 1d array, got an array of shape () instead.

Can anyone help me in telling how to actually implement this classification, so that the Y variable can be used for the Naive Baysen?

$\endgroup$
1
2
$\begingroup$

The examples's make_classification, shuffles, and stack are just to generate a multilabel dataset. You've already processed to y_train and don't need any of that.

Then, you want a base estimator to be Naive Bayes, not a random forest as in the example. And all sklearn models should be fit as model_instance.fit(X, y, sometimes_optional_parameters); the error being raised is because you passed a model object instead of y (and also are using the wrong model object). So the following should do what you want:

nb = GaussianNB() 
multi_target_nb = MultiOutputClassifier(nb, n_jobs=-1)
multi_target_nb.fit(X_train, y_train)
$\endgroup$
1
  • $\begingroup$ Thank you so much!!! I am new to this so I am not familiar with much of the syntax, thank you for the help. Greatly appreciated. $\endgroup$
    – Sakib Khan
    Dec 29 '20 at 4:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.