I am trying to perform linear regression and I want to analyse the available features beforehand. The task is to predict the value of a house. Some of them might have a high impact on the label, others are irrelevant... This is my code which performs the analysis and actual regression:
X_train, X_test, Y_train, Y_test = train_test_split( X, Y, test_size=0.2, random_state=1)
featureScores = SelectKBest(f_regression, k=10).fit(X_train, Y_train).scores_
model = LinearRegression()
reg = model.fit(X_train, Y_train)
df = pd.DataFrame({'actual':abs(model.coef_ / np.max(model.coef_)), 'feature analysis':featureScores/np.max(featureScores)})
df.plot(kind='bar')
plt.show()
Here are 2 different charts as they change quite a bit if I change the random_state (Why? This should not happen that much I think):
Feature 0 is the index of the row, so it has no impact on the label. Feature analysis aswell as regression model ignore it, perfect. But then there is e.g. feature 12: It contains the living space of the house, which is a very high factor for the price. Feature analysis got this right, however the regression model does not really use this information.
What could I be doing wrong?