import json
import folium
import pandas as pd
import numpy as np
crime_anal_norm = pd.read_csv("../data/02. crime_in_Seoul_final.csv", index_col=0, encoding="utf-8")
geo_path="../data/02. skorea_municipalities_geo_simple.json"
geo_str=json.load(open(geo_path, encoding="utf-8"))
my_map= folium.Map(
location=[37.5502, 126.982],
Zoom_start=11,
tiles="Stamen Toner"
)
folium.Choropleth(
geo_data=geo_str, #우리나라 경계선 좌표값이 담긴 데이터
data=crime_anal_norm["살인"],
columns=[crime_anal_norm.index, crime_anal_norm["살인"]],
key_on="feature.id",
fill_color="PuRd",
fill_opacity=1,
line_opacity=1,
legend_name="정규화된 살인 발생 건수"
).add_to(my_map)
my_map
my_map= folium.Map(
location=[37.5502, 126.982],
Zoom_start=11,
tiles="Stamen Toner"
)
folium.Choropleth(
geo_data=geo_str, #우리나라 경계선 좌표값이 담긴 데이터
data=crime_anal_norm["강간"],
columns=[crime_anal_norm.index, crime_anal_norm["강간"]],
key_on="feature.id",
fill_color="PuRd",
fill_opacity=1,
line_opacity=1,
legend_name="정규화된 강간 발생 건수"
).add_to(my_map)
my_map
my_map= folium.Map(
location=[37.5502, 126.982],
Zoom_start=11,
tiles="Stamen Toner"
)
folium.Choropleth(
geo_data=geo_str, #우리나라 경계선 좌표값이 담긴 데이터
data=crime_anal_norm["범죄"],
columns=[crime_anal_norm.index, crime_anal_norm["범죄"]],
key_on="feature.id",
fill_color="PuRd",
fill_opacity=1,
line_opacity=1,
legend_name="정규화된 5대 범죄 발생 건수"
).add_to(my_map)
my_map
tmp_criminal = crime_anal_norm["범죄"] / crime_anal_norm["인구수"]
my_map= folium.Map(
location=[37.5502, 126.982],
Zoom_start=11,
tiles="Stamen Toner"
)
folium.Choropleth(
geo_data=geo_str, #우리나라 경계선 좌표값이 담긴 데이터
data=tmp_criminal,
columns=[crime_anal_norm.index, tmp_criminal],
key_on="feature.id",
fill_color="PuRd",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="인구대비 범죄 발생 건수"
).add_to(my_map)
my_map
#경찰서별 정보를 범죄발생과 함께 정리
crime_anal_station=pd.read_csv("../data/02. crime_station_raw.csv", encoding="utf-8")
col = ["살인검거", "강도검거", "강간검거", "절도검거", "폭력검거"]
tmp = crime_anal_station[col] / crime_anal_station[col].max() #정규화
crime_anal_station["검거"]=np.mean(tmp, axis=1)#가로 평균
# folium
my_map = folium.Map(
location=[37.5502, 126.982], zoom_start=12
)
# 지역별 범죄 Choropleth
folium.Choropleth(
geo_data=geo_str,
data=crime_anal_norm["범죄"],
columns=[crime_anal_norm.index, crime_anal_norm["범죄"]],
key_on="feature.id",
fill_color="PuRd",
fill_opacity=0.7,
line_opacity=0.2,
).add_to(my_map)
# 경찰서별 검거 CircleMarker
for idx, rows in crime_anal_station.iterrows():
folium.CircleMarker(
location=[rows["lat"], rows["lng"]],
radius = rows["검거"] * 50,
popup=rows["구분"] + ":" + "%.2f"%rows["검거"],
color="#3186cc",
fill=True,
fill_color="#3186cc"
).add_to(my_map)
my_map
# 데이터 처리
crime_loc_raw=pd.read_csv("../data/02. crime_in_Seoul_location.csv", thousands=',', encoding="euc-kr")
# 데이터 확인
crime_loc_raw.info()
crime_loc_raw.범죄명.unique()
crime_loc_raw["장소"].unique()
# DataFrame pivot_table
crime_loc = crime_loc_raw.pivot_table(
crime_loc_raw, index="장소", columns="범죄명", aggfunc=[np.sum]
)
# MultiIndex 해결
crime_loc.columns=crime_loc.columns.droplevel([0, 1])
col = ["살인", "강도", "강간", "절도", "폭력"]
crime_loc_norm = crime_loc / crime_loc.max() #정규화
crime_loc_norm["종합"]=np.mean(crime_loc_norm, axis=1)
#heatmap
import matplotlib.pyplot as plt
from matplotlib import rc
import seaborn as sns
rc("font", family="Arial Unicode MS")
%matplotlib inline
crime_loc_norm_sorts=crime_loc_norm.sort_values("종합", ascending=False) #내림차순
def drawGraph():
plt.figure(figsize=(10, 10))
sns.heatmap(
crime_loc_norm_sorts,
annot=True,
fmt='f',
linewidth=0.5,
cmap="RdPu")
plt.title("범죄 발생 장소")
plt.show()
drawGraph()
Reference
1) 제로베이스 데이터스쿨 강의자료
2) folium