<jupyter_start><jupyter_text>ModelsModeling may take time to execute and is optionally available to execute from the command line.```bashpython ./scripts/model.py```<jupyter_code># %load -r 1-3 ../scripts/model.py
# load pandas for data analysis
import pandas as pd  # noqa E401
import numpy as np
# load dataset
parking_violations = pd.read_csv('../data/final/all_cleaned.csv')
# head of dataset
parking_violations.head()
# dataset info
parking_violations.info()
# %load -r 7-18 ../scripts/model.py
features = ['LATITUDE', 'LONGITUDE', 'MONTH', 'DAY_OF_MONTH', 'HOUR']

# establish data for classification
X = parking_violations[features]  # noqa F821
y = parking_violations['DISPOSITION_RESULT']  # noqa F821

from sklearn.preprocessing import StandardScaler  # noqa E402
X = StandardScaler().fit_transform(X)

from sklearn.model_selection import train_test_split  # noqa E402
X_train, X_test, y_train, y_test =\
    train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
# %load -r 20-26 ../scripts/model.py
from sklearn.linear_model import LogisticRegression  # noqa E402

log_reg = LogisticRegression(penalty='none', random_state=42)
log_reg.fit(X_train, y_train)

print('Score - Training: {:f}'.format(log_reg.score(X_train, y_train)))
print('Score - Test: {:f}'.format(log_reg.score(X_test, y_test)))
# %load -r 28-36 ../scripts/model.py
from sklearn.linear_model import PassiveAggressiveClassifier  # noqa E402

passive_aggressive_clf = PassiveAggressiveClassifier(random_state=42)
passive_aggressive_clf.fit(X_train, y_train)

print('Score - Training: {:f}'.format(passive_aggressive_clf.score(
    X_train, y_train)))
print('Score - Test: {:f}'.format(passive_aggressive_clf.score(
    X_test, y_test)))
# %load -r 38-44 ../scripts/model.py
from sklearn.tree import DecisionTreeClassifier  # noqa E402

tree_clf = DecisionTreeClassifier(random_state=42)
tree_clf.fit(X_train, y_train)

print('Score - Training: {:f}'.format(tree_clf.score(X_train, y_train)))
print('Score - Test: {:f}'.format(tree_clf.score(X_test, y_test)))
# %load -r 46-51 ../scripts/model.py
from sklearn.linear_model import RidgeClassifier  # noqa E402

ridge_clf = RidgeClassifier().fit(X_train, y_train)

print('Score - Training: {:f}'.format(ridge_clf.score(X_train, y_train)))
print('Score - Test: {:f}'.format(ridge_clf.score(X_test, y_test)))
# %load -r 53-59 ../scripts/model.py
from sklearn.ensemble import RandomForestClassifier  # noqa E402

forest_clf = RandomForestClassifier(random_state=42)
forest_clf.fit(X_test, y_test)

print('Score - Training: {:f}'.format(forest_clf.score(X_train, y_train)))
print('Score - Test: {:f}'.format(forest_clf.score(X_test, y_test)))
# %load -r 61-67 ../scripts/model.py
from sklearn.ensemble import AdaBoostClassifier  # noqa E402

ada_clf = AdaBoostClassifier(random_state=42)
ada_clf.fit(X_test, y_test)

print('Score - Training: {:f}'.format(ada_clf.score(X_train, y_train)))
print('Score - Test: {:f}'.format(ada_clf.score(X_test, y_test)))
# %load -r 69-75 ../scripts/model.py
from sklearn.neural_network import MLPClassifier  # noqa E402

mlp_clf = MLPClassifier(random_state=42)
mlp_clf.fit(X_test, y_test)

print('Score - Training: {:f}'.format(mlp_clf.score(X_train, y_train)))
print('Score - Test: {:f}'.format(mlp_clf.score(X_test, y_test)))
# %load -r 12-15 ../scripts/utils.py
def display_scores(name, scores):
    print(name)
    print('Mean:', scores.mean())
    print('Standard deviation:', scores.std())
# %load -r 78 ../scripts/model.py
from sklearn.model_selection import cross_val_score  # noqa E402
# %load -r 80-99 ../scripts/model.py
log_reg_scores = cross_val_score(log_reg, X, y,
                                 scoring="neg_mean_squared_error")
log_reg_rmse_scores = np.sqrt(-log_reg_scores)
display_scores('Logistic Regression', log_reg_rmse_scores)

passive_aggressive_clf_scores =\
    cross_val_score(passive_aggressive_clf, X, y,
                    scoring="neg_mean_squared_error")
passive_aggressive_clf_rmse_scores = np.sqrt(-passive_aggressive_clf_scores)
display_scores('Passive Aggressive', passive_aggressive_clf_rmse_scores)

tree_clf_scores = cross_val_score(tree_clf, X, y,
                                  scoring="neg_mean_squared_error")
tree_clf_rmse_scores = np.sqrt(-tree_clf_scores)
display_scores('Decision Tree', tree_clf_rmse_scores)

ridge_clf_scores = cross_val_score(ridge_clf, X, y,
                                   scoring="neg_mean_squared_error")
ridge_clf_rmse_scores = np.sqrt(-ridge_clf_scores)
display_scores('Ridge', ridge_clf_rmse_scores)
# %load -r 101-104 ../scripts/model.py
forest_clf_scores = cross_val_score(forest_clf, X, y,
                                    scoring="neg_mean_squared_error")
forest_clf_rmse_scores = np.sqrt(-forest_clf_scores)
display_scores('Forest', forest_clf_rmse_scores)
# %load -r 106-109 ../scripts/model.py
ada_clf_scores = cross_val_score(ada_clf, X, y,
                                 scoring="neg_mean_squared_error")
ada_clf_rmse_scores = np.sqrt(-ada_clf_scores)
display_scores('AdaBoost', ada_clf_rmse_scores)
# %load -r 111-114 ../scripts/model.py
mlp_clf_scores = cross_val_score(mlp_clf, X, y,
                                 scoring="neg_mean_squared_error")
mpl_clf_rmse_scores = np.sqrt(-mlp_clf_scores)
display_scores('MLP', mpl_clf_rmse_scores)
# %load -r 120-133 ../scripts/model.py
from sklearn.model_selection import GridSearchCV  # noqa E402

# ignore warnings
import warnings
warnings.filterwarnings('ignore')

param_grid = {'C': np.logspace(-3, 3, 7),
              'penalty': ['l2', 'none'],
              'solver': ['newton-cg', 'lbfgs', 'sag', 'saga']}

grid_search = GridSearchCV(LogisticRegression(),
                           param_grid,
                           scoring='neg_mean_squared_error',
                           return_train_score=True)
grid_search.fit(X_train, y_train)

print('Best parameters', grid_search.best_params_)
print('Best score', grid_search.best_score_)
# %load -r 135-143 ../scripts/model.py
from sklearn.metrics import mean_squared_error  # noqa E402

final_model = grid_search.best_estimator_

final_predictions = final_model.predict(X_test)

final_mse = mean_squared_error(y_test, final_predictions)
final_rmse = np.sqrt(final_mse)
print('Final score', final_rmse)
# pickle the model
import joblib  # noqa E402

joblib.dump(grid_search.best_estimator_, '../data/final/final_model.pkl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12751050910789352, 'fraction_numerical': 0.021952358710882764, 'mean_word_length': 4.600697471665214, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '285533', 'license': 'MIT', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2316, 'n_words': 425, 'path': 'notebooks/Models.ipynb', 'repo_name': 'burgwyn/DATA601-Final', 'size': '6423'}