<filename>linear_regression (1).py
# -*- coding: utf-8 -*-
"""linear_regression.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1deytit-Grv8_jaLco08S37MrI3_TglLA
"""

import zipfile
from google.colab import drive

drive.mount('/content/drive/')
zip_ref = zipfile.ZipFile('/real-estate.zip', 'r')
zip_ref.extractall()
zip_ref.close()

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.metrics import r2_score

from sklearn.linear_model import LinearRegression



from sklearn.model_selection import train_test_split

df = pd.read_csv('/content/Real estate.csv')

df



x = df['X2 house age'].values

x = x.reshape(-1,1)

x.shape

y = df[['Y house price of unit area']].values

y.shape

y =y.reshape(-1,1)

df

from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures(degree=2, include_bias=False)
#if you change the poly degree to 1 you get an r2 score of 1.0
x_new = poly.fit_transform(x)

new_model = LinearRegression()

new_model.fit(x_new, y)

y_pred = new_model.predict(x_new)

plt.scatter(x, y)
plt.plot(x_new, y_pred)
plt.legend(['Predicted Line', 'Observed Line'])
plt.show()

print(new_model.predict(poly.fit_transform(y)))

c = (new_model.predict(poly.fit_transform(y)))



new_model = LinearRegression()

new_model.fit(c,y)

y_pred = new_model.predict(c)





r2_score(y, y_pred)



plt.figure(figsize=(26,14))
plt.plot(y_pred,'g',label="Predicted real esate price by month with 74% r2 score using a linear model with Polynomial features to the 2nd degree")
plt.plot(y,'r',label="Past real estate price")
plt.legend(loc="upper left")
plt.xlabel('Transaction Date')
plt.ylabel('House price of unit area')
plt.title('Predicted and past real esate price')
plt.show()

<filename>linear_regression (1).py
# -*- coding: utf-8 -*-
"""linear_regression.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1deytit-Grv8_jaLco08S37MrI3_TglLA
"""

import zipfile
from google.colab import drive

drive.mount('/content/drive/')
zip_ref = zipfile.ZipFile('/real-estate.zip', 'r')
zip_ref.extractall()
zip_ref.close()

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.metrics import r2_score

from sklearn.linear_model import LinearRegression



from sklearn.model_selection import train_test_split

df = pd.read_csv('/content/Real estate.csv')

df



x = df['X2 house age'].values

x = x.reshape(-1,1)

x.shape

y = df[['Y house price of unit area']].values

y.shape

y =y.reshape(-1,1)

df

from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures(degree=2, include_bias=False)
#if you change the poly degree to 1 you get an r2 score of 1.0
x_new = poly.fit_transform(x)

new_model = LinearRegression()

new_model.fit(x_new, y)

y_pred = new_model.predict(x_new)

plt.scatter(x, y)
plt.plot(x_new, y_pred)
plt.legend(['Predicted Line', 'Observed Line'])
plt.show()

print(new_model.predict(poly.fit_transform(y)))

c = (new_model.predict(poly.fit_transform(y)))



new_model = LinearRegression()

new_model.fit(c,y)

y_pred = new_model.predict(c)





r2_score(y, y_pred)



plt.figure(figsize=(26,14))
plt.plot(y_pred,'g',label="Predicted real esate price by month with 74% r2 score using a linear model with Polynomial features to the 2nd degree")
plt.plot(y,'r',label="Past real estate price")
plt.legend(loc="upper left")
plt.xlabel('Transaction Date')
plt.ylabel('House price of unit area')
plt.title('Predicted and past real esate price')
plt.show()

# -*- coding: utf-8 -*- linear_regression.ipynb  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1deytit-Grv8_jaLco08S37MrI3_TglLA #if you change the poly degree to 1 you get an r2 score of 1.0