3
$\begingroup$

How do I test if the predicted values in Linear Regression model are matching with the actuals?

I tried using - Confusion matrix, but I get this error -

#==============================================================================
# Create confusion matrix to evaluate performance of data
#==============================================================================
from sklearn.metrics import confusion_matrix
confusionMatrix = confusion_matrix (dv_test, y_pred)

print(confusionMatrix)

ValueError: Can't handle mix of multiclass and continuous

When I execute the below code -

##Performing Linear Regression
from sklearn.linear_model import LinearRegression
from sklearn import model_selection
regressor=LinearRegression()
##Fit train
regressor.fit(iv_train,dv_train)
y_pred=regressor.predict(iv_test)
print('Accuracy of LR',mean_squared_error(y_pred,dv_test))

It results - Accuracy of LR 7837176694.18

Which is incorrect.

Below is my sample data set -

longitude   latitude    housing_median_age  total_rooms total_bedrooms  population  households  median_income   ocean_proximity median_house_value
-122.23 37.88   41  880 129 322 126 8.3252  NEAR BAY    452600
-122.22 37.86   21  7099    1106    2401    1138    8.3014  NEAR BAY    358500
-122.24 37.85   52  1467    190 496 177 7.2574  NEAR BAY    352100
-122.25 37.85   52  1274    235 558 219 5.6431  NEAR BAY    341300
-122.25 37.85   52  1627    280 565 259 3.8462  NEAR BAY    342200
-122.25 37.85   52  919 213 413 193 4.0368  NEAR BAY    269700
-122.25 37.84   52  2535    489 1094    514 3.6591  NEAR BAY    299200
$\endgroup$

1 Answer 1

4
$\begingroup$

There are several ways to check your Linear Regression model accuracy. Usually, you may use Root mean squared error. You may train several Linear Regression models, adding or removing features to your dataset, and see which one has the lowest RMSE - the best one in your case. Also try to normalize your data before fitting into Linear Regression model.

The confusion matrix is used to check discrete results, but Linear Regression model returns predicted result as a continuous values. That is why you get the error: your dv_test data likely is integer, but y_pred is float.

You may try using classification model if it is suitable for the problem you try to solve - depends on what you try to predict. But for regression problem it would be better to use metric mentioned above.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.