<jupyter_start><jupyter_text>Model-2 Logistic Regression<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
# import dependencies
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Read the CSV and Perform Basic Data Cleaning<jupyter_code>df = pd.read_csv("data/exoplanet_data.csv")
# Drop the null columns where all values are null
df = df.dropna(axis='columns', how='all')
pd.set_option('display.max_columns', None)
# Drop the null rows
df = df.dropna()
df.head(100)<jupyter_output><empty_output><jupyter_text>Select the features<jupyter_code># Set features. That will be used as your x values.
selected_features = df[[
                        '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_prad','koi_prad_err1','koi_prad_err2',
                        'koi_srad','koi_srad_err1','koi_srad_err2',
                        'ra','dec','koi_kepmag']]
selected_features.head(20)<jupyter_output><empty_output><jupyter_text>Create a Train Test Split<jupyter_code># get X and y variables
y = df["koi_disposition"]
X = selected_features
# split the data to train and test values

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, stratify=y)<jupyter_output><empty_output><jupyter_text>Pre-processing<jupyter_code># Import dependencies
from sklearn.preprocessing import LabelEncoder, MinMaxScaler,StandardScaler
#from tensorflow.keras.utils import to_categorical

# scale the data with StandardScaler
X_scaler = StandardScaler().fit(X_train)
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)

# Label-encode data set
label_encoder = LabelEncoder()
label_encoder.fit(y_train)
encoded_y_train = label_encoder.transform(y_train)
encoded_y_test = label_encoder.transform(y_test)


print(encoded_y_test)<jupyter_output>[0 2 2 ... 2 2 1]<jupyter_text>Train the Model<jupyter_code>from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model
# fit the data and make predictions
model.fit(X_train_scaled, encoded_y_train)
predictions = model.predict(X_test_scaled)
predictions
print(f"Training Data Score: {model.score(X_train_scaled, encoded_y_train)}")
print(f"Testing Data Score: {model.score(X_test_scaled, encoded_y_test)}")
# Calculate classification report
from sklearn.metrics import classification_report
print(classification_report(encoded_y_test, predictions,
                           target_names =["confirmed", "false positive", "candidate"]))<jupyter_output>precision    recall  f1-score   support

     confirmed       0.46      0.14      0.21       422
false positive       0.65      0.82      0.72       450
     candidate       0.69      0.84      0.76       876

      accuracy                           0.66      1748
     macro avg       0.60      0.60      0.56      1748
  weighted avg       0.63      0.66      0.62      1748<jupyter_text>Hyperparameter Tuning<jupyter_code># Create the GridSearchCV model
from sklearn.model_selection import GridSearchCV
param_grid = {'C': [10, 50, 100],
             'max_iter':[200, 500, 1000]}
grid = GridSearchCV(model,param_grid, verbose=3)
# Train the model with GridSearch
grid.fit(X_train_scaled, encoded_y_train)
print(grid.best_params_)
print(grid.best_score_)
# save the model

import joblib
filename = 'logistig_reg_model.sav'
joblib.dump(model, filename)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09229576008273009, 'fraction_numerical': 0.030248190279214063, 'mean_word_length': 4.084099868593955, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '37356', 'license': 'RSA-MD', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1271, 'n_words': 282, 'path': 'model_2.ipynb', 'repo_name': 'aijiezhao/machine-learning-challenge', 'size': '3868'}