jm4.py-20180918

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 17 12:46:07 2018

@author: vicky
"""

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import seaborn as sns
import numpy as np
plt.style.use('fivethirtyeight')
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.tools as tls
from mpl_toolkits.basemap import Basemap
import folium
import folium.plugins
from matplotlib import animation,rc
import io
import base64
from IPython.display import HTML, display
import warnings
from scipy.misc import imread
import codecs
from subprocess import check_output
from pandas.core.frame import DataFrame


df = pd.read_excel('/Users/vicky/Desktop/建模/附件1.xlsx')

col_t=['iyear','imonth','iday','country_txt','region_txt','attacktype1_txt','target1','nkill','nwound','summary','gname',
       'targtype1_txt','weaptype1_txt','motive']
terror=df[col_t]
terror.rename(columns={'iyear':'Year','imonth':'Month','iday':'Day','country_txt':'Country','region_txt':'Region',
  'attacktype1_txt':'AttackType','target1':'Target','nkill':'Killed','nwound':'Wounded','summary':'Summary',
  'gname':'Group','targtype1_txt':'Target_type','weaptype1_txt':'Weapon_type','motive':'Motive'},inplace=True)
terror['casualities']=terror['Killed']+terror['Wounded']

terror.head(3)
terror.isnull().sum()

#terror=terror[terror['Year'] > 2014]

#各年总数
plt.subplots(figsize=(10,6))
sns.countplot('Year',data=terror,palette='RdYlGn_r',edgecolor=sns.color_palette('dark',7))#计数图
plt.xticks(rotation=90)
plt.title('Number Of Terrorist Activities Each Year')
plt.show()
num=DataFrame(terror['Year'].value_counts())
num.plot(color=sns.color_palette('Set2',12))
num2=pd.concat([DataFrame(num)],axis=1)
DataFrame(num).plot(color=sns.color_palette('Set2',12))
num3=DataFrame(num_for.num,index=range(1998,2021),dtype=int)
num3.num=num_for.num

##各年分地区总数
#plt.subplots(figsize=(10,6))
#sns.countplot('Region',data=terror,palette='RdYlGn',edgecolor=sns.color_palette('dark',7),order=terror['Region'].value_counts().index)
#plt.xticks(rotation=90)
#plt.title('Number Of Terrorist Activities By Region')
#plt.show()

#各年分地区数量
terror_region=pd.crosstab(terror.Year,terror.Region)
terror_region.plot(color=sns.color_palette('Set2',12))
fig=plt.gcf()
fig.set_size_inches(10,6)
plt.show()

#各年top恐怖组织数量
top_groups10=terror[terror['Group'].isin(terror['Group'].value_counts()[1:11].index)]
group10=pd.crosstab(top_groups10.Year,top_groups10.Group)
group10.plot(color=sns.color_palette('Paired',10))
fig=plt.gcf()
fig.set_size_inches(10,6)
plt.show()

r=region_f.ix[20:25,1:]
r2=pd.concat([group10,r],axis=0)
r2.index=range(1998,2023)
r2.plot(color=sns.color_palette('Paired',10))








 

你可能感兴趣的:(py-建模比赛-罪犯统计-聚类)