<jupyter_start><jupyter_text>Read the CSV and Perform Basic Data Cleaning<jupyter_code>df = pd.read_csv("exoplanet_data.csv")
print(df.shape)
# Drop the null columns where all values are null
df = df.dropna(axis='columns', how='all')
# Drop the null rows
df = df.dropna()
# remove false positive records and other records.....can't do this
#df = df[~(df["koi_disposition"].str.match("CANDIDATE"))]
print(df.shape)
df.head()<jupyter_output>(6991, 41)
(6991, 41)<jupyter_text>Select your features (columns)<jupyter_code># Set features. This will also be used as your x values.
df['koi_fpflag_nt'] = df['koi_fpflag_nt'].astype('category')
df['koi_fpflag_ss'] = df['koi_fpflag_ss'].astype('category')
df['koi_fpflag_co'] = df['koi_fpflag_co'].astype('category')
df['koi_fpflag_ec'] = df['koi_fpflag_ec'].astype('category')
# Copy of all features
selected_features = df[['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' 
                       ]]

selected_features.describe()<jupyter_output><empty_output><jupyter_text>Remove err valuesselected_features = df[['koi_fpflag_nt', 'koi_fpflag_ss', 'koi_fpflag_co', 'koi_fpflag_ec',                        'koi_period',                         'koi_time0bk',                        'koi_impact',                         'koi_duration',                         'koi_depth',                        'koi_prad',                         'koi_teq',                         'koi_insol',                         'koi_model_snr',                         'koi_tce_plnt_num',                        'koi_steff',                         'koi_slogg',                        'koi_srad',                        'ra',                         'dec',                         'koi_kepmag'                        ]]selected_features.describe()  Create a Train Test SplitUse `koi_disposition` for the y values<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(selected_features, df["koi_disposition"], random_state=23)
X_train.head()
y_train.head()<jupyter_output><empty_output><jupyter_text>Pre-processingScale the data using the MinMaxScaler and perform some feature selection<jupyter_code># Scale your data
from sklearn.preprocessing import MinMaxScaler

X_scaler = MinMaxScaler().fit(X_train)
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)
print(X_train_scaled)
# HotKey Encode the data
from sklearn.preprocessing import LabelEncoder

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_train)<jupyter_output>[1 2 0 ... 1 2 2]<jupyter_text>Train the Model<jupyter_code># Create a Logistic Regression Classifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
classifier =  RandomForestClassifier() 

# fit the model with the training data
classifier.fit(X_train_scaled, encoded_y_train)
print(f"Training Data Score: {classifier.score(X_train_scaled, encoded_y_train)}")
print(f"Testing Data Score: {classifier.score(X_test_scaled, encoded_y_test)}")<jupyter_output>Training Data Score: 1.0
Testing Data Score: 0.9073226544622426<jupyter_text>Hyperparameter TuningUse `GridSearchCV` to tune the model's parameters<jupyter_code># Create the GridSearchCV model
from sklearn.model_selection import GridSearchCV

# (max_depth=5, n_estimators=10, max_features=1

param_grid = {'max_depth': [1, 2, 5, 10],
              'n_estimators': [1, 5, 10, 15],
              'n_jobs': [1, 5, 10],
              'max_features': [1, 5, 10, 20, 25]}
grid = GridSearchCV(classifier, param_grid, verbose=3)
print(grid.estimator.get_params().keys())<jupyter_output>dict_keys(['bootstrap', 'ccp_alpha', 'class_weight', 'criterion', 'max_depth', 'max_features', 'max_leaf_nodes', 'max_samples', 'min_impurity_decrease', 'min_impurity_split', 'min_samples_leaf', 'min_samples_split', 'min_weight_fraction_leaf', 'n_estimators', 'n_jobs', 'oob_score', 'random_state', 'verbose', 'warm_start'])<jupyter_text>Create the GridSearchCV modelfrom sklearn.model_selection import GridSearchCVparam_grid = {'C': [1, 5, 10, 50],              'kernel': ['linear'],              'gamma': [0.0001, 0.0005, 0.001, 0.005]}grid = GridSearchCV(model, param_grid, verbose=3)<jupyter_code># Train the model with GridSearch
grid.fit(X_train_scaled, encoded_y_train)
print(grid.best_params_)
print(grid.best_score_)<jupyter_output>{'max_depth': 10, 'max_features': 20, 'n_estimators': 10, 'n_jobs': 1}
0.8943353903026511<jupyter_text>Apply best parameters<jupyter_code>classifier =  RandomForestClassifier(max_depth=10, max_features=20, n_jobs=1, n_estimators=10) 

# fit the model with the training data
classifier.fit(X_train_scaled, encoded_y_train)
print(f"Training Data Score: {classifier.score(X_train_scaled, encoded_y_train)}")
print(f"Testing Data Score: {classifier.score(X_test_scaled, encoded_y_test)}")<jupyter_output>Training Data Score: 0.9303833683005913
Testing Data Score: 0.8958810068649885<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 = 'random_forest_classifier_model.sav'
joblib.dump(classifier, filename)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11446815137828999, 'fraction_numerical': 0.028188755645538077, 'mean_word_length': 3.519352568613652, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '112655', 'license': 'ADSL', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2174, 'n_words': 417, 'path': 'models/RandomForestClassifier_model.ipynb', 'repo_name': 'gshreve01/machine-learning-challenge', 'size': '6421'}