本文來自這里
冪律分布
- 在我們的日常生活中Power Law(冪次分布,Power-law Distributions)是常見的一個數(shù)學(xué)模型麻献,如二八原則们妥。這個世界上是20%的人掌握80%的人的金錢去經(jīng)營,20%的人口擁有80%的財富勉吻,20%的上市公司創(chuàng)造了80%的價值监婶,80%的收入來自20%的商品,80%的利潤來自20%的顧客等等餐曼。
為什么會有這樣的差別呢压储?
- 這是因為時間的乘積效應(yīng)鲜漩,智力上的微弱優(yōu)勢源譬,乘以時間集惋,就會得到價值(財富)幾何級的增長。經(jīng)濟(jì)學(xué)財富分布滿足Pareto Power law tail分布踩娘,語言中有詞頻的冪律分布刮刑,城市規(guī)模和數(shù)量滿足冪律分布,音樂中有f分之1噪音(冪律分布)养渴。通常人們理解冪律分布就是所謂的馬太效應(yīng)雷绢,二八原則,即少數(shù)人聚集了大量的財富理卑,而大多數(shù)人的財富數(shù)量都很小翘紊,因為勝者通吃的原則。
時間間隔分布胖尾圖
核心步驟如下
- 收集數(shù)據(jù)藐唠,我還是用的excel
- 對評論時間數(shù)組進(jìn)行排序帆疟,然后依次獲取兩兩評論時間的時間間隔
- 通過函數(shù)計算myset內(nèi)容的無重復(fù)項,并統(tǒng)計每個時間間隔出現(xiàn)的頻次
- 最后繪制Pow-low冪律分布圖
代碼
# -*- coding: utf-8 -*-
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.font_manager
# 讀取數(shù)據(jù)
df = pd.read_csv("data1.csv", encoding='GB18030')
# 處理數(shù)據(jù)
t = df.values.tolist()
PLTimeList = [] # 評論時間列表
Period = [] # 時間間隔
PeriodSeconds = [] # 時間間隔秒
for i in t:
PLTimeList.append(datetime.strptime(i[5], "%Y-%m-%d %H:%M:%S"))
PLTimeList.sort() # 時間排序
PLTimeList.reverse() # 列表中元素反向
# 獲取時間間隔再賦值給列表
for i in range(0, len(PLTimeList) - 1):
# print(PLTimeList[i])
cnt = (PLTimeList[i] - PLTimeList[i + 1])
Period.append(cnt)
# 獲取秒
for i in Period:
PeriodSeconds.append(i.seconds)
# myset是另外一個列表,里面的內(nèi)容是mylist里面的無重復(fù)項
x = []
y = []
myset = set(PeriodSeconds)
for item in myset:
x.append(item)
y.append(PeriodSeconds.count(item)) # 通過已過濾的時間統(tǒng)計之前PeriodSeconds中的出現(xiàn)次數(shù)作為y,為數(shù)量總數(shù)量
# 繪圖顯示中文字體和負(fù)號
plt.rcParams['font.sans-serif'] = ['SimHei']
myfont = matplotlib.font_manager.FontProperties(fname='C:/Windows/Fonts/msyh.ttf')
plt.rcParams['axes.unicode_minus'] = False
font1 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 26}
# plt.subplot(111)
plt.plot(x, y, 'ko')
plt.yscale('log')
plt.ylabel('P', font1)
plt.xlabel('timespan', font1)
plt.xscale('log')
plt.ylim(0.5, 20)
plt.show()