Linear Regression
Linear Regression
REGRESSION
WHAT IS REGRESSION?
Regression searches for relationships among variables.
For example, you can observe several employees of some company
and try to understand how their salaries depend on the features,
such as experience, level of education, role, city they work in, and
so on.
#load dataset
df = pd.read_csv('homeprices.csv')
df.head()
PYTHON
IMPLEMENTATION
#visualization
plt.xlabel('area')
plt.ylabel('price')
plt.scatter(df.area,df.price,color='red',marker='+')
new_df_x = df.drop('price',axis='columns')
new_df_x
price_y = df.price
price_y
# Create linear regression object
reg = linear_model.LinearRegression()
PYTHON
IMPLEMENTATION
#fit the data
reg.fit(new_df_x,price_y)
#predict
predict_price_y = reg.predict(new_df_x)
# model evaluation
rmse = mean_squared_error(price_y, predict_price_y)
r2 = r2_score(price_y, predict_price_y)
# printing values
print('Slope:' ,reg.coef_)
print('Intercept:', reg.intercept_)
print('Root mean squared error: ', rmse)
print('R2 score: ', r2)
PYTHON
IMPLEMENTATION
#(1) Predict price of a home with area = 3300 sqr ft
predct_price = reg.predict([[3300]])
predct_price
# plotting values
# data points
plt.scatter(new_df_x, price_y, s=10)
plt.xlabel('Area')
plt.ylabel('Price')
# predicted values
plt.plot(new_df_x, predict_price_y, color='r')
plt.show()