數據特征分析-相關性分析

相關性分析是指對兩個或多個具備相關性的變量元素進行分析,從而衡量兩個變量的相關密切程度。

相關性的元素之間需要存在一定的聯系或者概率才可以進行相關性分析。

相關系數在[-1,1]之間。

一、圖示初判

通過pandas做散點矩陣圖進行初步判斷

df1 = pd.DataFrame(np.random.randn(200,4)*100,columns=['A','B','C','D'])
pd.plotting.scatter_matrix(df1,figsize=(12,12),diagonal='kde',marker='o',range_padding=0.1)

?

二、Pearson相關系數(皮爾森相關系數)

前提條件:數據滿足正太分布

皮爾森相關系數,也稱皮爾森積矩相關系數,是一種線性相關關系。

衡量向量相似度的一種方法,輸出范圍為-1到1,0,0代表無相關性,負值為負相關,正值為正相關。

公式意義:分子為,(x-x的均值) 乘以 (y-y的均值)的累計和;分母為,(x-x的均值)的累計和的平方根 乘以 (y-y的均值)的累計和的平方根

|r| <= 0.3? → 不存在線性相關

0.3 < |r| <= 0.5? → 低度線性相關

0.5 < |r| <= 0.8? → 顯著線性相關

|r| > 0.8? → 高度線性相關

?

1.皮爾森相關系數的推導

from scipy import stats
s1 = pd.Series(np.random.rand(100)*100).sort_values()
s2 = pd.Series(np.random.rand(100)*50).sort_values()
df = pd.DataFrame({'value1':s1.values,'value2':s2.values}) 
u1,u2 = df['value1'].mean(),df['value2'].mean()
std1,std2 = df['value1'].std(),df['value2'].std()
print('value1正太性檢驗結果:',stats.kstest(df['value1'],'norm',(u1,std1))) #需要先驗證滿足正太分布
print('value2正太性檢驗結果:',stats.kstest(df['value2'],'norm',(u2,std2)))df['(x-u1)*(y-u2)'] = (df['value1'] - u1) * (df['value2'] - u2)
df['(x-u1)**2'] = (df['value1'] - u1) ** 2
df['(y-u2)**2'] = (df['value2'] - u2) ** 2
print(df.head(3))
r = df['(x-u1)*(y-u2)'].sum()/(np.sqrt(df['(x-u1)**2'].sum()) * np.sqrt(df['(y-u2)**2'].sum()))
print('皮爾森相關系數為%.4f'%r)
value1正太性檢驗結果: KstestResult(statistic=0.09073501372253845, pvalue=0.36300244109659735)
value2正太性檢驗結果: KstestResult(statistic=0.11608587123064174, pvalue=0.12471026010748129)value1    value2  (x-u1)*(y-u2)    (x-u1)**2   (y-u2)**2
0  2.727329  0.101045    1163.135987  1864.420003  725.633345
1  4.566353  0.296802    1105.504546  1708.987866  715.125206
2  6.132681  0.308134    1063.167351  1581.937521  714.519254
皮爾森相關系數為0.9699
結果輸出

?

2.pandas的corr()方法

上述方法為計算過程,可使用pandas的corr()方法直接生成相關系數矩陣

s1 = pd.Series(np.random.rand(100)*100).sort_values()
s2 = pd.Series(np.random.rand(100)*50).sort_values()
df = pd.DataFrame({'value1':s1.values,'value2':s2.values})
r = df.corr()  #參數默認為pearson
print(r)
#           value1    value2
# value1  1.000000  0.988596
# value2  0.988596  1.000000

?

三、Sperman秩相關系數(斯皮爾曼相關系數)

皮爾森相關系數只能用于分析服從正態分布的連續變量的相關性,對于不服從正態分布的變量,可采用Sperman秩相關系數進行相關性分析。

Sperman秩相關系數,也稱等級相關系數。如果數據中沒有重復值, 并且當兩個變量完全單調相關時,斯皮爾曼相關系數則為+1或?1。

