<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<jupyter_output><empty_output><jupyter_text>Select your features (columns)<jupyter_code>df.columns
# Set features. This will also be used as your x values.
X = df.drop("koi_disposition", axis=1)
X.head()<jupyter_output><empty_output><jupyter_text>Create a Train Test SplitUse `koi_disposition` for the y values<jupyter_code>from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()

label_encoder.fit(df["koi_disposition"])
label_encoder.classes_

y = label_encoder.transform(df["koi_disposition"])
y
# Split data into training and testing
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y)
X_train.head()<jupyter_output><empty_output><jupyter_text>Pre-processingScale the data using the MinMaxScaler and perform some feature selection  Train the Model<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(random_state=1)
rf.fit(X_train,y_train)
import numpy as np
np.array(y_test)
predictions = rf.predict(X_test)

for x in range(0,5,1):
    print(f"Predictions: {predictions[x]}, Actual: {y_test[x]}")
print(f"Training Data Score: {rf.score(X_train, y_train)}")
print(f"Testing Data Score: {rf.score(X_test, y_test)}")<jupyter_output>Training Data Score: 1.0
Testing Data Score: 0.9050343249427918<jupyter_text>Hyperparameter TuningUse `GridSearchCV` to tune the model's parameters<jupyter_code># Create the GridSearchCV model
from sklearn.model_selection import GridSearchCV

param_grid = { 
    'n_estimators': [200,500],
    'max_features': ['auto', 'sqrt', 'log2'],
    'max_depth' : [5,8,10],
    'criterion' :['gini', 'entropy']
}
grid_rf = GridSearchCV(rf, param_grid, verbose=3)
# Train the model with GridSearch
grid_rf.fit(X_train, y_train)
print(grid_rf.best_params_)
print(grid_rf.best_score_)
# Make predictions with the hypertuned model
predictions = grid_rf.predict(X_test)
predictions
# Calculate classification report
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.83      0.75      0.79       412
FALSE POSITIVE       0.81      0.86      0.83       446
     CANDIDATE       0.98      0.99      0.98       890

      accuracy                           0.90      1748
     macro avg       0.87      0.87      0.87      1748
  weighted avg       0.90      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 = 'Models/randomForestClassifier.sav'
joblib.dump(grid_rf, filename)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09732283464566929, 'fraction_numerical': 0.03338582677165354, 'mean_word_length': 4.533101045296167, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '64555', 'license': 'ADSL', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1067, 'n_words': 273, 'path': 'starter_code/.ipynb_checkpoints/Random Forest-checkpoint.ipynb', 'repo_name': 'SergioGL8/MachineLearning-challenge', 'size': '3175'}