<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()
# Remove "CANDIDATE" outcomes from the dataset
print(len(df))
# df = df.loc[((df['koi_disposition'] == 'CONFIRMED') | (df['koi_disposition'] == 'FALSE POSITIVE')) , :]
print(len(df))
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'] ]<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()<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>Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=99)
classifier
y_train = y_train.ravel()
model2 = classifier.fit(X_train_scaled, y_train)<jupyter_output>/anaconda3/envs/py365_for_class/lib/python3.6/site-packages/sklearn/linear_model/_logistic.py:940: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)<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: 0.8188060270837307
Testing Data Score: 0.8306636155606407<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 = {'C': [0.1, 0.5, 1, 1.5, 2, 3, 5, 7, 10],
              'penalty': ["l1", "l2"]}
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_)<jupyter_output>{'C': 5, 'penalty': 'l2'}
0.8208984929303809<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-logistic_regression.sav'
joblib.dump(grid, filename)
# models =  [logistic_regression, random_forest, svm]

# for model in models:
#     model.fit()
#     model.score()
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
lr = LogisticRegression(random_state=99)
rf = RandomForestClassifier(random_state=99)
knn = KNeighborsClassifier(n_neighbors=10)
svm_model = SVC()

models = [lr, rf, knn, svm_model]
names = ['Logistic Regression', 'Random Forest', 'K Nearest Neighbbors', 'Support Vector Classification']
for name, model in zip(names, models):
    model.fit(X_train_scaled, y_train)
    score = model.score(X_test_scaled, y_test)
    print(name, score)<jupyter_output>/anaconda3/envs/py365_for_class/lib/python3.6/site-packages/sklearn/linear_model/_logistic.py:940: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
{'fraction_non_alphanumeric': 0.117735565168328, 'fraction_numerical': 0.02388564980252022, 'mean_word_length': 6.820588235294117, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 4, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '26855', 'license': 'MIT', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1919, 'n_words': 424, 'path': 'model_2_knn.ipynb', 'repo_name': 'alexisperumal/machine-learning-challenge', 'size': '5317'}