Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Rise of Terrorism Project Report

1150 views
Kernel: Python 2 (SageMath)
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import plotly.offline as py import cufflinks as cf from ipywidgets import interact pd.options.mode.chained_assignment = None # Turning off the "SettingWithCopyWarning" df = pd.read_csv('islamic_state.csv') df.head()
df.dtypes
eventid int64 iyear int64 imonth int64 iday int64 country_txt object city object provstate object latitude float64 longitude float64 location object summary object attacktype1 int64 attacktype1_txt object targtype1 int64 targtype1_txt object targsubtype1 float64 targsubtype1_txt object natlty1 float64 natlty1_txt object gname object weaptype1 int64 weaptype1_txt object nkill float64 nwound float64 date object dtype: object
terror_gp = df.sort_values(by = 'country_txt', ascending = False) terror_gp.head()

Excersie 1. Find out which group was the ,most responsible for the Islamic terrorist attacks between 2007 and 2015. Also compare the groups to the targets.

PLace = df[(df.iyear >= 2007) & (df.iyear <=2015) ].sort_values('nkill', ascending = False)[0:100].gname.unique() PLace
array(['Islamic State of Iraq and the Levant (ISIL)', 'Sinai Province of the Islamic State', 'Islamic State of Iraq (ISI)', 'Khorasan Chapter of the Islamic State', 'Sanaa Province of the Islamic State'], dtype=object)
PLaceC= ['Islamic State of Iraq and the Levant (ISIL)','Sinai Province of the Islamic State','Islamic State of Iraq (ISI)', 'Khorasan Chapter of the Islamic State', 'Sanaa Province of the Islamic State'] web2 = df[(df.gname.isin(PLaceC)) & (df.iyear >= 2007) & (df.iyear <=2015)] web2['nkill'] = web2.nkill web2.head()
groups = df.groupby("gname").size() groups
gname Adan-Abyan Province of the Islamic State 11 Algeria Province of the Islamic State 3 Bahrain Province of the Islamic State 1 Barqa Province of the Islamic State 89 Caucasus Province of the Islamic State 1 Fezzan Province of the Islamic State 3 Hadramawt Province of the Islamic State 7 Hijaz Province of the Islamic State 2 Islamic State in Bangladesh 12 Islamic State of Iraq (ISI) 144 Islamic State of Iraq and the Levant (ISIL) 2833 Khorasan Chapter of the Islamic State 78 Lahij Province of the Islamic State 2 Najd Province of the Islamic State 5 Sanaa Province of the Islamic State 29 Shabwah Province of the Islamic State 1 Sinai Province of the Islamic State 172 Supporters of the Islamic State in Jerusalem 9 Supporters of the Islamic State in the Land of the Two Holy Mosques 2 Tripoli Province of the Islamic State 144 dtype: int64
groupsdf = pd.DataFrame(groups.reset_index()) groupsdf.columns = ["gname", "num_attacks"] groupsdf
sns.barplot( x = "num_attacks", y = "gname", data = groupsdf.sort_values("gname", ascending = True))
<matplotlib.axes._subplots.AxesSubplot at 0x7f940c5d1fd0>
Image in a Jupyter notebook
targetdf = pd.DataFrame(groups.reset_index()) targetdf.columns = ["targsubtype1", "num_attacks"] targetdf
targets = ['House/Apartment/Residence','Place of Worship','Unnamed Civilian/Unspecified','Village/City/Town/Suburb','Police Security Forces/Officers'] web2 = df[(df.targsubtype1_txt.isin(targets)) & (df.iyear >= 2007) & (df.iyear <=2015)] web2['num_attacks'] = web2.nkill web2.head()
missess = web2.pivot(index = 'iyear', columns = 'targsubtype1_txt', values = 'num_attacks') missess.plot() plt.ylabel("Popculture names per 1000", fontsize = 12) plt.xlabel("") plt.title("Modernized Names in the World", fontsize = 15) plt.legend(title = "", fontsize = 12)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-115-6204f8301adf> in <module>() ----> 1 missess = web2.pivot(index = 'iyear', columns = 'targsubtype1_txt', values = 'num_attacks') 2 missess.plot() 3 plt.ylabel("Popculture names per 1000", fontsize = 12) 4 plt.xlabel("") 5 plt.title("Modernized Names in the World", fontsize = 15) /projects/sage/sage-7.3/local/lib/python2.7/site-packages/pandas/core/frame.pyc in pivot(self, index, columns, values) 3844 """ 3845 from pandas.core.reshape import pivot -> 3846 return pivot(self, index=index, columns=columns, values=values) 3847 3848 def stack(self, level=-1, dropna=True): /projects/sage/sage-7.3/local/lib/python2.7/site-packages/pandas/core/reshape.pyc in pivot(self, index, columns, values) 330 indexed = Series(self[values].values, 331 index=MultiIndex.from_arrays([index, self[columns]])) --> 332 return indexed.unstack(columns) 333 334 /projects/sage/sage-7.3/local/lib/python2.7/site-packages/pandas/core/series.pyc in unstack(self, level, fill_value) 2041 """ 2042 from pandas.core.reshape import unstack -> 2043 return unstack(self, level, fill_value) 2044 2045 # ---------------------------------------------------------------------- /projects/sage/sage-7.3/local/lib/python2.7/site-packages/pandas/core/reshape.pyc in unstack(obj, level, fill_value) 405 else: 406 unstacker = _Unstacker(obj.values, obj.index, level=level, --> 407 fill_value=fill_value) 408 return unstacker.get_result() 409 /projects/sage/sage-7.3/local/lib/python2.7/site-packages/pandas/core/reshape.pyc in __init__(self, values, index, level, value_columns, fill_value) 99 100 self._make_sorted_values_labels() --> 101 self._make_selectors() 102 103 def _make_sorted_values_labels(self): /projects/sage/sage-7.3/local/lib/python2.7/site-packages/pandas/core/reshape.pyc in _make_selectors(self) 137 138 if mask.sum() < len(self.index): --> 139 raise ValueError('Index contains duplicate entries, ' 140 'cannot reshape') 141 ValueError: Index contains duplicate entries, cannot reshape
sns.countplot(y = "country_txt", data = df, )
<matplotlib.axes._subplots.AxesSubplot at 0x7f940658a6d0>
Image in a Jupyter notebook
sns.countplot(x = "targsubtype1", data = df)
<matplotlib.axes._subplots.AxesSubplot at 0x7f9406a6d290>
Image in a Jupyter notebook
sns.barplot( x = instances , y = groups, data = groups)
sns.set_style('whitegrid') afro = sns.factorplot(data = web2, x = 'iyear', y = 'nkill', hue = 'city', col = 'gname', palette = ['purple', 'red'], col_wrap = 2, markers = '.', join = True, sharey = False, size = 10) afro.set_xticklabels(step = 10) afro.set_axis_labels('Year', 'Number of Deaths caused')

Excercise 2

df.targsubtype1_txt.unique()
array(['Laborer (General)/Occupation Identified', 'Police Security Forces/Officers', 'Government Personnel (excluding police, military)', 'Religion Identified', 'Police Patrol (including vehicles and convoys)', 'Race/Ethnicity Identified', '.', 'Military Personnel (soldiers, troops, officers, forces)', 'Non-State Militia', 'Unnamed Civilian/Unspecified', 'Bus (excluding tourists)', 'Television Journalist/Staff/Facility', 'Military Recruiting Station/Academy', 'School/University/Educational Building', 'Place of Worship', 'Restaurant/Bar/Caf\xc3\xa9', 'Political Party Member/Rally', 'Vehicles/Transportation', 'Politician or Political Party Movement/Meeting/Rally', 'Police Building (headquarters, station, school)', 'Marketplace/Plaza/Square', 'Bank/Commerce', 'Public Area (garden, parking lot, garage, beach, public building, camp)', 'Government Building/Facility/Office', 'Religious Figure', 'Retail/Grocery/Bakery', 'Military Checkpoint', 'Judge/Attorney/Court', 'Highway/Road/Toll/Traffic Signal', 'Intelligence', 'Election-related', 'Other (including online news agencies)', 'Village/City/Town/Suburb', 'Entertainment/Cultural/Stadium/Casino', 'Police Checkpoint', 'Military Unit/Patrol/Convoy', 'Named Civilian', 'Procession/Gathering (funeral, wedding, birthday, religious)', 'Museum/Cultural Center/Cultural House', 'Hotel/Resort', 'Ambulance', 'Prison/Jail', 'Military Barracks/Base/Headquarters/Checkpost', 'Newspaper Journalist/Staff/Facility', 'Gas/Oil', 'Bus Station/Stop', 'Gas', 'International NGO', 'House/Apartment/Residence', 'Medical/Pharmaceutical', 'Teacher/Professor/Instructor', 'Radio Journalist/Staff/Facility', 'Terrorist', 'Bridge/Car Tunnel', 'Military Transportation/Vehicle (excluding convoys)', 'Refugee Camp', 'Military Aircraft', 'Affiliated Institution', 'Electricity', 'Memorial/Cemetery/Monument', 'Domestic NGO', 'Party Office/Facility', 'Airport', 'Student', 'Embassy/Consulate', 'Construction', 'Party Official/Candidate/Other Personnel', 'Farmer', 'Taxi/Rickshaw', 'Train/Train Tracks/Trolley', 'International Organization (peacekeeper, aid agency, compound)', 'Other Personnel', 'Water Supply', 'Diplomatic Personnel (outside of embassy, consulate)', 'Oil', 'Demilitarized Zone (including Green Zone)', 'Protester', 'Military Weaponry', 'Paramilitary', 'Labor Union Related', 'Military Maritime', 'Aircraft (not at an airport)', 'Industrial/Textiles/Factory', 'Tourist', 'Head of State'], dtype=object)
PLace = df[(df.iyear >= 2007) & (df.iyear <=2015) ].sort_values('nkill', ascending = False)[0:100].country_txt.unique() PLace