解读pandas.DataFrame.corrwith

作者:waifdzdn 时间:2023-10-17 13:22:09 

解读pandas.DataFrame.corrwith

pandas.DataFrame.corrwith用于计算DataFrame中行与行或者列与列之间的相关性。

Parameters:

other:DataFrame, Series. Object with which to compute correlations.

axis: {0 or ‘index’, 1 or ‘columns’}, default 0. 0 or ‘index’ to compute column-wise, 1 or ‘columns’ for row-wise.

method:{‘pearson’, ‘kendall’, ‘spearman’} or callable.

axis=0或者axis=‘index’ 表示计算列与列的相关性,axis=1或者axis=‘columns’ 表示计算行与行的相关性。

method是计算相关性的方法,这里采用pearson correlation coefficient(皮尔逊相关系数)。

下面以一个观众对电影评分的例子说明

解读pandas.DataFrame.corrwith

每一行表示一个观众对所有电影的评分,每一列表示所有观众对一部电影的评分。

然后分别计算第一位观众和其他观众的相关性 和第一部电影和其它电影的相关性。

代码如下

import pandas as pd
import numpy as np

data = np.array([[5, 5, 3, 3, 4], [3, 4, 5, 5, 4],
                [3, 4, 3, 4, 5], [5, 5, 3, 4, 4]])
df = pd.DataFrame(data, columns=['The Shawshank Redemption',
                                'Forrest Gump', 'Avengers: Endgame',
                                'Iron Man', 'Titanic '],
                 index=['user1', 'user2', 'user3', 'user4'])
# Compute correlation between user1 and other users
user_to_compare = df.iloc[0]
similarity_with_other_users = df.corrwith(user_to_compare, axis=1,
                                         method='pearson')
similarity_with_other_users = similarity_with_other_users.sort_values(
   ascending=False)
# Compute correlation between 'The Shawshank Redemption' and other movies
movie_to_compare = df['The Shawshank Redemption']
similarity_with_other_movies = df.corrwith(movie_to_compare, axis=0)
similarity_with_other_movies = similarity_with_other_movies.sort_values(
   ascending=False)

这里采用了pearson correlation coefficient:

解读pandas.DataFrame.corrwith

其中,n是样本的维度,xi和yi分别表示样本每个维度的值,解读pandas.DataFrame.corrwith解读pandas.DataFrame.corrwith表示样本均值。

以user1和user4为例,计算他们之间的相关系数,user1的均值是4,user2的均值是4.2:

解读pandas.DataFrame.corrwith

这个结果与corrwith函数计算的结果一致。

similarity_with_other_users

解读pandas.DataFrame.corrwith

similarity_with_other_movies

解读pandas.DataFrame.corrwith

从结果可以看出,user1和user4的相关性最高,说明他们对每部电影的评分最接近,或者说他们的喜欢电影的类型最接近;《The Shawshank Redemption》和《Forrest Gump》的相关性为1,说明后者的评分和前者最接近。

来源:https://blog.csdn.net/w1301100424/article/details/98473560

标签:pandas,DataFrame,corrwith
0
投稿

猜你喜欢

  • Python3爬虫学习之MySQL数据库存储爬取的信息详解

    2024-01-19 23:06:19
  • PHP实现上传文件并存进数据库的方法

    2023-07-03 21:35:14
  • 解决python opencv无法显示图片的问题

    2021-09-11 13:26:37
  • python多进程程序打包成exe的问题

    2023-08-07 13:39:03
  • Python二维码生成识别实例详解

    2021-06-10 19:59:22
  • 银行账号输入格式化, 支持部分浏缆器

    2007-09-26 18:27:00
  • 使用apiDoc实现python接口文档编写

    2023-10-23 21:28:40
  • 使用Python+selenium实现第一个自动化测试脚本

    2021-01-26 17:52:01
  • Python下划线5种含义代码实例解析

    2023-11-19 04:25:59
  • python cookie反爬处理的实现

    2021-10-16 23:33:17
  • 微信 用脚本查看是否被微信好友删除

    2021-12-30 19:59:42
  • 封装获取dom元素的简单实例

    2024-04-17 09:57:25
  • JS实现长图上下滚动效果

    2023-07-22 10:28:26
  • Sun正式发布MySQL 5.1版 简化数据库应用

    2008-12-11 15:15:00
  • 详细了解 MySQL锁机制

    2010-08-08 09:04:00
  • Pytorch保存模型用于测试和用于继续训练的区别详解

    2021-12-02 08:49:39
  • http状态码一览表以及HTTP响应的返回头信息

    2010-03-31 14:45:00
  • Git配置用户签名方式及原因说明

    2022-06-17 05:54:50
  • python生成以及打开json、csv和txt文件的实例

    2023-08-05 10:44:49
  • 微信跳一跳python代码实现

    2021-09-16 05:35:09
  • asp之家 网络编程 m.aspxhome.com