# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/aquarius31/Crime-Predictor/blob/master/Multi_Layer_Perceptron.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="1Ba0xpg6BZMe" colab_type="code" outputId="6a8d5097-ff5d-4663-f1ba-c43de7393f20" colab={"base_uri": "https://localhost:8080/", "height": 34}
# %pylab inline

# + id="WA5sm9e1Bdqh" colab_type="code" colab={}
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler   
from sklearn.model_selection import train_test_split
from sklearn import model_selection
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score

# + id="0rpoZ9HfBmnD" colab_type="code" outputId="805d3e0a-6b0a-4f1d-c2eb-70cd0ce6e9e0" colab={"base_uri": "https://localhost:8080/", "height": 34}
from google.colab import drive
drive.mount("/content/gdrive", force_remount=True)

# + id="pxZ-sHTCBqgb" colab_type="code" colab={}
# Read the dataset
crimes_stacked = pd.read_csv('gdrive/My Drive/Crime Predictor/crimes_stacked.csv', index_col=None)

# + id="Fj7akM9lBt2T" colab_type="code" colab={}
# Get rid of useless columns
X = crimes_stacked[['Arrest', 'Domestic', 'Beat', 'Community Area',
                   'Latitude', 'Longitude', 'Year', 'Hour', 'Closest police station']]
# Extract the target feature
y = crimes_stacked['Primary Type']

# + id="cpWbQ_noB5z3" colab_type="code" colab={}
# Fill Nan with 0
X = X.fillna(0)
# Gather column names
feature_names = list(X)
target_names = list(y)

# + id="1zensZ6GB6VS" colab_type="code" outputId="6c48ff5e-7af0-4f6b-82e1-27ce416832fa" colab={"base_uri": "https://localhost:8080/", "height": 51}
from sklearn.preprocessing import MinMaxScaler
# Separate data into training and testing datasets
X = MinMaxScaler().fit_transform(X)
seed = 42
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=seed)
# Rescale the data
scaler = StandardScaler()
scaler.fit(X_train)  
X_train = scaler.transform(X_train)  
X_test = scaler.transform(X_test)

# + id="dpQyregrB9J4" colab_type="code" outputId="8a4ef908-fdeb-4049-f9a7-e31d9ae4f096" colab={"base_uri": "https://localhost:8080/", "height": 34}
# Use grid search with 3 K-Fold to find the best parameters
scoring = 'accuracy'
kfold = model_selection.KFold(n_splits=3, random_state=seed)
# Parameters to be tested
parameters = {'activation':['logistic', 'tanh', 'relu'],
              'hidden_layer_sizes':[(50, 15), (25, 10), (10, 5)],
              'solver':['sgd', 'adam'], 
              'learning_rate_init':[0.001, 0.01]}
# Comparison
mlp = MLPClassifier()
clf = GridSearchCV(mlp, parameters, cv=kfold)
print("kFold Done")
clf.fit(X_train, y_train)
print("Fitted")
print("Best estimator: ", clf.best_estimator_)

# + id="n2s82FZ9CBv3" colab_type="code" colab={}
# Train with the best parameters
mlp = clf.best_estimator_
mlp.fit(X_train, y_train)

# + id="m5nfXpwmCCV7" colab_type="code" colab={}
results = cross_val_score(dt, X, y)
print("Accuracy: %.3f%% (%.3f%%)") % (results.mean()*100.0, results.std()*100.0)

# + id="lbupSf3RCEhk" colab_type="code" colab={}
print("Current loss:", mlp.loss_)

# + id="7Hza0D5PCHCz" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered