简体   繁体   中英

How to calculate correlation between two functions in Python

I need to calculate the correlation of two functions in Python

In R, I'd do:

g = function(x) {exp(-0.326723067*x)*cos(0.36002998837*x)}
f = function(x) {-x+1}

cor(f(u),g(u))

What would be the equivalent way in Python? I think I'd need to evaluate the function first before calculate the correlation. Is that so?

Assuming you want Pearson's correlation . If so:

Using numpy :

import numpy as np
np.corrcoef(f(u), g(u))

Using scipy :

import scipy.stats
scipy.stats.pearsonr(f(u), g(u))

Using pandas :

import pandas as pd
f(u).corr(g(u)) # or
g(u).corr(f(u))

Using pingouin :

import pingouin as pg
pg.corr(f(u), g(u))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM