<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()
df.columns<jupyter_output><empty_output><jupyter_text>Select your features (columns)<jupyter_code># Set features. This will also be used as your x values.
# selected_features = df[['names', 'of', 'selected', 'features', 'here']]
selected_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<jupyter_output><empty_output><jupyter_text>Create a Train Test SplitUse `koi_disposition` for the y values<jupyter_code># y = df["koi_disposition"].map({'CANDIDATE':0.0, 'CONFIRMED':1.0, 'FALSE POSITIVE':0.0}).values.reshape(-1, 1)
y = df["koi_disposition"]

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(selected_features, y, random_state=1)

y
print("X_train length: ", len(X_train))
print("X_test length: ", len(X_test))
print("y_train length: ", len(y_train))
print("y_test length: ", len(y_test))
X_train.head()
X_train.shape<jupyter_output>X_train length:  5243
X_test length:  1748
y_train length:  5243
y_test length:  1748<jupyter_text>Pre-processingScale the data using the MinMaxScaler and perform some feature selection<jupyter_code># Scale your data
from sklearn.preprocessing import StandardScaler
X_scaler = StandardScaler().fit(X_train)
# y_scaler = StandardScaler().fit(y_train)
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)
# y_train_scaled = y_scaler.transform(y_train)
# y_test_scaled = y_scaler.transform(y_test)<jupyter_output><empty_output><jupyter_text>Classifier Model of Choice<jupyter_code># Note SVM needs sclaed data, but random forest doesn't, but scaling also doesn't hurt.
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(random_state=99)
classifier
y_train = y_train.ravel()
model2 = classifier.fit(X_train_scaled, y_train)<jupyter_output><empty_output><jupyter_text>Train the Model<jupyter_code>print(f"Training Data Score: {model2.score(X_train_scaled, y_train)}")
print(f"Testing Data Score: {model2.score(X_test_scaled, y_test)}")<jupyter_output>Training Data Score: 1.0
Testing Data Score: 0.9107551487414187<jupyter_text>Hyperparameter TuningUse `GridSearchCV` to tune the model's parameters<jupyter_code># # Create the GridSearchCV model for Logistic Regression
# from sklearn.model_selection import GridSearchCV
# param_grid = {'C': [0.1, 0.5, 1, 1.5, 2, 3, 5, 7, 10],
#               'penalty': ["l1", "l2"]}
# grid = GridSearchCV(model2, param_grid, verbose=3)
# Create the GridSearchCV model for Logistic Regression
from sklearn.model_selection import GridSearchCV
param_grid = {'n_estimators': [50, 75, 100, 125, 150],
              'max_features':[None, 'auto', 'sqrt']
              }
grid = GridSearchCV(model2, param_grid, verbose=3)
# Train the model with GridSearch
grid.fit(X_train_scaled, y_train)
print(grid.best_params_)
print(grid.best_score_)
# Train the model with GridSearch
grid.score(X_test_scaled, y_test)
grid
len(X_test_scaled[0])
X_test_scaled[0].shape
X_test_scaled_point = X_test_scaled[0].reshape(1,20)
X_test_scaled_point

# Make a prediction!
X_test_scaled_point = X_test_scaled[0].reshape(1,20)
grid.predict(X_test_scaled_point)<jupyter_output><empty_output><jupyter_text>Plot the Results<jupyter_code># %matplotlib inline
# import matplotlib.pyplot as plt

# plt.scatter(X_test.iloc[:,2], y_test, c='red')
# X_test.iloc[:,0].shape
# # type(X_test)<jupyter_output><empty_output><jupyter_text>Feature Tuning<jupyter_code>feature_names = X_train.columns.tolist()
selected_features = sorted(zip(grid.estimator.feature_importances_, feature_names), reverse=True)
# grid.estimator.feature_importances_
ranked_features = pd.DataFrame(selected_features, columns=['Score', 'Feature'])
ranked_features = ranked_features.set_index('Feature')
ranked_features<jupyter_output><empty_output><jupyter_text>Note, in the list above, big drop off after 7 items, then 13.  Feature Tuning - Recursive Feature Elimination<jupyter_code>classifier_tuned = RandomForestClassifier(random_state=99, max_features='auto', n_estimators=125)
model3 = classifier_tuned.fit(X_train_scaled, y_train)
print(f"Training Data Score: {model3.score(X_train_scaled, y_train)}")
print(f"Testing Data Score: {model3.score(X_test_scaled, y_test)}")
# From: https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html?highlight=rfe#sklearn.feature_selection.RFE
# from sklearn.datasets import make_friedman1
from sklearn.feature_selection import RFE

selector = RFE(model3, 13, step=1)
selector = selector.fit(X_train_scaled, y_train)
print(selector.support_)
selector.ranking_

X_train_scaled
type(X_train_scaled)

X_train_scaled.shape
X_train_scaled_filtered = X_train_scaled[:,selector.support_]
print(X_train_scaled_filtered.shape)
X_test_scaled_filtered = X_test_scaled[:,selector.support_]
X_test_scaled_filtered.shape
model4 = classifier_tuned.fit(X_train_scaled_filtered, y_train)
print(f"Training Data Score: {model4.score(X_train_scaled_filtered, y_train)}")
print(f"Testing Data Score: {model4.score(X_test_scaled_filtered, y_test)}")


model4.predict(X_test_scaled_filtered)<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 = 'alexis-random-forest.sav'
joblib.dump(model4, filename)
model_for_verification = joblib.load(filename)
model_for_verification
model_for_verification.predict(X_test_scaled_filtered)
# print(f"Training Data Score: {model_for_verification.score(X_train_scaled_filtered, y_train)}")
print(f"Testing Data Score: {model_for_verification.score(X_test_scaled_filtered, y_test)}")<jupyter_output>Testing Data Score: 0.9107551487414187
{'fraction_non_alphanumeric': 0.11574146878440006, 'fraction_numerical': 0.02075798081459349, 'mean_word_length': 8.034090909090908, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 1, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '554277', 'license': 'MIT', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2323, 'n_words': 453, 'path': 'model_4_random_forest.ipynb', 'repo_name': 'alexisperumal/machine-learning-challenge', 'size': '6359'}