<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Linear Regression
The dataset USA_Housing.csv contains the following columns
Step1:  Check out the Data
Step2:  EDA-Exploratory Data Analysis
Step3:  Training a Linear Regression Model
Splitting the data into X and y arrays. we are dropping the address column as it doesn't contains numerical values.We are using X features to predict y i.e. Price of the houses.
Step4:  Train Test Split
We will train out model on the training set and then use the test set to evaluate the model.
Step5:  Creating and Training the Model
Step6:  Model Evaluation
Let's evaluate the model by checking out it's coefficients and how we can interpret them.
Step7:  Predictions from our Model
Let's grab predictions off our test set and see how well it did!
Step8:  Here we can notice that y_test and predictions are almost in a straight line which denotes that our predictions almost did a good job!
Residual Histogram
Step9:  Regression Evaluation Metrics
MAE is the easiest to understand, because it's the average error.
MSE is more popular than MAE, because MSE "punishes" larger errors, which tends to be useful in the real world.
RMSE is even more popular than MSE, because RMSE is interpretable in the "y" units.
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
Explanation: Linear Regression
The dataset USA_Housing.csv contains the following columns:
'Avg. Area Income': Avg. Income of residents of the city house is located in.
'Avg. Area House Age': Avg Age of Houses in same city
'Avg. Area Number of Rooms': Avg Number of Rooms for Houses in same city
'Avg. Area Number of Bedrooms': Avg Number of Bedrooms for Houses in same city
'Area Population': Population of city house is located in
'Price': Price that the house sold at
'Address': Address for the house
Import Libraries
End of explanation
USAhousing = pd.read_csv('USA_Housing.csv')
USAhousing.head()
USAhousing.info()
USAhousing.describe()
USAhousing.columns
Explanation: Check out the Data
End of explanation
sns.set_style('darkgrid')
sns.pairplot(USAhousing)
sns.distplot(USAhousing['Price'])
sns.heatmap(USAhousing.corr())
Explanation: EDA-Exploratory Data Analysis
End of explanation
X = USAhousing[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
               'Avg. Area Number of Bedrooms', 'Area Population']]
y = USAhousing['Price']
Explanation: Training a Linear Regression Model
Splitting the data into X and y arrays. we are dropping the address column as it doesn't contains numerical values.We are using X features to predict y i.e. Price of the houses.
End of explanation
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=101)
Explanation: Train Test Split
We will train out model on the training set and then use the test set to evaluate the model.
End of explanation
from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(X_train,y_train)
Explanation: Creating and Training the Model
End of explanation
# print the intercept
print(lm.intercept_)
coeff_df = pd.DataFrame(lm.coef_,X.columns,columns=['Coefficient'])
coeff_df
Explanation: Model Evaluation
Let's evaluate the model by checking out it's coefficients and how we can interpret them.
End of explanation
predictions = lm.predict(X_test)
plt.scatter(y_test,predictions)
Explanation: Predictions from our Model
Let's grab predictions off our test set and see how well it did!
End of explanation
sns.distplot((y_test-predictions),bins=50);
Explanation: Here we can notice that y_test and predictions are almost in a straight line which denotes that our predictions almost did a good job!
Residual Histogram
End of explanation
from sklearn import metrics
print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))
Explanation: Regression Evaluation Metrics
MAE is the easiest to understand, because it's the average error.
MSE is more popular than MAE, because MSE "punishes" larger errors, which tends to be useful in the real world.
RMSE is even more popular than MSE, because RMSE is interpretable in the "y" units.
End of explanation
<END_TASK>