解讀pandas.DataFrame.corrwith
解讀pandas.DataFrame.corrwith
pandas.DataFrame.corrwith用于計(jì)算DataFrame中行與行或者列與列之間的相關(guān)性。
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’ 表示計(jì)算列與列的相關(guān)性,axis=1或者axis=‘columns’ 表示計(jì)算行與行的相關(guān)性。
method是計(jì)算相關(guān)性的方法,這里采用pearson correlation coefficient(皮爾遜相關(guān)系數(shù))。
下面以一個(gè)觀眾對(duì)電影評(píng)分的例子說(shuō)明
每一行表示一個(gè)觀眾對(duì)所有電影的評(píng)分,每一列表示所有觀眾對(duì)一部電影的評(píng)分。
然后分別計(jì)算第一位觀眾和其他觀眾的相關(guān)性 和第一部電影和其它電影的相關(guān)性。
代碼如下
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:
其中,n是樣本的維度,xi和yi分別表示樣本每個(gè)維度的值,和
表示樣本均值。
以u(píng)ser1和user4為例,計(jì)算他們之間的相關(guān)系數(shù),user1的均值是4,user2的均值是4.2:
這個(gè)結(jié)果與corrwith函數(shù)計(jì)算的結(jié)果一致。
similarity_with_other_users
similarity_with_other_movies
從結(jié)果可以看出,user1和user4的相關(guān)性最高,說(shuō)明他們對(duì)每部電影的評(píng)分最接近,或者說(shuō)他們的喜歡電影的類(lèi)型最接近;《The Shawshank Redemption》和《Forrest Gump》的相關(guān)性為1,說(shuō)明后者的評(píng)分和前者最接近。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
超全Python圖像處理講解(多模塊實(shí)現(xiàn))
這篇文章主要介紹了超全Python圖像處理講解(多模塊實(shí)現(xiàn)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04python3爬蟲(chóng)之設(shè)計(jì)簽名小程序
這篇文章主要為大家詳細(xì)介紹了python3爬蟲(chóng)之寫(xiě)為朋友設(shè)計(jì)簽名的小程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06tensorflow saver 保存和恢復(fù)指定 tensor的實(shí)例講解
今天小編就為大家分享一篇tensorflow saver 保存和恢復(fù)指定 tensor的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Python+Selenium實(shí)現(xiàn)表單自動(dòng)填充和提交
你是不是也厭倦了每天重復(fù)表單填寫(xiě)的工作,是時(shí)候讓技術(shù)來(lái)幫助我們解放雙手了,下面小編就為大家介紹一下如何使用Selenium和Python來(lái)自動(dòng)填充和提交表單2023-09-0920個(gè)超實(shí)用Python自動(dòng)化腳本分享
在當(dāng)今的快節(jié)奏工作環(huán)境中,自動(dòng)化不再是一種奢侈,而是提高效率和精確性的必需手段,這篇文章為大家整理了20個(gè)超實(shí)用Python自動(dòng)化腳本,希望對(duì)大家有所幫助2024-01-01pip matplotlib報(bào)錯(cuò)equired packages can not be built解決
這篇文章主要介紹了pip matplotlib報(bào)錯(cuò)equired packages can not be built解決,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01