#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jun 27 18:42:36 2023 @author: richardson """ import statistics as st import pandas as pd import numpy as np WS = pd.read_csv('sampleData.csv', header=None) bubba = np.array(WS) #print(bubba) xVals = [b[0] for b in bubba] yVals = [b[1] for b in bubba] #print(xVals) #print(yVals) mx = st.mean(xVals) my = st.mean(yVals) sx = st.pstdev(xVals) sy = st.pstdev(yVals) xy = [a[0]*a[1] for a in bubba] sumxy = sum(xy) sumx = sum(xVals) sumx2 = sum(a[0]**2 for a in bubba) sumy = sum(yVals) N = len(bubba) # linear regression equation a = (sumy*sumx2-sumx*sumxy)/(N*sumx2-sumx*sumx) b = (N*sumxy-sumx*sumy)/(N*sumx2-sumx*sumx) LRVals = [a+b*xVals[j] for j in range(N)] #We write the equation print("Our equation is y = "+str(round(a,3))+" + "+str(round(b,3))+"x") # another way to make the linear # regression vector of predicted # yVals def LR(t): return a+b*t LRvect = [LR(a) for a in xVals] #print(LRVals) #print(LRvect) import matplotlib.pyplot as plt plt.scatter(xVals,yVals, color='red') xmin = min(xVals) xmax = max(xVals) xs = np.linspace(xmin,xmax,100) ys = [LR(t) for t in xs] plt.plot(xs, ys, color= 'blue') plt.show()