-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyModel.py
More file actions
66 lines (50 loc) · 1.29 KB
/
MyModel.py
File metadata and controls
66 lines (50 loc) · 1.29 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import math as mt
import pandas as pd
from sklearn.model_selection import train_test_split
# def GetTotalXY(x,y):
# if len(x) == 0:
# return 0
# else:
# return (x[0] * y[0]) + GetTotalXY(x[1:],y[1:])
# def getSum(x):
# if len(x) == 0:
# return 0
# else:
# return x[0] + getSum(x[1:])
# def getSum1(x,y):
# if len(x) == 0:
# return 0
# else:
# return x[0] + getSum(x[1:]), y[0] + getSum(y[1:])
# def getinfo1(x, y):
# return getSum1(x,y)
# def getinfo(x, y):
# return getSum(x), getSum(y),GetTotalXY(x, y)
# import Dataset from csv
dataset = pd.read_csv('Salary_Data.csv')
x1 = dataset.iloc[:, :-1].values
y1 = dataset.iloc[:, -1].values
[x, X_test, y, Y_test] = train_test_split(x1, y1, test_size=0.2, random_state=0)
n = len(x)
ex = 0 # ∑x
ey = 0 # ∑y
exy = 0 # ∑xy
exp2 = 0 # ∑x^2
for i, j in zip(x, y):
exy += i * j
exp2 += mt.pow(i, 2)
ex += i
ey += j
nexy = n * exy[0] # n∑xy
nexp2 = n * exp2 # n∑x^2
exp22 = mt.pow(ex, 2) # (∑x)^2
# print('∑x = ', ex)
# print('∑y = ', ey)
# print('∑xy = ', exy[0])
# print('n∑xy = ', nexy)
# print('n∑x^2 = ', nexp2)
# print('(∑x)^2 = ', exp22)
b = (nexy - (ex * ey)) / (nexp2 - exp22)
print('b = ', b)
a = (ey-(b*ex)) / n
print('a = ', a)