In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Python Analysis of EDA of US Police Shooting Case". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Python Analysis of US Police Shooting EDA"!
1. ready
Before you start, make sure Python and pip are installed successfully on your computer. If you are using Python for data analysis, you can install Anaconda directly, which has Python and pip built in.
In addition, VSCode editor is recommended because it can install dependent modules by running commands in the terminal below the editor.
This article provides a process, it is recommended to use VSCode's Jupiter Notebook extension, create a new file called test.ipynb, and follow the tutorial step by step.
Open Cmd (Start-Run-CMD) in Windows environment, open Terminal (command + space input Terminal) in Apple system environment, enter command to install dependency:
Dependency required:
pip install numpyip install pandaspip install plotlyip install seaborn2. Code and analysis
First of all, dating we analyze the modules that need to be used:
import pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsimport numpy as npfrom datetime import datetimeimport plotly.express as pximport plotly.graph_objects as goimport warningsimport plotly.offline as pyopyo.init_notebook_mode()warnings.filterwarnings('ignore')pd.set_option('display.max_columns', 500)sns.set_style('white')%matplotlib inline
Data sets to be analyzed:
df = pd.read_csv('./ PoliceKillingsUS.csv', encoding='cp1252')df.head()
temporal features
Looking at monthly data for these six years, we can see that in the first half of 2015, early 2018 and the first quarter of 2020, we reached a peak of more than 100 fatal accidents per month. From a monthly perspective, the consequences of this phenomenon are not obvious.
df['date'] = df['date'].apply(lambda x: pd.to_datetime(x))df['date'].groupby(df.date.dt.to_period('M')).count().plot(kind='line')
See if police shootings have weekend characteristics:
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 (%)');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_2019count_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)From these superimposed density maps can be polished:
For both white and white people, the victims in most cases were around 30 years old.
For other and Indian people, in most cases the victim is about 28 years old.
For Hispanics and blacks, most of the victims were in their mid-20s.
So we can say that Hispanic Americans and black young people are at high risk of being shot by police.
Sex ratio of victims
It is common sense that the victims of such violence are generally male. Let's see if this is the case:
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()
At this point, I believe everyone has a deeper understanding of "Python Analysis of EDA of US Police Shooting Case". Let's do it in practice! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.