Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use R language and Python for correlation Analysis

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use R language and Python for correlation analysis, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Due to the recent graduation thesis, I don't have much time and energy to write long articles, but I can't stop learning. Today, I'll take an inventory of the common functions of R language and the correlation analysis part of Python.

There are three commonly used methods to measure the correlation of random variables:

Pearson correlation coefficient, namely Pearson correlation coefficient, is used for the correlation coefficient between two continuous random variables.

Spearman correlation coefficient, namely Spelman correlation coefficient, is used to measure the degree of correlation between classification and sequencing variables.

Kendall correlation coefficient, that is, Kendall correlation coefficient, is also a rank correlation coefficient, but the object it calculates is a classification variable.

R language:

Cor

Cor.test

Corrplot

Cor (XMagna Null method= use = "everything", method= c ("pearson", "kendall", "spearman"))

In R language, cor function is usually used for correlation coefficient analysis, which can specify vectors or assign a data box to cor function.

The use function specifies how missing values are handled

Method is one of the three optional methods for calculating correlation coefficients.

Take the diamonds dataset as an example:

Library ("ggplot2")

Str (diamonds)

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

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

The pearson correlation coefficient is used by default.

The corrplot function can visualize the output of the correlation coefficient:

Library ("corrplot")

Library ("dplyr")

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

Use the cor.test function to test the correlation:

Cor.test (x, y, # specify with analysis variable

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

# bilateral test, unilateral test (default two sides)

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

# correlation algorithm (default pearson method)

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

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

Cor.test (~ carat+depth,diamonds)

Both of the above two writing methods are supported.

It can be seen from the results that there is almost no correlation between them, and the pearson correlation coefficient is only about 0.02.

Pyhton:

Import pandas as pd

Import numpy as np

Diamonds=pd.read_csv ('DVOGUGUP', 'encoding =' utf-8')

Diamonds.info ()

Pandas.corr with correlation coefficient in pandas

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

Mydata.info ()

Mydata.corr ()

# the correlation coefficient matrix of the data frame can be given directly

Mydata.corr () ["carat"]

# give the correlation coefficient between caret variables and other variables

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

# calculate the correlation coefficient between "carat" and "depth"

As in R language, the correlation coefficient algorithm built into pandas is also aimed at the pearson method for numerical variables.

Mydata.corr (method='pearson')

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

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

Method can also specify Spearman method and kendall method to calculate the correlation coefficient.

A summary of this article:

R language:

Cor

Cor.test

Corplot

Python:

Pandas.corr

The above is how to use R language and Python for correlation analysis, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report