Scikit-Learn: Supported target types are: ('binary', 'multiclass'). Got 'unknown' instead

Viewed 258

I have 2 nominal values of a product (category and producer) and its price and try to identify if in any given category a producer has usually a higher price. In another word, I try to measure the impact of a brand on price. I used below Python code and couldn't manage to run and get this error:

Supported target types are: ('binary', 'multiclass'). Got 'unknown' instead.

Could you please help me to resolve this?

# Load dataset
path = "Sales.xlsx"
names = ['Category', 'Producer', 'Average_base_price']
dataset = read_excel(path, dtype={'Average_base_price':float} ,names=names)

# creating instance of labelencoder
labelencoder = LabelEncoder()

array = dataset.values

# Split-out validation dataset
X, y = array[:, :-1], array[:, -1]

X[:, 0] = labelencoder.fit_transform(X[:, 0])
X[:, 1] = labelencoder.fit_transform(X[:, 1])

X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.20, random_state=1)

# Spot Check Algorithms
models = []
models.append(('LR', LogisticRegression(solver='liblinear', multi_class='ovr')))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC(gamma='auto')))

# evaluate each model in turn
results = []
names = []

for name, model in models:
    kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)
    cv_results = cross_val_score(model, X_train, Y_train, cv=kfold, scoring='accuracy')
    results.append(cv_results)
    names.append(name)
    print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))
1 Answers

You get that error because your dependent variable is continuous and you are trying to do a stratified kfold which doesn't make sense

If it is like you said:

In another word, I try to measure the impact of a brand on price.

Then your dependent variable should be price. Your independent variables would be the nominal values. And you should do one-hot encoding instead of label encoding because these are not predictors, not labels.

Using an example dataset:

from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
import numpy as np
import pandas as pd

dataset = pd.DataFrame({'Category':np.random.choice(['A','B','C'],100),
'Producer':np.random.choice(['l','m','n'],100),
'Average_base_price':np.random.uniform(0,1,100)})

Onehot encode the predictor:

enc = OneHotEncoder(handle_unknown='ignore')
X = enc.fit_transform(dataset[['Category','Producer']])
y = dataset[['Average_base_price']]

X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.20, random_state=1)

Then fit the model, in this case you can use for example a simple linear regression:

model = LinearRegression()
cv_results = cross_val_score(model, X_train, Y_train, cv=10)
Related