In [520]:
%matplotlib inline
import numpy as np
import pandas as pd
import scipy
import sklearn
import matplotlib.pyplot as plt
import seaborn as sns
import math
from matplotlib.mlab import PCA as mlabPCA
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn import preprocessing
from sklearn.feature_selection import SelectKBest
import seaborn as sns
import scipy.stats as stats
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import cross_val_score, KFold
import matplotlib.pyplot as plt
from sklearn.model_selection import StratifiedKFold
from sklearn.feature_selection import RFECV
from sklearn.datasets import make_classification
from sklearn.feature_selection import RFE
from sklearn.model_selection import cross_val_predict
from sklearn import metrics
from sklearn.decomposition import PCA as sklearn_pca
import locale
from locale import atof
import warnings
from IPython.display import display
from sklearn import linear_model
from sklearn.model_selection import cross_val_score, cross_val_predict
from sklearn.feature_selection import f_regression
import statsmodels.formula.api as smf
from statsmodels.sandbox.regression.predstd import wls_prediction_std
import xlrd
from sklearn import ensemble
import time
In [521]:
# Import FBI Raw Data
fbidata = pd.read_csv('https://raw.githubusercontent.com/Thinkful-Ed/data-201-resources/master/New_York_offenses/NEW_YORK-Offenses_Known_to_Law_Enforcement_by_City_2013%20-%2013tbl8ny.csv', delimiter=",", thousands=',',decimal=".")
fbiraw = pd.DataFrame(fbidata)
fbiraw.head()
Out[521]:
In [522]:
#Transform FBI Raw Data
#Rename columns with row 3 from the original data set
fbiraw_t1 = fbiraw.rename(columns=fbiraw.iloc[3])
#Delete first three rows don´t contain data for the regression model
fbiraw_t2 = fbiraw_t1.drop(fbiraw_t1.index[0:4])
In [523]:
#Delete column "Rape (revised definition)1 as it contains no data
fbiraw_t2 = fbiraw_t2.drop('Rape\n(revised\ndefinition)1', axis = 1)
In [524]:
#Delete Arson Column as there is insufficient data
# 'The FBI does not publish arson data unless it receives data from either the agency or the state
# for all 12 months of the calendar year.'
fbiraw_t2 = fbiraw_t2.drop('Arson3', axis = 1)
In [525]:
#Clean tail from the data set
#Re-shape dataset excluding the last 3 rows of the dataset as they don´t contain relevant information for the model
fbiraw_t2 = fbiraw_t2[:-3]
#Change names in Columns
fbiraw_t2= fbiraw_t2.rename(columns={'Violent\ncrime': 'Violent Crime', 'Murder and\nnonnegligent\nmanslaughter': 'Murder','Rape\n(legacy\ndefinition)2': 'Rape', 'Robbery': 'Robbery', 'Aggravated\nassault': 'Assault', 'Property\ncrime': 'PropertyCrime', 'Burglary': 'Burglary', 'Larceny-\ntheft': 'Larceny & Theft', 'Motor\nvehicle\ntheft': 'MotorVehicleTheft'})
In [526]:
#Analyse missing information
fbiraw_t2.info()
In [527]:
#Change all columns from object to float
locale.setlocale(locale.LC_NUMERIC, '')
fbiraw_t2['Population'] = fbiraw_t2['Population'].apply(atof)
fbiraw_t2['Violent Crime'] = fbiraw_t2['Violent Crime'].apply(atof)
fbiraw_t2['Murder'] = fbiraw_t2['Murder'].apply(atof)
fbiraw_t2['Rape'] = fbiraw_t2['Rape'].apply(atof)
fbiraw_t2['Robbery'] = fbiraw_t2['Robbery'].apply(atof)
fbiraw_t2['Assault'] = fbiraw_t2['Assault'].apply(atof)
fbiraw_t2['PropertyCrime'] = fbiraw_t2['PropertyCrime'].apply(atof)
fbiraw_t2['Burglary'] = fbiraw_t2['Burglary'].apply(atof)
fbiraw_t2['Larceny & Theft'] = fbiraw_t2['Larceny & Theft'].apply(atof)
fbiraw_t2['MotorVehicleTheft'] = fbiraw_t2['MotorVehicleTheft'].apply(atof)
fbiraw_t2.info()
In [528]:
#Reindex the dataframe
fbiraw_t3 = fbiraw_t2.reset_index(drop=True)
fbiraw_t2.head()
Out[528]:
In [529]:
#Extract only the columns that are needed
fbiraw_t3 = fbiraw_t2
In [530]:
#Convert Robbery into a categorical feature
fbiraw_t3.loc[fbiraw_t3['Robbery'] > 0, 'Robbery'] = 1
In [531]:
#Convert Murder into a categorical feature
fbiraw_t3.loc[fbiraw_t3['Murder'] > 0, 'Murder'] = 1
In [532]:
#Transform dataset into final dataset with features
fbidata = fbiraw_t3.drop('City',axis=1)
In [533]:
# Set up the Random Forest Model
rfc = ensemble.RandomForestClassifier()
X = fbidata.drop(['Robbery'], 1)
Y = fbidata['Robbery']
X = pd.get_dummies(X)
In [534]:
# Make the correlation matrix
corrmat = X.corr()
print(corrmat)
In [535]:
# Set up the timer to calculate simplicity of the Random Forest
start_time = time.clock()
In [536]:
# Calculate accuracy of the Random Forest
cross_val_score(rfc, X, Y, cv=20).mean()
Out[536]:
In [537]:
# Calculate Simplicity of the Random Forest
trfc = time.clock() - start_time
print (trfc, "seconds")
In [538]:
# Set up the timer to calculate simplicity of the tree
start_time1 = time.clock()
In [539]:
# This is the model we'll be using.
from sklearn import tree
# A convenience for displaying visualizations.
from IPython.display import Image
# Packages for rendering our tree.
import pydotplus
import graphviz
# Initialize and train our tree.
decision_tree = tree.DecisionTreeClassifier(
criterion='entropy',
max_features=1,
max_depth=4,
random_state = 0
)
decision_tree.fit(X, Y)
# Calculate accuracy of the tree
cross_val_score(decision_tree, X, Y, cv=20).mean()
Out[539]:
In [540]:
# Calculate Simplicity of Tree
ttree = time.clock() - start_time1
print (ttree, "seconds")
In [541]:
#Print Simplicity of Random Forest and Tree
print('Simplicity Random Forest:', trfc)
print('Simplicity Tree:', ttree)
ratio = trfc/ttree
print('Ratio Random Forest/Tree:', ratio)
In [542]:
# Render our tree.
dot_data = tree.export_graphviz(
decision_tree, out_file=None,
feature_names=X.columns,
class_names=['High', 'Low'],
filled=True
)
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())
Out[542]:
In [ ]: