本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
以下文章來源于Python實用寶典 ,作者:肯德
前言
2014年在密蘇里州一名叫做弗格森(Ferguson)的警察殺害了邁克爾·布朗(Michael Brown)后,美國開始了一場抗議警察暴力對待黑人的運動-Black Lives Matter(黑人的命也是命,簡稱BLM ),
2020年,在明尼阿波利斯警察德里克·喬文殺害喬治·弗洛伊德(喬治·弗洛伊德)之后,BLM運動再次成為頭條新聞,引起國際社會的進一步關注,
自2015年1月1日起,《華盛頓郵報》一直在整理一個資料庫,其中記錄了值班警員在美國發生的每起致命槍擊事件,這個資料庫里包含了死者的種族,年齡和性別,該人是否有武器,以及受害人是否正在遭受精神健康危機,
下面就讓我們來使用這個資料集來進行資料分析,
1.準備
開始之前,你要確保Python和pip已經成功安裝在電腦上,如果你用Python的目的是資料分析,可以直接安裝Anaconda,它內置了Python和pip,
此外,推薦大家用VSCode編輯器,因為它可以在編輯器下面的終端運行命令安裝依賴模塊
本文提供了流程性,建議使用VSCode的Jupiter Notebook擴展,新建一個稱為 test.ipynb 的檔案,跟著教程一步步走下去,
Windows環境下打開Cmd(開始-運行-CMD),蘋果系統環境下請打開Terminal(command +空格輸入Terminal),輸入命令安裝依賴:
所需依賴:
pip install numpy
pip install pandas
pip install plotly
pip install seaborn
本文譯自:https://www.kaggle.com/edoardo10/fatal-police-shooting-eda-plotly-seaborn/data
2.代碼與分析
首先,約會我們分析所需要使用的模塊:
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np from datetime import datetime import plotly.express as px import plotly.graph_objects as go import warnings import plotly.offline as pyo pyo.init_notebook_mode() warnings.filterwarnings('ignore') pd.set_option('display.max_columns', 500) sns.set_style('white') %matplotlib inline
需要分析的資料集:
df = pd.read_csv('./PoliceKillingsUS.csv', encoding='cp1252') df.head()
時間特征
從這6年的月度資料來看,我們可以看到,在2015年上半年,2018年初和2020年第一季度,我們達到了每月超過100起致命事故的高峰,從月度來看,這種現象不明顯的后果,
df['date'] = df['date'].apply(lambda x: pd.to_datetime(x)) df['date'].groupby(df.date.dt.to_period('M')).count().plot(kind='line')
看看警察槍擊案的事故是否具有周末特征:
count = df['date'].apply(lambda x: 'Weekday' if x.dayofweek < 5 else 'Weekend').value_counts(normalize=True) f, ax = plt.subplots(1,1) sns.barplot(x=count.index, y=count.values, ax=ax, palette='twilight')
顯然,我們沒有證據表明周末會發生更多的案件,
不過,如果細化到星期里的每一天,我們會發現周中發生案件的概率較高:
count = df['date'].apply(lambda x: x.dayofweek).value_counts(normalize=True).sort_index() count.index = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'] f, ax = plt.subplots(1,1) sns.barplot(x=count.index, y=count.values, ax=ax, palette='twilight') ax.set_title('Cases (%) for each day of the week');
接下來看看以下4個特征的分布:
精神疾病的征兆:是否精神障礙
威脅等級:威脅等級
body_camera:警察是否帶了隨身攝像頭
way_of_death:死亡方式
count_1 = df['signs_of_mental_illness'].value_counts(normalize=True) count_2 = df['threat_level'].value_counts(normalize=True) count_3 = df['body_camera'].value_counts(normalize=True) count_4 = df['manner_of_death'].value_counts(normalize=True) fig, axes = plt.subplots(2, 2, figsize=(8, 8), sharey=True) sns.barplot(x=count_1.index, y=count_1.values, palette="rocket", ax=axes[0,0]) axes[0,0].set_title('Signs of mental illness (%)') sns.barplot(x=count_2.index, y=count_2.values, palette="viridis", ax=axes[0,1]) axes[0,1].set_title('Threat level (%)') sns.barplot(x=count_3.index, y=count_3.values, palette="nipy_spectral", ax=axes[1,0]) axes[1,0].set_title('Body camera (%)') sns.barplot(x=count_4.index, y=count_4.values, palette="gist_heat", ax=axes[1,1]) axes[1,1].set_title('Manner of death (%)');
我們可以看到,只有20%的案例受害者有精神殘疾的限額;
只有10%的警察有隨身攝像頭;
70%的情況被宣布為危險狀況;
死亡方式似乎不是一個有趣的變數,因為大多數案件都是“槍斃”;
美國的警察是否具有種族主義傾向?
count = df.race.value_counts(normalize=True) count.index = ['White', 'Black', 'Hispanic', 'Asian', 'Native American', 'Other'] f, ax = plt.subplots(1,1, figsize=(8,6)) sns.barplot(y=count.index, x=count.values, palette='Reds_r') ax.set_title('Total cases for each race (%)');
從上圖我們知道,大部分致命的槍擊事件中,涉及最多的是白人,其次是黑人和西班牙裔,
但這個圖表并沒有考慮人種比例,參考2019年美國的種族比例,我們可以看到,美國黑人受害者的比例更高:
資料來源:https ://data.census.gov/cedsci/table?q = Hispanic%20or%20Latino & tid = ACSDP1Y2019.DP05&hidePreview = false
share_race_usa_2019 = pd.Series([60.0, 12.4, 0.9, 5.6, 18.4, 2.7], index=['White','Black','Native American','Asian','Hispanic','Other']) count_races = count / share_race_usa_2019 count_races = count_races.sort_values(ascending=False) f, ax = plt.subplots(1,1, figsize=(8,6)) sns.barplot(y=count_races.index, x=count_races.values, palette='Greens_r') ax.set_title('Total cases for each race on total USA race percentage rate');
受害者的年齡
接下來看看受害者年齡的分布密度圖:
sns.set_style('whitegrid') fig, axes = plt.subplots(1, 1, figsize=(10, 8)) axes.xaxis.set_ticks(np.arange(0,100,10)) sns.kdeplot(df[df.race == 'N'].age, ax=axes, shade=True, color='#7FFFD4') sns.kdeplot(df[df.race == 'O'].age, ax=axes, shade=True, color='#40E0D0') sns.kdeplot(df[df.race == 'B'].age, ax=axes, shade=True, color='#00CED1') sns.kdeplot(df[df.race == 'H'].age, ax=axes, shade=True, color='#6495ED') sns.kdeplot(df[df.race == 'A'].age, ax=axes, shade=True, color='#4682B4') sns.kdeplot(df[df.race == 'W'].age, ax=axes, shade=True, color='#008B8B')
legend = axes.legend_
legend.set_title("Race")
for t, l in zip(legend.texts,("Native", "Other", 'Black', 'Hispanic', 'Asian', 'White')):
t.set_text(l)
由這些疊加的密度圖可以拋光:
對于而言和白人而言,大多數案件的受害者年齡都在30歲左右,
對于其他和印第安人來說,在大多數案件中,受害者大約28歲,
對于西班牙裔和黑人而言,大多數案件的受害者年齡都在25歲左右,
所以我們可以說,西班牙裔美國人和黑人的年輕人,是被警察開槍射擊的高危人群,
受害者性別比例
按常理,這種暴力事件的受害者一般都為男性,看看是不是這樣:
fig = px.pie(values = df.gender.value_counts(normalize=True).values, names=df.gender.value_counts(normalize=True).index, title='Total cases gender (%)') fig.update(layout=dict(title=dict(x=0.5),autosize=False, width=400, height=400)) fig.show()
果然如此,超過95%的受害者都為男性,
簡單的EDA分析就是這些,另外,作者還分享了很多深層次的分析,不過并沒有將資料分享出來,這里就不展示了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/235235.html
標籤:Python
