<jupyter_start><jupyter_text>Read the CSV and Perform Basic Data Cleaning<jupyter_code>df = pd.read_csv("exoplanet_data.csv")
# Drop the null columns where all values are null
df = df.dropna(axis='columns', how='all')
# Drop the null rows
df = df.dropna()
df.head()<jupyter_output><empty_output><jupyter_text>Feature Selection<jupyter_code># Set features. This will also be used as your x values.
X = df.iloc[:, 1:]
X.head()
y = df.iloc[:, 0]
print(X.shape, y.shape)
import matplotlib.pyplot as plt
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_selection import SelectFromModel

clf = DecisionTreeClassifier()
trans = SelectFromModel(clf, threshold='median')
clf.fit(X, y)

pd.Series(clf.feature_importances_, index=df.columns[1:]).plot.bar(color='steelblue', figsize=(12, 6))<jupyter_output><empty_output><jupyter_text>Select your features (columns)<jupyter_code>trans = SelectFromModel(clf, threshold='median')
kepler_X_trans = trans.fit_transform(X, y)
columns_retained_FromMode = df.iloc[:, 1:].columns[trans.get_support()].values
columns_retained_FromMode
X = df[['koi_fpflag_nt', 'koi_fpflag_ss', 'koi_fpflag_co', 'koi_fpflag_ec',
       'koi_period', 'koi_time0bk', 'koi_impact', 'koi_impact_err1',
       'koi_duration', 'koi_prad', 'koi_prad_err1', 'koi_model_snr',
       'koi_tce_plnt_num', 'koi_steff', 'koi_steff_err1',
       'koi_slogg_err1', 'koi_srad_err1', 'ra', 'dec', 'koi_kepmag']]
X.head()<jupyter_output><empty_output><jupyter_text>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(X, y, random_state=1)
X_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
import tensorflow
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from tensorflow.keras.utils import to_categorical

X_scaler = MinMaxScaler().fit(X_train)
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=400)
rf = rf.fit(X_train_scaled, y_train)
print(f"Training Data Score: {rf.score(X_train_scaled, y_train)}")
print(f"Testing Data Score: {rf.score(X_test_scaled, y_test)}")
predictions = rf.predict(X_test_scaled)
print(f"First 10 Predictions:   {predictions[:10]}")
print(f"First 10 Actual labels: {y_test[:10].tolist()}")
pd.DataFrame({"Prediction": predictions, "Actual": y_test}).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>Hyperparameter TuningUse `GridSearchCV` to tune the model's parameters<jupyter_code># Create the GridSearchCV model
from sklearn.svm import SVC
rf = RandomForestClassifier(n_estimators=400)
from sklearn.model_selection import GridSearchCV
param_grid = { 
    'max_features': ['auto', 'sqrt', 'log2'],
    'max_depth' : [4,5,6,7,8],
    'criterion' :['gini', 'entropy']
}
grid = GridSearchCV(rf, param_grid, verbose=3)
grid.fit(X_train_scaled, y_train)
print(grid.best_params_)
print(grid.best_score_)
predictions = grid.predict(X_test_scaled)
from sklearn.metrics import classification_report
print(classification_report(y_test, predictions,
                            target_names=["confirmed", "false positive", "candidate"]))<jupyter_output>precision    recall  f1-score   support

     confirmed       0.86      0.73      0.79       404
false positive       0.78      0.87      0.82       435
     candidate       0.99      1.00      0.99       909

      accuracy                           0.90      1748
     macro avg       0.87      0.87      0.87      1748
  weighted avg       0.91      0.90      0.90      1748<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 = 'Angel_Milla2_best_model.sav'
joblib.dump(rf, filename)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11318944844124701, 'fraction_numerical': 0.02541966426858513, 'mean_word_length': 5.216095380029806, 'pattern_counts': {'":': 2, '<': 28, '<?xml version=': 0, '>': 28, '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': '274044', 'license': 'ADSL', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1487, 'n_words': 323, 'path': 'starter_code/model_2.ipynb', 'repo_name': 'angheljf/machine-learning-challenge', 'size': '4170'}