0
$\begingroup$

Hello Data Science community,

I have a model with 1 quantitative variable (y) and 2 categorical variables. In order to work with the categorical variables I have created dummy variables (binary) for the first one this is binary by nature so I still have one variable No = 0, Yes = 1, but for the second categorical value I have 48 different inputs (locations), so I've created 47 new dummy variables in which 1 = if it is that location (ref. prnt.sc/payj7q )

But the problem I'm getting know is that my output for the coefficients, looks the same for all of the x's, like this... (prnt.sc/payio0) I'm doing something wrong here or where should I look to improve the model? Again, thank you so much for your support.

Here's the code I'm using...

import pandas as pd  
import numpy as np  
import matplotlib.pyplot as plt  
import seaborn as seabornInstance 
from sklearn.model_selection import train_test_split 
from sklearn.linear_model import LinearRegression
from sklearn import metrics
%matplotlib inline

# Main files
dataset = pd.read_csv('namaste_econ_model.csv')
dataset.shape
dataset.describe()
dataset.isnull().any()
#Dividing data into "attributes" and "labels". X variable contains all the attributes and y variable contains labels.
X = dataset[['Read?', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6' , 'x7','x8','x9','x10','x11','x12','x13','x14','x15','x16','x17','x18','x19','x20','x21','x22','x23','x24','x25','x26','x27','x28','x29','x30','x31','x32','x33','x34','x35','x36','x37','x38','x39','x40','x41','x42','x43','x44','x45','x46','x47']].values
y = dataset['Change in Profit (BP)'].values
#Place columns back on df
X_df = pd.DataFrame(X, columns = dataset[['Read?', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6' , 'x7','x8','x9','x10','x11','x12','x13','x14','x15','x16','x17','x18','x19','x20','x21','x22','x23','x24','x25','x26','x27','x28','x29','x30','x31','x32','x33','x34','x35','x36','x37','x38','x39','x40','x41','x42','x43','x44','x45','x46','x47']].columns)
#Plot 'Change in Profit (BP)'
plt.figure(figsize=(15,10))
plt.tight_layout()
seabornInstance.distplot(dataset['Change in Profit (BP)'])
X_train, X_test, y_train, y_test = train_test_split(X_df, y, test_size=0.2, random_state=0)
regressor = LinearRegression()  
#Train the algorithm
regressor.fit(X_train, y_train)
coeff_df = pd.DataFrame(regressor.coef_, X_df.columns, columns=['Coefficient'])  
coeff_df
print(regressor.coef_)

Thank you in advance for the support!

$\endgroup$
  • $\begingroup$ The common coefficient is also huge. What's the range of the dependent variable? $\endgroup$ – Ben Reiniger Sep 25 '19 at 22:37
  • $\begingroup$ Do you mean that you only have these two categorical features to predict y? And are you sure that the location has an influence on y? $\endgroup$ – Erwan Sep 26 '19 at 1:01
  • $\begingroup$ Hi @Erwan, that's correct, I only have these two categorical features to predict y, however, I'm not sure if the location has any influence, that's what I'm trying to find out, but the problem seems to be that I was using the same number of dummy variables as the levels within that categorical variable, where I should have been using n-1, to avoid multicollinearity. Any tips on how to approach this model? $\endgroup$ – Eduardo Martinez Sep 26 '19 at 15:30
  • $\begingroup$ Hi @BenReiniger, the range for the dependent variable is from -10480 to 39400. I'm still learning about linear regressions, is this a bad thing in any sense? Thanks for the support! $\endgroup$ – Eduardo Martinez Sep 26 '19 at 15:34
  • $\begingroup$ Something is wrong; did the fit warn that it hadn't converged? The prediction for any of the first few locations will all be on the order of -10^15. Try scaling the dependent variables; maybe the lapack solver underlying LinearRegression is struggling with the scale. $\endgroup$ – Ben Reiniger Sep 26 '19 at 16:18

Your Answer

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

Browse other questions tagged or ask your own question.