0
$\begingroup$

I have a mobile price classification dataset in which I have 20 features and one target variable called price_range. I need to classify mobile prices as low, medium, high, very high.

I have applied a one-hot encoding to my target variable. After that, I split the data into trainX, testX, trainy, testy. So my shape for trainX and trainy is (1600,20) and (1600,4) respectively.

Now when I try to fit trainX and trainy to logisticRegresion, i.e -> lr.fit(trainX,trainy) I am getting an

error and it says: bad input (1600,4)

So, I understood that I have to give trainy value in shape (1600,1) but by one hot encoding I have got array of 4 columns for each individual price_range as per the concept of one hot encoding.

So now I am totally confused how people use one hot encoding for target variable in practice? please help me out.

$\endgroup$
1
$\begingroup$

So now I am totally confused how people use one hot encoding for target variable in practice? please help me out.

This mostly comes down to the tool you are using. Sklearn, which I assume you are using, does not use one hot encoded target variables. So your y should be of dimension (1600, 1) where the classes are 0, 1, 2 and 3. Instead of applying one hot encoding you can use a LabelEncoder to get it on the correct format.

I suspect the reason for your confusion comes from having seen deep learning frameworks such as Tensorflow and Keras. With them you always one hot encode your target variable.

Short answer:

  • Using sklearn: Label encode
  • Using deep learning: one hot encode
$\endgroup$
  • $\begingroup$ One other thing, can I directly use one-hot encoding for the target variable with deep learning library Keras or TensorFlow? So in those libraries, it won't be a problem of y dimension (1600,4). will it accept this dimension of y? I am just asking out of curiosity.. $\endgroup$ – Sachin Yadav Nov 5 at 5:39
  • $\begingroup$ Yes, that is correct. With TF and Keras the y dimension should be (1600, 4) to work correctly. $\endgroup$ – Simon Larsson Nov 5 at 8:14
0
$\begingroup$

Logistic regression is used for a binary response (two categories). Here are some things that you may consider doing:

1) If your target variables is normally distributed use ordinary least squares (OLS) regression and manually choose cutoffs for your categories (i.e. 0-25 = 1, 26-50 = 2, 51-75 = 3, 76-100 = 4, or similar)

2) Otherwise, you should consider multinomial logistic regression. Here is a great summary of multinomial logistic regression in R: https://stats.idre.ucla.edu/r/dae/multinomial-logistic-regression/

$\endgroup$
0
$\begingroup$

You can try fitting one model for each of the 4 labels you have (so you need to do fit/predict 4 times). This way you'll have probabilities for each class separately. E.g. [0.1, 0.5, 0.45, 0.7], in this case 0.7 is the maximum so prediction would be "very high" (4th class).

$\endgroup$

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.