<jupyter_start><jupyter_text>Georgetown CCPE Data Science Program Team CDWT Created:  May 2016 Copyright (C) 2016 For license information, see LICENSE.txt  We would like to answer is to determine if african american victims are more likely to be killed, given the officer race, officer age, and officer gender.<jupyter_code>import pandas as pd
import numpy as np
aulvindy = pd.read_csv('READ_IN_YOUR.csv')
aulvindy.tail(1)<jupyter_output><empty_output><jupyter_text>Dataset summary visualization<jupyter_code>%matplotlib inline
import seaborn as sns
sns.set()
sns.pairplot(aulvindy, hue="victim_race")<jupyter_output><empty_output><jupyter_text>Drop unused columns<jupyter_code>aulvindy.drop(["UniqueID","victim_gender","victim_age","Police_Department"],
                        axis=1,inplace=True)
aulvindy.tail(1)
# Check for missing values for age
X = aulvindy["officer_age"].reshape(-1, 1)
# Assign the mean value
from sklearn.preprocessing import Imputer
dataimp = Imputer(missing_values='NaN', strategy='mean',verbose=1,axis=0).fit_transform(X)
aulvindy.officer_age = dataimp
aulvindy.tail(1)
# Encode victim_race as 1 for african american or 0 for other races
def victim_race_encode(x):
    if x=='Black':
        return 1
    else: 
        return 0
aulvindy['victim_race'] = aulvindy['victim_race'].apply(victim_race_encode)
aulvindy.tail(1)
# Encode officer_gender as 1 for male or 0 for female
def officer_gender_encode(x):
    if x=='Male':
        return 1
    else: 
        return 0
aulvindy['officer_gender'] = aulvindy['officer_gender'].apply(officer_gender_encode)
aulvindy.tail(1)
X = aulvindy.officer_race.reshape(-1, 1)
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelEncoder
X = aulvindy.officer_race.reshape(-1, 1)
enc = LabelEncoder()
label_encoder = enc.fit(X)
label_encoder.classes_
integer_classes = label_encoder.transform(label_encoder.classes_).reshape(4,1)
enc = OneHotEncoder()
one_hot_encoder = enc.fit(integer_classes)
                          
# First, convert classes to 0-(N-1) integers using labelencoder
num_of_rows = len(aulvindy.index)

t = label_encoder.transform(X).reshape(num_of_rows, 1)

# Second, create a sparse matrix with three columns, each one indicating if the instance belongs to the class
new_features = one_hot_encoder.transform(t)

# Add the new features to titanix_X
X = np.concatenate([X, new_features.toarray()], axis = 1)

#Eliminate converted columns
X = np.delete(X, [0], 1)
# Update officer race names
feature_names = ['OfficerRaceAsian', 'OfficerRaceBlack', 'OfficerRaceHispanic', 'OfficerRaceWhite']
#convert X back to dataframe
X_df = pd.DataFrame(X)
        
X_df.columns = feature_names
X_df.head()
aulvindy.head(1)
y = aulvindy.victim_race
aulvindy.drop(["victim_race","officer_race"],axis=1,inplace=True)
X = pd.concat([X_df, aulvindy], axis=1)
X = X.astype('float64', raise_on_error = False)
y = y.astype(float)

#from sklearn.preprocessing import StandardScaler
#scalar = StandardScaler()
#X = scalar.fit_transform(X)
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=33)<jupyter_output><empty_output><jupyter_text>Decision  Tree<jupyter_code>from sklearn import tree
clf = tree.DecisionTreeClassifier(criterion='entropy',
                                  max_depth=3,min_samples_leaf=5)
clf = clf.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>We would like to answer is to determine if african american victims are more likely to be killed, given the officer race, officer age, and officer gender.<jupyter_code>import pydot,StringIO
dot_data = StringIO.StringIO() 
tree.export_graphviz(clf, out_file=dot_data,
                     feature_names=['officer_Asian','officer_Black','officer_Hispanic','officer_White','officer_gender','officer_age']) 
graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
graph.write_png('/home/dwaynej/Dropbox/Georgetown Data Science/Capstone/Data/decisiontreepd.png') 
from IPython.core.display import Image 
Image(filename='/home/dwaynej/Dropbox/Georgetown Data Science/Capstone/Data/decisiontreepd.png')


y_pred = clf.predict(X_test)
from sklearn.metrics import precision_recall_fscore_support
precision_recall_fscore_support(y_test, y_pred, average='binary')<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
lrmodel = LogisticRegression()
#Train Model
lrmodel.fit(X_train,y_train)
y_lrpred = lrmodel.predict(X_test)
precision_recall_fscore_support(y_test, y_lrpred, average='binary')<jupyter_output><empty_output><jupyter_text>SVC<jupyter_code>from sklearn.svm import SVC
svcmodel = SVC()
svcmodel.fit(X_train, y_train)
y_svcpred = svcmodel.predict(X_test)
precision_recall_fscore_support(y_test, y_svcpred, average='binary')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1039624183006536, 'fraction_numerical': 0.010212418300653595, 'mean_word_length': 6.29806259314456, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '104382', 'license': 'MIT', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1685, 'n_words': 355, 'path': 'cdwtmachinelearningfnl.ipynb', 'repo_name': 'pacemaker2828/CDTW', 'size': '4896'}