Skip to content

Commit 571e12d

Browse files
authoredOct 19, 2021
Merge pull request GeeksforGeeks-VIT-Bhopal#60 from AlyfarhanKhan/main
LinearRegression.py
2 parents 9a45b09 + 9037b2c commit 571e12d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎LinearRegression.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from numpy import loadtxt, ones, zeros, array, linspace, logspace
2+
# from pylab import scatter, show, xlabel, ylabel, title, plot, contour
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
import pandas as pd
6+
from sklearn.linear_model import LinearRegression
7+
from sklearn.model_selection import train_test_split
8+
from sklearn import metrics
9+
10+
# Load the datasets
11+
df = pd.read_csv('USA_Housing.csv')
12+
# print(df.columns)
13+
x = df[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
14+
'Avg. Area Number of Bedrooms', 'Area Population']]
15+
x = df[['Avg. Area Number of Rooms', 'Avg. Area Number of Bedrooms']]
16+
y = df[['Price']]
17+
18+
reg = LinearRegression()
19+
20+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.10, random_state=2)
21+
reg.fit(x_train, y_train)
22+
print(reg.score(x_test, y_test))
23+
print(reg.intercept_)
24+
print(reg.coef_)
25+
predictions = reg.predict(x_test)
26+
print(reg.predict([[5, 6]]))
27+
28+
print('MAE:', metrics.mean_absolute_error(y_test, predictions))
29+
print('MSE:', metrics.mean_squared_error(y_test, predictions))

0 commit comments

Comments
 (0)