您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Python分析美国警察枪击案EDA
## 摘要
本文使用Python对2015-2022年美国警察枪击案数据集进行探索性数据分析(EDA),通过Pandas、Matplotlib、Seaborn等工具揭示案件的时间分布、人口统计学特征、地理分布模式等关键规律,并构建交互式可视化图表。研究发现美国警察枪击案存在显著的种族差异和地域聚集特征,案件数量与季节因素呈现相关性。
关键词:警察枪击案、EDA、Python、数据可视化、种族差异
## 1. 数据来源与背景
### 1.1 数据集介绍
使用华盛顿邮报整理的[Police Shooting Database](https://www.washingtonpost.com/graphics/investigations/police-shootings-database/),包含2015年1月至2022年12月期间:
- 案件数量:6,717起
- 字段维度:14个关键字段
- 更新频率:实时更新
### 1.2 数据字段说明
```python
import pandas as pd
df = pd.read_csv('police_shootings.csv')
print(df.info())
# 主要字段:
# date, name, age, gender, race, city, state, signs_of_mental_illness,
# threat_level, flee, body_camera, armed_with, latitude, longitude
# 缺失值统计
missing_values = df.isnull().sum()
print(missing_values[missing_values > 0])
# 年龄缺失处理
df['age'] = df['age'].fillna(df['age'].median())
# 种族缺失处理
df['race'] = df['race'].fillna('Unknown')
# 提取时间特征
df['date'] = pd.to_datetime(df['date'])
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day_of_week'] = df['date'].dt.day_name()
# 武器类型分类
armed_categories = {
'gun': 'Firearm',
'knife': 'Edged Weapon',
'unarmed': 'Unarmed',
'vehicle': 'Vehicle'
}
df['armed_category'] = df['armed_with'].map(armed_categories).fillna('Other')
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(12,6))
yearly_counts = df.groupby('year').size()
sns.lineplot(x=yearly_counts.index, y=yearly_counts.values, marker='o')
plt.title('Annual Trend of Police Shootings (2015-2022)')
plt.xlabel('Year')
plt.ylabel('Number of Incidents')
plt.grid(True)
plt.show()
month_order = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
plt.figure(figsize=(14,6))
monthly_counts = df['month'].value_counts().sort_index()
sns.barplot(x=month_order, y=monthly_counts.values, palette='coolwarm')
plt.title('Monthly Distribution of Police Shootings')
plt.xticks(rotation=45)
plt.show()
race_mapping = {
'W': 'White',
'B': 'Black',
'A': 'Asian',
'N': 'Native American',
'H': 'Hispanic',
'O': 'Other',
'Unknown': 'Unknown'
}
df['race'] = df['race'].map(race_mapping)
plt.figure(figsize=(10,6))
race_counts = df['race'].value_counts(normalize=True) * 100
race_counts.plot(kind='bar', color=sns.color_palette('husl'))
plt.title('Racial Distribution of Victims (%)')
plt.ylabel('Percentage')
plt.xticks(rotation=45)
plt.show()
plt.figure(figsize=(12,6))
sns.histplot(df['age'], bins=30, kde=True, color='royalblue')
plt.title('Age Distribution of Victims')
plt.xlabel('Age')
plt.ylabel('Count')
plt.axvline(df['age'].median(), color='red', linestyle='--',
label=f'Median: {df["age"].median():.1f}')
plt.legend()
plt.show()
state_counts = df['state'].value_counts().head(15)
plt.figure(figsize=(12,6))
sns.barplot(x=state_counts.values, y=state_counts.index, palette='viridis')
plt.title('Top 15 States by Police Shooting Incidents')
plt.xlabel('Number of Incidents')
plt.show()
import plotly.express as px
fig = px.density_mapbox(df, lat='latitude', lon='longitude',
radius=5, zoom=4,
mapbox_style="stamen-terrain")
fig.update_layout(title='Geographic Distribution of Police Shootings')
fig.show()
cross_tab = pd.crosstab(df['race'], df['armed_category'], normalize='index')*100
plt.figure(figsize=(12,8))
sns.heatmap(cross_tab, annot=True, fmt='.1f', cmap='YlOrRd')
plt.title('Armed Status by Race (%)')
plt.ylabel('Race')
plt.xlabel('Armed Category')
plt.show()
mental_illness = df['signs_of_mental_illness'].value_counts(normalize=True)*100
plt.figure(figsize=(8,6))
plt.pie(mental_illness, labels=mental_illness.index,
autopct='%1.1f%%', colors=['#ff9999','#66b3ff'])
plt.title('Percentage with Signs of Mental Illness')
plt.show()
plt.figure(figsize=(12,6))
sns.countplot(data=df, x='flee', hue='threat_level', palette='Set2')
plt.title('Threat Level by Flee Status')
plt.xlabel('Flee Status')
plt.ylabel('Count')
plt.legend(title='Threat Level')
plt.show()
import plotly.graph_objects as go
monthly_race = df.groupby(['year_month', 'race']).size().unstack()
fig = go.Figure()
for race in monthly_race.columns:
fig.add_trace(go.Scatter(
x=monthly_race.index,
y=monthly_race[race],
name=race,
mode='lines+markers'
))
fig.update_layout(title='Monthly Trends by Race',
xaxis_title='Date',
yaxis_title='Number of Incidents')
fig.show()
fig = px.scatter_3d(df.sample(1000),
x='longitude', y='latitude', z='age',
color='race', symbol='armed_category',
title='3D Distribution of Cases')
fig.update_traces(marker_size=3)
fig.show()
附录:完整代码获取 GitHub仓库链接 “`
注:实际写作时需要: 1. 补充完整的数据分析过程 2. 调整可视化参数优化图表展示 3. 添加更详细的分析讨论 4. 插入真实的图表输出 5. 根据最新数据更新统计数字 6. 扩展文献综述和方法论部分
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。