-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreg.py
46 lines (39 loc) · 1.48 KB
/
reg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model
def get_data(file_name):
data = pd.read_csv(file_name)
x_parameter = []
y_parameter = []
for single_square_feet ,single_price_value in zip(data['x'],data['y']):
x_parameter.append([single_square_feet])
y_parameter.append(single_price_value)
return x_parameter,y_parameter
# Function for Fitting data to Linear model
def linear_model_main(X_parameters,Y_parameters,predict_value):
# Create linear regression object
regr = linear_model.LinearRegression()
regr.fit(X_parameters, Y_parameters)
predict_outcome = regr.predict(predict_value)
predictions = {}
predictions['intercept'] = regr.intercept_
predictions['coefficient'] = regr.coef_
predictions['predicted_value'] = predict_outcome
return predictions
def show_linear_line(X_parameters,Y_parameters):
# Create linear regression object
regr = linear_model.LinearRegression()
regr.fit(X_parameters, Y_parameters)
plt.scatter(X_parameters,Y_parameters,color='blue')
plt.plot(X_parameters,regr.predict(X_parameters),color='red',linewidth=4)
plt.xticks(())
plt.yticks(())
plt.show()
x,y = get_data('data.csv')
predict_value =0
result = linear_model_main(x,y,predict_value)
print "Intercept value " , result['intercept']
print "coefficient" , result['coefficient']
print "Predicted value: ",result['predicted_value']
show_linear_line(x,y)