天天看點

Python資料分析與挖掘實戰第三章筆記之相關性分析

#相關性分析:分析連續變量之間線性相關程度的強弱,并用适當的統計名額表示出來
# 直接繪制散點圖
# 繪制散點圖矩陣:當同僚考慮多個變量間的相關關系時,可以利用散點圖矩陣同時繪制各變量間的散點圖。
# 計算相關系數:二進制變量的相關性分析中常用pearson相關系數,spearman秩相關系數和判定系數;pearson相關系數要求連續變量的取值服從正态分布,不服從正态分布的變量、分類或者等級變量之間采用spearman秩相關系數
# 一個變量相同的取值必須有相同的秩次。隻要兩個變量具有嚴格單調的函數關系,那麼他們就是完全spearman相關的,pearson相關隻有在變量具有線性關系時才是完全相關的。
# 在正态分布假定下,spearman和pearson在效率上是等價的,對于連續測量資料,pearson更适合。
# 判定系數:是相關系數的平方,用來衡量回歸方程對y的解釋成都。
# 餐飲銷量資料相關性分析代碼
from __future__ import print_function
import pandas as pd
catering_sale='E:/WTTfiles/自我學習/機器學習/python資料分析與挖掘實戰/chapter3/demo/data/catering_sale_all.xls'
data=pd.read_excel(catering_sale,index_col=u'日期')
print(data.corr())
print(data.corr()[u'百合醬蒸鳳爪'])#隻顯示百合醬蒸鳳爪與其它菜式的相關系數
print(data[u'百合醬蒸鳳爪'].corr(data[u'翡翠蒸香茜餃']))#計算百合醬蒸鳳爪與翡翠蒸香茜餃之間的相關系數



# 一些pandas和numpy的常用函數和作圖函數
#>>> import pandas as pd
# >>> D=pd.DataFrame([range(1,8),range(2,9)])
# >>> D.corr(method='pearson')
# >>> s1=D.loc[0]
# >>> s2=D.loc[1]
# >>> s1.corr(s2,method='pearson')
# >>> import numpy as np
# >>> D=pd.DataFrame(np.random.randn(6,5))
# >>> D.cov()
#>>> D=pd.DataFrame([range(1,8),range(2,9)])
# >>> D.corr(method='spearman')
#>>> D=pd.DataFrame(np.random.randn(6,5))
# >>> D.skew()
#>>> D.kurt()
# >>> D.describe()
# >>> D=pd.Series(range(0,20))
# >>> D.cumsum

# >>> import matplotlib.pyplot as plt
# >>> plt.rcParams['font.sans-serif']=['SimHei']
# >>> plt.rcParams['axes.unicode_minus']=False
# >>> plt.figure(figsize=(7,5))
# >>> import numpy as np
# >>> x=np.linspace(0,2*np.pi,50)
# >>> y=np.sin(x)
# >>> plt.plot(x,y,'bp--')
# [<matplotlib.lines.Line2D object at 0x000001F715CE3710>]
# >>> plt.show()
# >>> import matplotlib.pyplot as plt
# >>> labels='Frogs','Hogs','Dogs','Logs'
# >>> sizes=[15,30,45,10]
# >>> colors=['yellowgreen','gold','lightskyblue','lightcoral']
# >>> explode=(0,0.1,0,0)
# >>> plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.1f%%',shadow=True,startangle=90)
# >>> plt.axis('equal')
# >>> plt.show()

# >>> import matplotlib.pyplot as plt
# >>> import numpy as np
# >>> x=np.random.randn(1000)
# >>> plt.hist(x,10)
# >>> plt.show()

# >>> import pandas as pd
# >>> x=np.random.randn(1000)
# >>> D=pd.DataFrame([x,x+1]).T
# >>> D.plot(kind='box')
# <matplotlib.axes._subplots.AxesSubplot object at 0x000001F715A7E898>
# >>> plt.show()

# >>> erro=np.random.randn(10)
# >>> y=pd.Series(np.sin(np.arange(10)))
# >>> y.plot(yerr=erro)
# >>> plt.show()      
           百合醬蒸鳳爪    翡翠蒸香茜餃   金銀蒜汁蒸排骨     樂膳真味雞     蜜汁焗餐包      生炒菜心    鐵闆酸菜豆腐  \
百合醬蒸鳳爪   1.000000  0.009206  0.016799  0.455638  0.098085  0.308496  0.204898   
翡翠蒸香茜餃   0.009206  1.000000  0.304434 -0.012279  0.058745 -0.180446 -0.026908   
金銀蒜汁蒸排骨  0.016799  0.304434  1.000000  0.035135  0.096218 -0.184290  0.187272   
樂膳真味雞    0.455638 -0.012279  0.035135  1.000000  0.016006  0.325462  0.297692   
蜜汁焗餐包    0.098085  0.058745  0.096218  0.016006  1.000000  0.308454  0.502025   
生炒菜心     0.308496 -0.180446 -0.184290  0.325462  0.308454  1.000000  0.369787   
鐵闆酸菜豆腐   0.204898 -0.026908  0.187272  0.297692  0.502025  0.369787  1.000000   
香煎韭菜餃    0.127448  0.062344  0.121543 -0.068866  0.155428  0.038233  0.095543   
香煎羅蔔糕   -0.090276  0.270276  0.077808 -0.030222  0.171005  0.049898  0.157958   
原汁原味菜心   0.428316  0.020462  0.029074  0.421878  0.527844  0.122988  0.567332   


            香煎韭菜餃     香煎羅蔔糕    原汁原味菜心  
百合醬蒸鳳爪   0.127448 -0.090276  0.428316  
翡翠蒸香茜餃   0.062344  0.270276  0.020462  
金銀蒜汁蒸排骨  0.121543  0.077808  0.029074  
樂膳真味雞   -0.068866 -0.030222  0.421878  
蜜汁焗餐包    0.155428  0.171005  0.527844  
生炒菜心     0.038233  0.049898  0.122988  
鐵闆酸菜豆腐   0.095543  0.157958  0.567332  
香煎韭菜餃    1.000000  0.178336  0.049689  
香煎羅蔔糕    0.178336  1.000000  0.088980  
原汁原味菜心   0.049689  0.088980  1.000000  
百合醬蒸鳳爪     1.000000
翡翠蒸香茜餃     0.009206
金銀蒜汁蒸排骨    0.016799
樂膳真味雞      0.455638
蜜汁焗餐包      0.098085
生炒菜心       0.308496
鐵闆酸菜豆腐     0.204898
香煎韭菜餃      0.127448
香煎羅蔔糕     -0.090276
原汁原味菜心     0.428316
Name: 百合醬蒸鳳爪, dtype: float64
0.009205803051836482
      

繼續閱讀