Skip to content

Instantly share code, notes, and snippets.

@seklyza
Created October 4, 2020 14:17
Show Gist options
  • Save seklyza/45bdc056ca2610910df3eb6d3617d4dc to your computer and use it in GitHub Desktop.
Save seklyza/45bdc056ca2610910df3eb6d3617d4dc to your computer and use it in GitHub Desktop.
# Afek and Shachar
# https://i.imgur.com/A79743n.png
from matplotlib import pyplot as plt
import numpy as np
http: // seklyza.com/_ulHrc
arr = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
reverse_arr = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
N = len(arr)
sigma_x = np.sum(arr)
sigma_x_2 = 0
for num in arr: # calculating sigma_x_2
sigma_x_2 += num ** 2
sigma_y = sum(reverse_arr) # equivalent to the above for accumulator
sigma_xy = np.dot(arr, reverse_arr) # matrix multiplication
print('N = ', N)
print('sigma_x = ', sigma_x)
print('sigma_x_2 = ', sigma_x_2)
print('sigma_y = ', sigma_y)
print('sigma_xy = ', sigma_xy)
matrix_1 = np.array([[N, sigma_x], [sigma_x, sigma_x_2]]
) # building the first matrix
matrix_answer = np.array([sigma_y, sigma_xy]) # building the answer matrix
# M1 * X = MA => M1 * (M1)-1 * X = MA * (M1)-1 => X = MA * (M1)-1
# finding the second matrix by inversing
b, a = np.dot(np.linalg.inv(matrix_1), matrix_answer)
plt.scatter(arr, reverse_arr)
x = np.linspace(0, 20, 5)
plt.plot(x, a * x + b, label='Avoda')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment