<jupyter_start><jupyter_text>Machine Learning Challenge <NAME> August 11, 2020 File Name:  RobGauer_model_1.ipynb -----------------------<jupyter_code># Update sklearn to prevent version mismatches.

# !pip install sklearn --upgrade
# Install joblib. This will be used to save your model. 
# Restart your kernel after installing. 

# !pip install joblib
# Setup and Dependancies.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Read the CSV and Perform Basic Data Cleaning<jupyter_code># total dataset is 9564 records
df = pd.read_csv("cumulative.csv")   
df.shape
# show column names
print(df.columns)<jupyter_output>Index(['rowid', 'kepid', 'kepoi_name', 'kepler_name', 'koi_disposition',
       'koi_pdisposition', 'koi_score', 'koi_fpflag_nt', 'koi_fpflag_ss',
       'koi_fpflag_co', 'koi_fpflag_ec', 'koi_period', 'koi_period_err1',
       'koi_period_err2', 'koi_time0bk', 'koi_time0bk_err1',
       'koi_time0bk_err2', 'koi_impact', 'koi_impact_err1', 'koi_impact_err2',
       'koi_duration', 'koi_duration_err1', 'koi_duration_err2', 'koi_depth',
       'koi_depth_err1', 'koi_depth_err2', 'koi_prad', 'koi_prad_err1',
       'koi_prad_err2', 'koi_teq', 'koi_teq_err1', 'koi_teq_err2', 'koi_insol',
       'koi_insol_err1', 'koi_insol_err2', 'koi_model_snr', 'koi_tce_plnt_num',
       'koi_tce_delivname', 'koi_steff', 'koi_steff_err1', 'koi_steff_err2',
       'koi_slogg', 'koi_slogg_err1', 'koi_slogg_err2', 'koi_srad',
       'koi_srad_err1', 'koi_srad_err2', 'ra', 'dec', 'koi_kepmag'],
      dtype='object')<jupyter_text>Select your features (columns)<jupyter_code># Set features. This will also be used as your x values.
#  Remove features that create noise and result in improved model
df = df.drop(columns=['rowid', 'kepid', 'kepoi_name', 'kepler_name', 'koi_disposition', 'koi_score','koi_tce_delivname'])
# Drop the null columns where all values are nullkepoi_name
df = df.dropna(axis='columns', how='all')
# Drop the null rows
df = df.dropna()
df.describe()
# still 8700 records without nulls
df.shape 
# first column is the label, koi_pdisposition
df.head(5) 
# check remaining feature's names
print(df.columns)<jupyter_output>Index(['koi_pdisposition', 'koi_fpflag_nt', 'koi_fpflag_ss', 'koi_fpflag_co',
       'koi_fpflag_ec', 'koi_period', 'koi_period_err1', 'koi_period_err2',
       'koi_time0bk', 'koi_time0bk_err1', 'koi_time0bk_err2', 'koi_impact',
       'koi_impact_err1', 'koi_impact_err2', 'koi_duration',
       'koi_duration_err1', 'koi_duration_err2', 'koi_depth', 'koi_depth_err1',
       'koi_depth_err2', 'koi_prad', 'koi_prad_err1', 'koi_prad_err2',
       'koi_teq', 'koi_insol', 'koi_insol_err1', 'koi_insol_err2',
       'koi_model_snr', 'koi_tce_plnt_num', 'koi_steff', 'koi_steff_err1',
       'koi_steff_err2', 'koi_slogg', 'koi_slogg_err1', 'koi_slogg_err2',
       'koi_srad', 'koi_srad_err1', 'koi_srad_err2', 'ra', 'dec',
       'koi_kepmag'],
      dtype='object')<jupyter_text>Create a Train Test Split Use koi_dispostion for the y values.<jupyter_code># Separate data into train (75%) and test (25%)
from sklearn.model_selection import train_test_split 
 # define label
y = df["koi_pdisposition"] 

# drop label from features
X = df.drop(columns=["koi_pdisposition"])  

# random sample of label and features
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1, stratify=y)
X_train.tail(5)
y_train.shape<jupyter_output><empty_output><jupyter_text>Pre-Processing Scale the data using the MinMaxScaler and perform some feature selection.<jupyter_code># normalize features between  1 and 0
from sklearn.preprocessing import MinMaxScaler 
# Scale your data
# feature selection
X_scale = MinMaxScaler().fit(X_train)  
scaled_X_train = X_scale.transform(X_train)
scaled_X_test = X_scale.transform(X_test)
# Sample testing
scaled_X_test<jupyter_output><empty_output><jupyter_text>Create and Train the Logistic Regression Model for Classification<jupyter_code>from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(scaled_X_train, y_train)<jupyter_output><empty_output><jupyter_text>Display Train the Model<jupyter_code>print(f"Training Data Score: {model.score(X_train, y_train)}")
print(f"Testing Data Score: {model.score(X_test, y_test)}")  

#not very good result at 0.50<jupyter_output>Training Data Score: 0.503354681305276
Testing Data Score: 0.5036596523330283<jupyter_text>Hyperparameter Tuning  Use  GridSearchCV to tune the model's parameters.<jupyter_code># Create the GridSearchCV model
from sklearn.model_selection import GridSearchCV
grid = GridSearchCV(model, {'C': [1, 10], 'penalty': ["l1", "l2"]}, verbose=3)
# Train the model with GridSearch
grid.fit(X_train, y_train)
print(grid.best_params_)
print(grid.best_score_)
grid<jupyter_output><empty_output><jupyter_text>Save the Model<jupyter_code># save your model by updating "your_name" with your name
# and "your_model" with your model variable
# be sure to turn this in to BCS
# if joblib fails to import, try running the command to install in terminal/git-bash
import joblib
filename = 'RobGauer_model_1.sav'
joblib.dump(model, filename)  

# model at 0.77447
# EOF<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12136199353243295, 'fraction_numerical': 0.023016929807875213, 'mean_word_length': 6.115020297699594, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '231035', 'license': 'ADSL', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2015, 'n_words': 440, 'path': '.ipynb_checkpoints/Rob_Gauer_model_1-checkpoint.ipynb', 'repo_name': 'robgauer/21_machine-learning-challenge', 'size': '5260'}