<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 = df[df["koi_disposition"] != 'CANDIDATE']
df["koi_disposition"].value_counts()<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[['koi_period', 'koi_impact', 'koi_duration', 'koi_depth', 'koi_prad', 'koi_teq', 'koi_insol', 'koi_steff', 'koi_slogg', 'koi_srad']]
selected_features.koi_period.astype(float)<jupyter_output><empty_output><jupyter_text>Create a Train Test SplitUse `koi_disposition` for the y values<jupyter_code>df['koi_disposition_dummy'] = df.koi_disposition.map({'FALSE POSITIVE':0, 'CONFIRMED':1})
y = df['koi_disposition_dummy']
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(selected_features, y, random_state=0)
X_train.shape<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
# minmax = MinMaxScaler()
# X_scaler = minmax.fit_transform(X_train)<jupyter_output><empty_output><jupyter_text>Train the Model<jupyter_code>from sklearn.ensemble import RandomForestClassifier
import numpy as np

np.random.seed(0)
# Creating a random forest classifier
model2 = RandomForestClassifier(n_jobs=2, random_state=0)
# Training classifier
model2.fit(X_train, y_train)
preds = model2.predict(X_test)
pd.crosstab(y_test, preds, rownames=['Actual Result'], colnames=['Predicted Result'])
num_important = (model2.feature_importances_).astype(str)
features = X_test.columns

def myfunc(a, b):
    return [round(float(b)*100, 2)] + [a]


important_features_map = map(myfunc, features, num_important)
important_features = dict(important_features_map)
 
sorted_important_features = sorted(important_features.items(), reverse=True)
print(f"Training Data Score: {model2.score(X_train, y_train)}")
print(f"Testing Data Score: {model2.score(X_test, y_test)}")
print("-----------------------------------------------------")
print(f"Most important features:\n{sorted_important_features[:4]}")
from matplotlib import pyplot as plt

lists = sorted(important_features.items()) # sorted by key, return a list of tuples

y, x = zip(*lists) # unpack a list of pairs into two tuples

fig, ax = plt.subplots()
ax.bar(x, y)

ax.set(xlabel='Features', ylabel='% of importance',
       title='Importance of features by %')

labels = x
plt.xticks(x, labels, rotation=45)

plt.show()

plt.savefig("../Images/important_features.png")<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 = 'megan_okerlund_model2.sav'
joblib.dump(model2, filename)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13438857852265673, 'fraction_numerical': 0.006517690875232775, 'mean_word_length': 6.637440758293839, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '214099', 'license': 'ADSL', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1108, 'n_words': 260, 'path': "Mokerlund's_solution/Ipynb_and_code/.ipynb_checkpoints/model_2-checkpoint.ipynb", 'repo_name': 'mokerlund/machine_learning_classification_challenge', 'size': '3222'}