title: Slope m of least squares best fit line from two arrays
body: <p>Given two text files and 'x' and 'y' as arrays, write a subroutine that will give the slope 'm' and intercept 'b' of the least squares best fit line.</p>
  <blockquote>
  <p>x = 1,2,3,4</p>
  <p>y = 5,6,7,8</p>
  <p>m = ((sum(xy)-sum(x)ybar)/((sumx^2)-(sum(x)xbar))</p>
  <p>b = ybar - m(xbar).</p>
  <p>xbar and ybar are the mean values of the x and y arrays.</p>
  </blockquote>
  <p>My instructor said we cannot use intrinsic python functions beyond np.loadtxt.</p>
  <p>I'm trying to find a more efficient way to do this than besides defining the 4 array combinations, then the sums of each of the 4 combos, and then finding m.</p>
  <p>I tried to define each sum of the array combinations as nested loops.</p>
  <pre><code>import numpy as np
  
  x = np.loadtxt(&quot;C:/Users/Bob/Documents/School/Comp Phys/xlsf.dat&quot;)
  y = np.loadtxt(&quot;C:/Users/Bob/Documents/School/Comp Phys/ylsf.dat&quot;)
  
  xbar = np.mean(x)
  ybar = np.mean(y)
  
  nx = len(x)
  ny = len(y)
  
  q = print(xbar)
  
  sum1=[[0 for i in range(nx)] for j in range(ny)]
  
  for i in range (0, nx):
      for j in range (0, ny):
          sum1[i][j] = x[i] * y[j]
  
  for i in range (ny):
          for j in range (nx):
              ((sum1[i][j]))
  
  sum2=[[0 for q in range(nx)] for r in range(ybar)]
  
  for q in range (0, nx):
      for r in range (0, ybar):
          sum2[q][r] = x[q] * ybar[r]
  
  for q in range (ybar):
      for r in range (nx):
          ((sum2[q][r]))
  </code></pre>
  <p>Python is getting caught up with the sum2 part, with the following error:</p>
  <blockquote>
  <p>sum2=[[0 for q in range(nx)] for r in range(ybar)]</p>
  <blockquote>
  <p>TypeError: 'numpy.float64' object cannot be interpreted as an integer.</p>
  </blockquote>
  </blockquote>