Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'ValueError: shapes not aligned' on SciKit Linear Regression

Quite new to SciKit and linear algebra/machine learning with Python in general, so I can't seem to solve the following:

I have a training set and a test set of data, containing both continuous and discrete/categorical values. The CSV files are loaded into Pandas DataFrames and match in shape, being (1460,81) and (1459,81). However, after using Pandas' get_dummies, the shapes of the DataFrames change to (1460, 306) and (1459, 294). So, when I do linear regression with the SciKit Linear Regression module, it builds a model for 306 variables and it tries to predict one with only 294 with it. This then, naturally, leads to the following error:

ValueError: shapes (1459,294) and (306,1) not aligned: 294 (dim 1) != 306 (dim 0)

How could I tackle such a problem? Could I somehow reshape the (1459, 294) to match the other one?

Thanks and I hope I've made myself clear :)

like image 688
Koen Avatar asked Dec 21 '16 20:12

Koen


People also ask

What is the value error when using the linearregression model?

Predicting the test data with LinearRegression model gives ValueError: shapes (8523,1606) and (1605,) not aligned: 1606 (dim 1) != 1605 (dim 0) Hot Network Questions A question on table formatting Why does Tritone sound good in Lydian scale?

What happens when fit_intercept is set to false?

Whether to calculate the intercept for this model. If set to False, no intercept will be used in calculations (i.e. data is expected to be centered). This parameter is ignored when fit_intercept is set to False.

What is ridge regression?

Names of features seen during fit. Defined only when X has feature names that are all strings. New in version 1.0. Ridge regression addresses some of the problems of Ordinary Least Squares by imposing a penalty on the size of the coefficients with l2 regularization.


1 Answers

This is an extremely common problem when dealing with categorical data. There are differing opinions on how to best handle this.

One possible approach is to apply a function to categorical features that limits the set of possible options. For example, if your feature contained the letters of the alphabet, you could encode features for A, B, C, D, and 'Other/Unknown'. In this way, you could apply the same function at test time and abstract from the issue. A clear downside, of course, is that by reducing the feature space you may lose meaningful information.

Another approach is to build a model on your training data, with whichever dummies are naturally created, and treat that as the baseline for your model. When you predict with the model at test time, you transform your test data in the same way your training data is transformed. For example, if your training set had the letters of the alphabet in a feature, and the same feature in the test set contained a value of 'AA', you would ignore that in making a prediction. This is the reverse of your current situation, but the premise is the same. You need to create the missing features on the fly. This approach also has downsides, of course.

The second approach is what you mention in your question, so I'll go through it with pandas.

By using get_dummies you're encoding the categorical features into multiple one-hot encoded features. What you could do is force your test data to match your training data by using reindex, like this:

test_encoded = pd.get_dummies(test_data, columns=['your columns'])
test_encoded_for_model = test_encoded.reindex(columns = training_encoded.columns, 
    fill_value=0)

This will encode the test data in the same way as your training data, filling in 0 for dummy features that weren't created by encoding the test data but were created in during the training process.

You could just wrap this into a function, and apply it to your test data on the fly. You don't need the encoded training data in memory (which I access with training_encoded.columns) if you create an array or list of the column names.

like image 189
Nick Becker Avatar answered Oct 31 '22 18:10

Nick Becker