-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear_regression.py
95 lines (81 loc) · 2.7 KB
/
Linear_regression.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Linear_regression.py
#
# Copyright 2023 Diego Martinez Gutierrez <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
# ---------------------------
# Importación de los módulos
# ---------------------------
import numpy as np
import matplotlib.pyplot as plt
# ----------
# Funciones
# ----------
def estimate_coef(x, y):
# number of observations/points
n = np.size(x)
# mean of x and y vector
m_x = np.mean(x)
m_y = np.mean(y)
# calculating cross-deviation and deviation about x
SS_xy = np.sum(y*x) - n*m_y*m_x
SS_xx = np.sum(x*x) - n*m_x*m_x
# calculating regression coefficients
b_1 = SS_xy / SS_xx
b_0 = m_y - b_1*m_x
return (b_0, b_1)
def plot_regression_line(x, y, b):
# plotting the actual points as scatter plot
plt.scatter(x, y, color = "m",
marker = "o", s = 30)
# predicted response vector
y_pred = b[0] + b[1]*x
# plotting the regression line
plt.plot(x, y_pred, color = "g")
# plot line at y=0
plt.axhline(y=0.0, color='r', linestyle='--')
# putting labels
plt.xlabel('T(K)')
plt.ylabel('$\Omega^2$ (cm-1)')
plt.title(r'Squared Frequencies versus Temperatures', fontsize='small')
plt.tight_layout()
plt.savefig("Linear_regression.png")
#plt.savefig("Ajuste_{}".format("lineal"))
# function to show plot
plt.show()
def main():
# observations / data
# x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
#y = np.array([14.9118152**2, 23.44094259**2, 49.22668079**2])
#y = np.array([109.26893737**2, 109.82139367**2, 112.42649199**2])
# x = np.array([50, 100, 300])
# y = np.array([-(16.23**2),-(16.09**2),12.14**2,31.18**2])
x = np.array([0, 50, 100, 200, 300])
y = np.array([-(19.51637005**2), -(16.84247323**2), -(17.05193197**2), 16.46233502**2, 31.22355090**2])
# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))
# plotting regression line
plot_regression_line(x, y, b)
if __name__ == "__main__":
main()