計算邏輯:對兩個變量的取值按照從小到大順序編秩,如果兩個值大小相等,則秩次為(index1+index2)/2,

1.spearman相關系數的推導

df = pd.DataFrame({'智商':[106,86,100,101,99,103,97,113,112,110],'每周看電視小數':[7,0,27,50,28,29,20,12,6,17]})
n = len(df)
df.sort_values('智商',inplace = True)
df['range1'] = np.arange(1,n+1)
df.sort_values('每周看電視小數',inplace = True)
df['range2'] = np.arange(1,n+1)
df['d'] = df['range1'] - df['range2']
df['d**2'] = df['d'] ** 2
rs = 1 - 6 * (df['d**2'].sum())/(n*(n**2-1))
print('斯皮爾曼相關系數為%.4f'%rs)
# 斯皮爾曼相關系數為-0.1758

?

2.pandas的corr()方法

corr()默認為pearson相關系數,添加參數method='spearman'轉化為spearman相關系數。

df = pd.DataFrame({'智商':[106,86,100,101,99,103,97,113,112,110],'每周看電視小數':[7,0,27,50,28,29,20,12,6,17]})
rs = df.corr(method='spearman')
print(rs)
#                   智商        每周看電視小數
# 智商            1.000000       -0.175758
# 每周看電視小數   -0.175758       1.000000

?

轉載于:https://www.cnblogs.com/Forever77/p/11361213.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391671.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391671.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391671.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

leetcode 354. 俄羅斯套娃信封問題(dp+二分)

給定一些標記了寬度和高度的信封&#xff0c;寬度和高度以整數對形式 (w, h) 出現。當另一個信封的寬度和高度都比這個信封大的時候&#xff0c;這個信封就可以放進另一個信封里&#xff0c;如同俄羅斯套娃一樣。 請計算最多能有多少個信封能組成一組“俄羅斯套娃”信封&#…

fastlane use_legacy_build_api true

fastlane版本號&#xff1a;fastlane 1.108.0 Xcode版本號&#xff1a;8.1 MacOS版本號&#xff1a;10.12 使用fastlane打包 - Release / Ad-Hoc包時報錯: [13:36:59]: There was an error exporting your application [13:36:59]: Unfortunately the new Xcode export API is …

獲取所有權_住房所有權經濟學深入研究

獲取所有權Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seekin…

scala 單元測試_Scala中的法律測試簡介

scala 單元測試Property-based law testing is one of the most powerful tools in the scala ecosystem. In this post, I’ll explain how to use law testing and the value it’ll give you using in-depth code examples.基于財產的法律測試是scala生態系統中最強大的工具…

getBoundingClientRect說明

getBoundingClientRect用于獲取某個元素相對于視窗的位置集合。 1.語法&#xff1a;這個方法沒有參數。 rectObject object.getBoundingClientRect() 2.返回值類型&#xff1a;TextRectangle對象&#xff0c;每個矩形具有四個整數性質&#xff08; 上&#xff0c; 右 &#xf…

robot:接口入參為圖片時如何發送請求

https://www.cnblogs.com/changyou615/p/8776507.html 接口是上傳圖片&#xff0c;通過F12抓包獲得如下信息 由于使用的是RequestsLibrary&#xff0c;所以先看一下官網怎么傳遞二進制文件參數&#xff0c;https://2.python-requests.org//en/master/user/advanced/#post-multi…

開發小Tips-setValue

字典添加數據請用 [dict setValue:value forKey:key] 代替 [dict setObject:value forKey:key] 復制代碼這樣在一些網絡傳參時不用考慮nil的情況&#xff0c;?

瀏覽器快捷鍵指南_快速但完整的IndexedDB指南以及在瀏覽器中存儲數據

瀏覽器快捷鍵指南Interested in learning JavaScript? Get my JavaScript ebook at jshandbook.com有興趣學習JavaScript嗎&#xff1f; 在jshandbook.com上獲取我JavaScript電子書 IndexedDB簡介 (Introduction to IndexedDB) IndexedDB is one of the storage capabilities …

Java-Character String StringBuffer StringBuilder

Java Character 類 Character 類用于對單個字符進行操作character 類在對象包裝一個基本類型char的值 char ch "a";char uniChar \u039A;char[] charArray {a, b, c};使用Character的構造方法創建一個Character類對象 Character ch new Character(a);Charact…

已知兩點坐標拾取怎么操作_已知的操作員學習-第3部分

已知兩點坐標拾取怎么操作有關深層學習的FAU講義 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as mu…

缺失值和異常值處理

一、缺失值 1.空值判斷 isnull()空值為True&#xff0c;非空值為False notnull() 空值為False&#xff0c;非空值為True s pd.Series([1,2,3,np.nan,hello,np.nan]) df pd.DataFrame({a:[1,2,np.nan,3],b:[2,np.nan,3,hello]}) print(s.isnull()) print(s[s.isnull() False]…

leetcode 503. 下一個更大元素 II(單調棧)

給定一個循環數組&#xff08;最后一個元素的下一個元素是數組的第一個元素&#xff09;&#xff0c;輸出每個元素的下一個更大元素。數字 x 的下一個更大的元素是按數組遍歷順序&#xff0c;這個數字之后的第一個比它更大的數&#xff0c;這意味著你應該循環地搜索它的下一個更…

setNeedsDisplay看我就懂!

前言&#xff1a; setNeedsDisplay異步執行的。它會自動調用drawRect方法&#xff0c;這樣可以拿到 UIGraphicsGetCurrentContext&#xff0c;就可以繪制了。而setNeedsLayout會默認調用layoutSubViews&#xff0c;處理子視圖中的一些數據。 一、著手 我定義了一個UIView的子類…

如何使用ArchUnit測試Java項目的體系結構

by Emre Savc?由EmreSavc? 如何使用ArchUnit測試Java項目的體系結構 (How to test your Java project’s architecture with ArchUnit) In this post, I will show you an interesting library called ArchUnit that I met recently. It does not test your code flow or bu…

解決ionic3 android 運行出現Application Error - The connection to the server was unsuccessful

在真機上啟動ionic3打包成的android APK,啟動了很久結果彈出這個問題&#xff1a; Application Error - The connection to the server was unsuccessful 可能是我項目資源太多東西了&#xff0c;啟動的時間太久了&#xff0c;導致超時了。 解決方案是在項目目錄下的config.xml…

特征工程之特征選擇_特征工程與特征選擇

特征工程之特征選擇&#x1f4c8;Python金融系列 (&#x1f4c8;Python for finance series) Warning: There is no magical formula or Holy Grail here, though a new world might open the door for you.警告 &#xff1a; 這里沒有神奇的配方或圣杯&#xff0c;盡管新世界可…

搭建Harbor企業級docker倉庫

https://www.cnblogs.com/pangguoping/p/7650014.html 轉載于:https://www.cnblogs.com/gcgc/p/11377461.html

leetcode 131. 分割回文串(dp+回溯)

給你一個字符串 s&#xff0c;請你將 s 分割成一些子串&#xff0c;使每個子串都是 回文串 。返回 s 所有可能的分割方案。 回文串 是正著讀和反著讀都一樣的字符串。 示例 1&#xff1a; 輸入&#xff1a;s “aab” 輸出&#xff1a;[[“a”,“a”,“b”],[“aa”,“b”]]…

[翻譯練習] 對視圖控制器壓入導航棧進行測試

譯自&#xff1a;swiftandpainless.com/testing-pus… 上個月我寫的關于使用 Swift 進行測試驅動開發的書終于出版了&#xff0c;我會在本文和接下來的一些博文中介紹這本書撰寫過程中的一些心得和體會。 在本文中&#xff0c;我將會展示一種很好的用來測試一個視圖控制器是否因…

python多人游戲服務器_Python在線多人游戲開發教程

python多人游戲服務器This Python online game tutorial from Tech with Tim will show you how to code a scaleable multiplayer game with python using sockets/networking and pygame. You will learn how to deploy your game so that people anywhere around the world …