首页 > 其他 > 详细

MLR:输入两个向量,得出两个向量之间的相关度—Jason niu

时间:2018-01-07 23:59:49      阅读:385      评论:0      收藏:0      [点我收藏+]
import numpy as np
from astropy.units import Ybarn
import math  
from statsmodels.graphics.tukeyplot import results

def computeCorrelation(X, Y): 
    xBar = np.mean(X) 
    yBar = np.mean(Y)
    SSR = 0        
    varX = 0
    varY = 0
    for i in range(0 , len(X)):
        diffXXBar = X[i] - xBar  
        diffYYBar = Y[i] - yBar
        SSR += (diffXXBar * diffYYBar)
        varX +=  diffXXBar**2    
        varY += diffYYBar**2
    
    SST = math.sqrt(varX * varY)  
    return SSR / SST

testX = [1, 3, 8, 7, 9]
testY = [10, 12, 24, 21, 34]

print ("r:",computeCorrelation(testX, testY))     

def polyfit(x,y,degree):
    results={}
    
    coeffs =np.polyfit(x,y,degree)  

    results[polynomial] = coeffs.tolist()  
    p=np.poly1d(coeffs)            
    yhat=p(x)                         
    ybar=np.sum(y)/len(y)                  
    ssreg=np.sum((yhat-ybar)**2)         
    sstot=np.sum((y-ybar)**2)
    results[determination]=ssreg/sstot
    
    return results


print (polyfit(testX, testY, 1)["determination"])     

 

MLR:输入两个向量,得出两个向量之间的相关度—Jason niu

原文:https://www.cnblogs.com/yunyaniu/p/8232856.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!