- Python 資料科學教程
- Python 資料科學 - 首頁
- Python 資料科學 - 入門
- Python 資料科學 - 環境設定
- Python 資料科學 - Pandas
- Python 資料科學 - Numpy
- Python 資料科學 - SciPy
- Python 資料科學 - Matplotlib
- Python 資料處理
- Python 資料操作
- Python 資料清洗
- Python 處理 CSV 資料
- Python 處理 JSON 資料
- Python 處理 XLS 資料
- Python 關係資料庫
- Python NoSQL 資料庫
- Python 日期和時間
- Python 資料整理
- Python 資料聚合
- Python 讀取 HTML 頁面
- Python 處理非結構化資料
- Python 詞法分析
- Python 詞幹提取和詞形還原
- Python 資料視覺化
- Python 圖表屬性
- Python 圖表樣式
- Python 箱線圖
- Python 熱力圖
- Python 散點圖
- Python 氣泡圖
- Python 3D 圖表
- Python 時間序列
- Python 地理資料
- Python 圖資料
Python - 集中趨勢測量
從數學角度來看,集中趨勢是指測量資料集值的位置中心或分佈。它可以幫助瞭解資料集的平均值,以及值在資料集中的分散程度。這反過來有助於評估新輸入融入現有資料集的可能性,從而評估成功的機率。
可以使用 pandas Python 庫中的方法計算三種主要的集中趨勢測量值。
平均值 - 它是資料的平均值,等於值之和除以值的數量。
中位數 - 當值按升序或降序排列時,它是分佈中的中間值。
眾數 - 它是分佈中出現頻率最高的數值。
計算平均值和中位數
可以直接使用 pandas 函式來計算這些值。
import pandas as pd
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack',
'Lee','Chanchal','Gasper','Naviya','Andres']),
'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])}
#Create a DataFrame
df = pd.DataFrame(d)
print "Mean Values in the Distribution"
print df.mean()
print "*******************************"
print "Median Values in the Distribution"
print df.median()
其輸出如下:
Mean Values in the Distribution Age 31.833333 Rating 3.743333 dtype: float64 ******************************* Median Values in the Distribution Age 29.50 Rating 3.79 dtype: float64
計算眾數
根據資料是連續的還是是否存在頻率最高的數值,分佈中可能存在也可能不存在眾數。我們下面使用一個簡單的分佈來找出眾數。這裡我們有一個在分佈中具有最高頻率的值。
import pandas as pd
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack',
'Lee','Chanchal','Gasper','Naviya','Andres']),
'Age':pd.Series([25,26,25,23,30,25,23,34,40,30,25,46])}
#Create a DataFrame
df = pd.DataFrame(d)
print df.mode()
其輸出如下:
Age Name 0 25.0 Andres 1 NaN Chanchal 2 NaN Gasper 3 NaN Jack 4 NaN James 5 NaN Lee 6 NaN Naviya 7 NaN Ricky 8 NaN Smith 9 NaN Steve 10 NaN Tom 11 NaN Vin
廣告