pandas相关系数函数-corr
2020-05-09 本文已影响0人
橘猫吃不胖
前面写了篇相关系数的文章:
当时,在计算相关系数的时候,是在Excel中,如果使用pandas的话,就非常容易了,因为算法是固定的,所以直接调用一个函数就算好了
pandas.DataFrame.corr
DataFrame.corr(self, method='pearson', min_periods=1) → 'DataFrame'
Compute pairwise correlation of columns, excluding NA/null values.
这个函数是计算,每两列之间的相关系数,是两两之间
函数很简单,也只有2个参数:

method
是计算相关系数的方法,默认是皮尔森相关系数;
min_periods
感觉是说,最小样本数,因为相关系数有些缺点,比如样本数太少的话,相关系数的意义就不是很大
我们来试试,依然,使用“相关系数”里面说的数据集
data_path = r'D:\OneDrive\python\datasets\demo_广告费投入与销售额.txt'
df = pd.read_csv(data_path , encoding='utf-8' , sep='\t')

df.corr()
就是这样,直接调用,默认使用皮尔森相关系数就好了

很方便吧,和我们上一篇用Excel算的也是一样的

好了,收工。