溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何用R語言和Python進行相關(guān)性分析

發(fā)布時間:2021-10-11 17:51:23 來源:億速云 閱讀:313 作者:柒染 欄目:大數(shù)據(jù)

本篇文章給大家分享的是有關(guān)如何用R語言和Python進行相關(guān)性分析,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

由于最近畢業(yè)論文纏身,一直都沒有太多時間和精力撰寫長篇的干貨,但是呢學(xué)習(xí)的的腳步不能停止,今天跟大家盤點一下R語言與Python中到的相關(guān)性分析部分的常用函數(shù)。

常用的衡量隨機變量相關(guān)性的方法主要有三種:

  • pearson相關(guān)系數(shù);即皮爾遜相關(guān)系數(shù),用于橫向兩個連續(xù)性隨機變量間的相關(guān)系數(shù)。

  • spearman相關(guān)系數(shù);即斯皮爾曼相關(guān)系數(shù),用于衡量分類定序變量間的相關(guān)程度。

  • kendall相關(guān)系數(shù);即肯德爾相關(guān)系數(shù),也是一種秩相關(guān)系數(shù),不過它所計算的對象是分類變量。

R語言:

  • cor

  • cor.test

  • corrplot

cor(x,y=NULL,use="everything",method= c("pearson","kendall","spearman"))

在R語言中,通常使用cor函數(shù)進行相關(guān)系數(shù)分析,可以分別指定向量,也可以指定給cor函數(shù)一個數(shù)據(jù)框。

  • use函數(shù)指定處理缺失值的方式

  • method是可選的三種相關(guān)系數(shù)計算方法。

這里以diamonds數(shù)據(jù)集為例:

library("ggplot2")

str(diamonds)

如何用R語言和Python進行相關(guān)性分析

cor(diamonds[,c("carat","depth","price")])

cor(diamonds[,c("carat","depth","price")],method= "pearson")

如何用R語言和Python進行相關(guān)性分析

默認(rèn)情況下使用的是pearson相關(guān)系數(shù)。

corrplot函數(shù)可以針對相關(guān)系數(shù)輸出的結(jié)果進行可視化:

library("corrplot")

library("dplyr")

cor(diamonds[,c("carat","depth","price")])%>%corrplot()

如何用R語言和Python進行相關(guān)性分析

使用cor.test函數(shù)進行相關(guān)性的檢驗:

cor.test(x, y,    #指定帶分析變量

alternative = c("two.sided", "less", "greater"),         

#雙側(cè)檢驗,單側(cè)檢驗(默認(rèn)雙側(cè))

method = c("pearson", "kendall", "spearman"), 

 #相關(guān)性算法(默認(rèn)pearson法)

exact = NULL, conf.level = 0.95, continuity = FALSE, ...)

cor.test(diamonds$carat,diamonds$depth)

cor.test(~carat+depth,diamonds)

如何用R語言和Python進行相關(guān)性分析

以上兩種寫法都是支持的。

從結(jié)果可以看到,兩者幾乎不相關(guān),pearson相關(guān)系數(shù)僅有0.02左右。

Pyhton:

import pandas as pd

import numpy as np

diamonds=pd.read_csv('D:/R/File/diamonds.csv',sep = ',',encoding = 'utf-8')

diamonds.info()

如何用R語言和Python進行相關(guān)性分析

pandas中帶有相關(guān)系數(shù)函數(shù)pandas.corr

如何用R語言和Python進行相關(guān)性分析

mydata=diamonds[["carat","depth","table","price"]]

mydata.info()

如何用R語言和Python進行相關(guān)性分析

mydata.corr()    

#可以直接給出數(shù)據(jù)框的相關(guān)系數(shù)矩陣

mydata.corr()["carat"]    

#給出caret變量與其他變量之間的相關(guān)系數(shù)

mydata["carat"].corr(mydata["depth"])    

#計算"carat"與"depth"之間的相關(guān)系數(shù)

與R語言中一樣,pandas中內(nèi)置的相關(guān)系數(shù)算法也是針對針對數(shù)值型變量的pearson法。

mydata.corr(method='pearson')                             

mydata.corr(method='pearson')["carat"]                    

mydata["carat"].corr(method='pearson',mydata["depth"])    

如何用R語言和Python進行相關(guān)性分析

method也可以指定spearman法和kendall法計算相關(guān)系數(shù)。

本文小結(jié):

R語言:

  • cor

  • cor.test

  • corplot

Python:

pandas.corr

以上就是如何用R語言和Python進行相關(guān)性分析,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI