<jupyter_start><jupyter_text>**GLOBAL TERRORISM ANALYSIS** PART 3: DATA ANALYSIS AND VISUALISATION  Author : <NAME>------ This file contains analysis and vizualisation using Python, for viewing some more advanced analysis and visualizations refer to Global Terrorism Analysis Workbook @ my Tableau Public Profile---**Tableau Public Workbook on** [Global Terrorism Analysis by Samarpan](https://public.tableau.com/profile/samarpan.das!/) Note : You wont have to create an account or sign in to view my workbook, but tableau does sometimes have server issues, so referesh the page a few times in case it doesn't open at once. ------ Importing necessary libraries<jupyter_code>import time
import matplotlib.pyplot as plt 
import matplotlib.ticker as ticker
from matplotlib import animation
import numpy as np
import pandas as pd
pd.options.mode.chained_assignment = None
import seaborn as sns
import plotly.express as px<jupyter_output><empty_output><jupyter_text>Connecting drive to google colab<jupyter_code>from google.colab import drive 
drive.mount('/content/drive')
terror_df = pd.read_csv('/content/drive/My Drive/BaseForAnalysis_Version2.csv', sep=',', encoding="ISO-8859-1")<jupyter_output><empty_output><jupyter_text>Glimpse of the final dataset that will be used to draw the analysis<jupyter_code>terror_df.head(10)<jupyter_output><empty_output><jupyter_text>Cols involved<jupyter_code>terror_df.columns<jupyter_output><empty_output><jupyter_text>Analysis of the numerical figures in the data frame<jupyter_code>terror_df[['nkilled', 'nkillonlyter', 'nwounded', 'propdamageextent', 
        'ncasualties']].describe().transpose()
terror_df.info(verbose = True)<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 201183 entries, 0 to 201182
Data columns (total 31 columns):
 #   Column            Non-Null Count   Dtype  
---  ------            --------------   -----  
 0   Unnamed: 0        201183 non-null  int64  
 1   eventid           201183 non-null  int64  
 2   year              201183 non-null  int64  
 3   month             201183 non-null  int64  
 4   day               201183 non-null  int64  
 5   extended          201183 non-null  int64  
 6   country           201183 non-null  object 
 7   region            201183 non-null  object 
 8   city              200757 non-null  object 
 9   latitude          196556 non-null  float64
 10  longitude         196555 non-null  float64
 11  vicinity          201183 non-null  int64  
 12  crit              201183 non-null  int64  
 13  multiple          201183 non-null  int64  
 14  success           201183 non-null  int64  
 15  suicide           201183 non-null  int64  
 16  attacktype        20[...]<jupyter_text>..... .... Analysis of the number of attacks per year<jupyter_code>f = plt.figure(figsize=(20, 7))

sns.set(font_scale = 1.1)
sns.set_theme(style = "darkgrid")
xaxis = sns.countplot(x = 'year', data = terror_df)
xaxis.set_xticklabels(xaxis.get_xticklabels(), rotation=60)
plt.ylabel('Count', fontsize=12)
plt.xlabel('Year', fontsize=12)
plt.title('Number of Terrorist Attack by Year', fontsize = 12)<jupyter_output><empty_output><jupyter_text>........ ........ Number of Attacks per Region (The globe has been divided into 12 distinct regions as per global standards)<jupyter_code>f = plt.figure(figsize=(16, 8))

sns.set(font_scale=0.7)
sns.countplot(y='region', data=terror_df)
plt.ylabel('Region', fontsize=12)
plt.xlabel('Count', fontsize=12)
plt.title('Number of Terrorist Attack by Region', fontsize=12)<jupyter_output><empty_output><jupyter_text>Number of Attacks per Attack Method<jupyter_code>f = plt.figure(figsize=(20, 8))

sns.set(font_scale=0.8)
sns.countplot(x='attacktype', data=terror_df,)
plt.xlabel('Methods of Attack', fontsize=12)
plt.ylabel('Counts', fontsize=12)
plt.title('Types of Terrorist Attack ', fontsize=12)<jupyter_output><empty_output><jupyter_text>Number of Attacks per Type of Targets<jupyter_code>f = plt.figure(figsize=(20, 8))

sns.set(font_scale=0.8)
xaxis = sns.countplot(x='targettype', data=terror_df,)

xaxis.set_xticklabels(xaxis.get_xticklabels(), rotation=60)
plt.xlabel('Target Types', fontsize=12)
plt.ylabel('Count', fontsize=12)
plt.title('Types of Target', fontsize=12)<jupyter_output><empty_output><jupyter_text>Top 15 Contries with most number of Attacks by terror groups<jupyter_code>fig= plt.figure(figsize=(16, 10))
sns.set(font_scale=0.9)
terror_country = sns.barplot(x=terror_df['country'].value_counts()[0:15].index, y=terror_df['country'].value_counts()[0:15], palette='RdYlGn')
terror_country.set_xticklabels(terror_country.get_xticklabels(), rotation=70)
terror_country.set_xlabel('Country', fontsize=12)
terror_country.set_ylabel('Counts', fontsize=12)
plt.title('Top 15 Countries: Most Attacks by Terrorist Groups', fontsize=12)<jupyter_output><empty_output><jupyter_text>...... ............. ........ ....... Analysis of number of attacks in a region on a particular calender yearThis helps us compare the rise / fall of attacks on a region<jupyter_code>region_year = pd.crosstab(terror_df.year, terror_df.region)

region_year.head(20)
fig = plt.figure(figsize=(16, 10))

color_list_reg_yr = ['palegreen', 'lime', 'green', 'Aqua', 'skyblue', 'darkred', 'darkgray', 'tan', 
                    'orangered', 'plum', 'salmon', 'mistyrose']
region_year.plot(figsize=(14, 10), fontsize=13, color=color_list_reg_yr)
#region_year.plot(figsize=(14, 10), fontsize=13)
plt.xlabel('Year', fontsize=12)
plt.ylabel('Number of Attacks', fontsize=12)
plt.legend(fontsize=12)
plt.title('Number of Attacks per Region by Year', fontsize=12)<jupyter_output><empty_output><jupyter_text>..... Analysis of number of attacks of a particular type on a particular calender yearThis helps us compare the rise / fall of attack types over the years<jupyter_code>attacktype_year = pd.crosstab(terror_df.year, terror_df.attacktype)

attacktype_year.head(20)<jupyter_output><empty_output><jupyter_text>....... Analysis of number of attacks using a particular class of weapons on a particular calender yearThis helps us compare the rise / fall of preferred weapon types for attacks<jupyter_code>fig = plt.figure(figsize=(16, 10))

color_list_reg_yr = ['palegreen', 'lime', 'green', 'Aqua', 'skyblue', 'darkred', 'darkgray', 'tan', 
                    'orangered', 'plum', 'salmon', 'mistyrose']
attacktype_year.plot(figsize=(14, 10), fontsize=13, color=color_list_reg_yr)
#region_year.plot(figsize=(14, 10), fontsize=13)
plt.xlabel('Year', fontsize=12)
plt.ylabel('Number of Attacks', fontsize=12)
plt.legend(fontsize=12)
plt.title('Weapon trends by Year', fontsize=12)<jupyter_output><empty_output><jupyter_text><jupyter_code>organisation_year = pd.crosstab(terror_df.year, terror_df.organisation)

organisation_year.head(20)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1254416961130742, 'fraction_numerical': 0.0443168433451119, 'mean_word_length': 4.675020885547202, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '437417', 'license': 'Apache-2.0', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2254, 'n_words': 557, 'path': 'project/3. Analysis and Visualisations/data_analysis.ipynb', 'repo_name': 'SamarpanDas/Global-Terrorism-Analysis', 'size': '6798'}