# 第十二週筆記
## 辨識程式碼:
```
import pandas as pd #Pandas:處理單維度或單一欄位的資料
import numpy as np #Numpy:產生一維、二維陣列進行向量和矩陣運算
import matplotlib.pyplot as plt #matplotlib:讓資料視覺化
#讀取雲端硬碟中的excel檔
dfIncome = pd.read_excel('/content/drive/My Drive/___DataSet/001_Income_F.xlsx')
dfIncome
```
```
#總收入與人數
sum=dfIncome['Income'].sum() #計算Pandas DataFrame列的元素總和
n=dfIncome['Income'].count() #計算Pandas DataFrame列的數量
print(f'總和={sum}, 樣本數={n}')
#直方圖
plt.hist(dfIncome['Income'], bins=range(0, 100000, 1000)) #range:是可選參數,為箱子的上下限
plt.show()
```
```
dfLorenz=dfIncome[:] #選取Pandas DataFrame列的元素
se=dfLorenz['Income'].sort_values() #排列Pandas DataFrame列的元素
cumulativeSum=0
gini=0
i=0
# 宣告xx為串列變數
xx=[]
# 宣告yy為串列變數
yy=[]
for x in se:
i=i+1
cumulativeSum+=x
xx.append(i/n)
yy.append(cumulativeSum/sum)
plt.plot([0,1], linestyle = 'dotted') #(x座標串列值,y座標串列值,其餘參數值)
#散佈圖
plt.scatter(x=xx,y=yy,s=0.1) #(x座標值,y座標值,標示大小)
plt.axis('square')
plt.xlim(0,1) #x軸的範圍
plt.ylim(0,1) #y軸的範圍
print('羅倫茲曲線(Lorenz curve)')
plt.show()
```
## 操弄程式碼:
```
import pandas as pd #Pandas:處理單維度或單一欄位的資料
import numpy as np #Numpy:產生一維、二維陣列進行向量和矩陣運算
import matplotlib.pyplot as plt #matplotlib:讓資料視覺化
#讀取雲端硬碟中的excel檔
dfIncome = pd.read_excel('/content/drive/My Drive/___DataSet/收入分布.xlsx')
dfIncome
```
結果:

```
#總收入與人數
sum=dfIncome['Income'].sum() #計算Pandas DataFrame列的元素總和
n=dfIncome['Income'].count() #計算Pandas DataFrame列的數量
print(f'總和={sum}, 樣本數={n}')
#直方圖
plt.hist(dfIncome['Income'], bins=range(0, 30, 5)) #range:是可選參數,為箱子的上下限
plt.show()
```
結果:

```
dfLorenz=dfIncome[:] #選取Pandas DataFrame列的元素
se=dfLorenz['Income'].sort_values() #排列Pandas DataFrame列的元素
cumulativeSum=0
gini=0
i=0
# 宣告xx為串列變數
xx=[]
# 宣告yy為串列變數
yy=[]
for x in se:
i=i+1
cumulativeSum+=x
xx.append(i/n)
yy.append(cumulativeSum/sum)
plt.plot([0,1], linestyle = 'dotted') #(x座標串列值,y座標串列值,其餘參數值)
#散佈圖
plt.scatter(x=xx,y=yy,s=10) #(x座標值,y座標值,標示大小)
plt.axis('square')
plt.xlim(0,1) #x軸的範圍
plt.ylim(0,1) #y軸的範圍
print('羅倫茲曲線(Lorenz curve)')
plt.show()
```
