<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import math
from numpy import linalg as la
from pylab import * 

x = np.array([0, 1, 2, 5, 9, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 4, 8, 8, 9, 12, 12])

def distanceCalculator(m,b,x1,y1):
  return  (((((x1*m) + (m**2) -(y1))/ ((m**2)+1))**2) + (m*(y1-b-(x1*m))) /((((m**2) + 1))**2))

BestFit= 110000
mAns,bAans=0,0
def sumDistanceCalculator(m,b):
  global BestFit,mAns,bAans
  #iterate all x,y
  sum=0
  for i in range(len(x)):
    #find distance between x,y
    #find +=sum distance
    sum+= distanceCalculator(m,b,x[i],y[i])
  #compare sum with bestFit
  #if less than 
    #then assign BestFit
    #bestfit_M_B assign
  if (BestFit >= sum):
    BestFit=sum
    mAns,bAans=m,b



#generate m,b and iterate with for loop
for m in np.arange(-0.02,1.5,0.02):
  sum=0
  for b in np.arange(1.0,2.0,0.02):
    #pass(m,b)
    sumDistanceCalculator(m,b)

print('the ans my code shows',mAns,bAans)  
from pylab import * 

realM,realB = polyfit(x, y, 1) 
print('the real ans is',realM,realB)
plot(x, y, 'yo', x, mAns*x+bAans, '--k') 
show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1883289124668435, 'fraction_numerical': 0.053934571175950484, 'mean_word_length': 3.9649122807017543, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '661400', 'license': 'MIT', 'n_tokens_mistral': 567, 'n_tokens_neox': 513, 'n_words': 129, 'path': '8.linear_regression/8_linear_regression.ipynb', 'repo_name': 'mohammadeunus/MachineLearningAlgorithms-MLA', 'size': '1131'}