We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
X=[95,85,80,70,60]Y=[85,95,70,65,70]importmathclassJStatistics():@staticmethoddefvectorial_operations(X,Y,operator):iftype(Y)==type(1):Y=[Yfor_inX]assertlen(X)==len(Y)ifoperator=="-":return[X[i]-Y[i]foriinrange(len(X))]elifoperator=="+":return[X[i]+Y[i]foriinrange(len(X))]elifoperator=="*":return[X[i]*Y[i]foriinrange(len(X))]elifoperator=="**":return[X[i]**Y[i]foriinrange(len(X))]elifoperator=="/":return[X[i]/Y[i]foriinrange(len(X))]raiseException("Operation not managed yet")@staticmethoddefmean(v):returnsum(v)/len(v)@staticmethoddefvariance(arr):mi=JStatistics.mean(arr)arr_minus_mi_pow2=[math.pow(x-mi,2)forxinarr]returnJStatistics.mean(arr_minus_mi_pow2)@staticmethoddefstdDev(arr):returnmath.sqrt(JStatistics.variance(arr))@staticmethoddefcovariance(X,Y):assertlen(X)==len(Y)n=len(X)xBar,yBar=JStatistics.mean(X),JStatistics.mean(Y)cov=(1/n)*sum((X[i]-xBar)*(Y[i]-yBar)foriinrange(n))returncov@staticmethoddefcoefficientPearson(X,Y):xstd=JStatistics.stdDev(X)ystd=JStatistics.stdDev(Y)returnJStatistics.covariance(X,Y)/(xstd*ystd)@staticmethoddefregressionLine(X,Y):m=JStatistics.coefficientPearson(X,Y)*(JStatistics.stdDev(Y)/JStatistics.stdDev(X))q=JStatistics.mean(Y)-m*JStatistics.mean(X)returnm,qm,q=JStatistics.regressionLine(X,Y)y_hat=m*80+qprint(round(y_hat,3))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 8: Least Square Regression Line
You are viewing a single comment's thread. Return to all comments →