# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.9.7 ('MSCS-torch')
#     language: python
#     name: python3
# ---

import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
pd.options.mode.chained_assignment = None

# +
train_df = pd.read_csv('train.csv', index_col=0)
test_df = pd.read_csv('test.csv', index_col=0)

Y_columns = ['koi_disposition', 'koi_pdisposition', 'koi_score']
misc_columns = ['kepid', 'kepoi_name', 'kepler_name', 'koi_tce_delivname']

train_X = train_df.drop(columns=Y_columns + misc_columns)
train_Y = train_df[Y_columns + misc_columns]

test_X = test_df.drop(columns=Y_columns + misc_columns)
test_Y = test_df[Y_columns + misc_columns]
# -

les = {}
Y = pd.concat([train_Y, test_Y])
for dtype, col in zip(Y.dtypes, Y.columns):
  if dtype == 'object':
    les[col] = LabelEncoder()
    les[col].fit(Y[col])
    train_Y[col] = les[col].transform(train_Y[col])
    test_Y[col] = les[col].transform(test_Y[col])

# ### KOI Disposition - Exoplanet Archive Disposition

m = DecisionTreeClassifier()
cvs = cross_val_score(m, train_X, train_Y['koi_disposition'], cv=5)
m.fit(train_X, train_Y['koi_disposition'])
score = m.score(test_X, test_Y['koi_disposition'])
print(f'Cross Validation Score: {cvs.min()}\nScore: {score}')

m = KNeighborsClassifier()
cvs = cross_val_score(m, train_X, train_Y['koi_disposition'], cv=5)
m.fit(train_X, train_Y['koi_disposition'])
score = m.score(test_X, test_Y['koi_disposition'])
print(f'Cross Validation Score: {cvs.min()}\nScore: {score}')

m = RandomForestClassifier()
cvs = cross_val_score(m, train_X, train_Y['koi_disposition'], cv=5)
m.fit(train_X, train_Y['koi_disposition'])
score = m.score(test_X, test_Y['koi_disposition'])
print(f'Cross Validation Score: {cvs.min()}\nScore: {score}')

# ### Conclusion
#
# The random forest classifier is unsurprisingly the best classifier to predict KOI disposition. Further, both the random forest and decision tree classifiers did surprisingly well considering KOI disposition is according the scholarly sources and not statistical analysis. Lastly, the k nearest neighbor classifier was expected to perform poorly because there are so many dimensions and its hard to get a notion of "nearest."

# ### KOI P-Disposition - Disposition Using Kepler Data

m = DecisionTreeClassifier()
cvs = cross_val_score(m, train_X, train_Y['koi_pdisposition'], cv=5)
m.fit(train_X, train_Y['koi_pdisposition'])
score = m.score(test_X, test_Y['koi_pdisposition'])
print(f'Cross Validation Score: {cvs.min()}\nScore: {score}')

m = KNeighborsClassifier()
cvs = cross_val_score(m, train_X, train_Y['koi_pdisposition'], cv=5)
m.fit(train_X, train_Y['koi_pdisposition'])
score = m.score(test_X, test_Y['koi_pdisposition'])
print(f'Cross Validation Score: {cvs.min()}\nScore: {score}')

m = RandomForestClassifier()
cvs = cross_val_score(m, train_X, train_Y['koi_pdisposition'], cv=5)
m.fit(train_X, train_Y['koi_pdisposition'])
score = m.score(test_X, test_Y['koi_pdisposition'])
print(f'Cross Validation Score: {cvs.min()}\nScore: {score}')

# ### Conclusion
#
# The same conclusions from KOI disposition can be used with the exception that the classifiers are a lot better because KOI p-disposition is according to statistical analysis.

StarcoderdataJupyterScriptsDedupFiltered