-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear-reg.py
More file actions
executable file
·55 lines (43 loc) · 1.39 KB
/
linear-reg.py
File metadata and controls
executable file
·55 lines (43 loc) · 1.39 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
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import numpy as np
import matplotlib.pyplot as graph
import statsmodels.formula.api as smf
from scipy import stats
dataset = pd.read_csv('datos.csv', index_col=False, sep=" ",header=0)
#print(dataset.head())
#dataset.head()
# Asign data to X and Y
dataframe = pd.DataFrame(
{'X': dataset.gastos,
'y': dataset.ventas}
)
print(dataset)
print(dataframe)
# Calculate the mean of X and y
xmean = np.mean(dataset.gastos)
ymean = np.mean(dataset.ventas)
print("vi prom = " + str(xmean))
print("vo prom = " + str(ymean))
# Calculate the terms needed for the numator and denominator of beta
dataframe['xycov'] = (dataframe['X'] - xmean) * (dataframe['y'] - ymean)
dataframe['xvar'] = (dataframe['X'] - xmean)**2
# Calculate beta and alpha
beta = dataframe['xycov'].sum() / dataframe['xvar'].sum()
alpha = ymean - (beta * xmean)
print('A = ' + str(alpha))
print('B = ' + str(beta))
# Show calculated model
print('Y = A + BX : ')
print('Y = ' + str(alpha) + ' + ' + str(beta) + 'X')
ypred = alpha + beta * dataframe['X']
print(ypred)
# Plot regression agains data
graph.figure(figsize=(12, 6))
graph.plot(dataframe['X'], ypred) #regression line
graph.plot(dataframe['X'], dataframe['y'], 'ro') #actual data
graph.title('Datos y linea de mejor ajuste')
graph.xlabel('Gastos')
graph.ylabel('Ventas')
graph.show()