<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()<jupyter_output><empty_output><jupyter_text>Select your features (columns) and pre-processing the data by encoding yUse `koi_disposition` for the y values<jupyter_code># Set features. This will also be used as x values.
X=df.drop("koi_disposition",axis=1)

# target values will come from "koi_disposition" column
y=df["koi_disposition"]

print(X.shape,y.shape)
# Dummy Encoding(Binary Encoded Data)
# Dummy Encoding transform each categorical feature into new columns with a 1(True) or 0(False) to represent if 
# that categorical label was presnet or not in the original row.
y=pd.get_dummies(y)
y.head()<jupyter_output><empty_output><jupyter_text>Create a Train Test Split<jupyter_code>from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=10,stratify=y)
X_train.head()<jupyter_output><empty_output><jupyter_text>Pre-processing: Scaling the dataScale the data using the MinMaxScaler and perform some feature selection<jupyter_code># Scale your data
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler().fit(X_train)
X_train=scaler.transform(X_train)
X_test=scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Train the Model--KNN<jupyter_code># Can also run Random Forest model
# from sklearn.ensemble import RandomForestClassifier
# knn = RandomForestClassifier(n_estimators=10)
# knn.fit(X_train, y_train)
# #print('k=13 Test Acc: %.3f' % knn.score(X_test, y_test)
# KNN model

from sklearn.neighbors import KNeighborsClassifier

import matplotlib.pyplot as plt
train_scores = []
test_scores = []
for k in range(1, 20, 2):
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, y_train)
    train_score = knn.score(X_train, y_train)
    test_score = knn.score(X_test, y_test)
    train_scores.append(train_score)
    test_scores.append(test_score)
    print(f"k: {k}, Train/Test Score: {train_score:.3f}/{test_score:.3f}")
    
    
plt.plot(range(1, 20, 2), train_scores, marker='o')
plt.plot(range(1, 20, 2), test_scores, marker="x")
plt.xlabel("k neighbors")
plt.ylabel("Testing accuracy Score")
plt.show()
# Using KNN model here. According to the graph above, the model has the best score when K is around 5
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
print('k=13 Test Acc: %.3f' % knn.score(X_test, y_test))<jupyter_output>k=13 Test Acc: 0.823<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_neighbors':[5,6,7,8,9,10],
          'leaf_size':[1,2,3,5],
          'weights':['uniform', 'distance'],
          'algorithm':['auto', 'ball_tree','kd_tree','brute'],
          'n_jobs':[-1]}


grid=GridSearchCV(knn,param_grid,verbose=3)
# Train the model with GridSearch
grid.fit(X_train,y_train)
print(grid.best_params_)
print(grid.best_score_)
predictions=grid.predict(X_test)
# Classificaiton report 
from sklearn.metrics import classification_report
print(classification_report(y_test,predictions,target_names=["CANDIDATE","CONFIRMED","FALSE POSITIVE"]))

from sklearn import metrics
#evaluation(Accuracy)
print("Accuracy:",metrics.accuracy_score(predictions,y_test))<jupyter_output>precision    recall  f1-score   support

     CANDIDATE       0.68      0.54      0.60       422
     CONFIRMED       0.63      0.73      0.68       450
FALSE POSITIVE       0.98      1.00      0.99       876

     micro avg       0.82      0.82      0.82      1748
     macro avg       0.76      0.75      0.75      1748
  weighted avg       0.82      0.82      0.81      1748
   samples avg       0.82      0.82      0.82      1748

Accuracy: 0.8180778032036613<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 = 'KNN.sav'
joblib.dump(grid, filename)
# Comparing with SVM model, KNN gives slightly lower accuracy score. But both models are decent with scores acround 80%<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10564010743061773, 'fraction_numerical': 0.03491495076096687, 'mean_word_length': 4.7368421052631575, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '134263', 'license': 'ADSL', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1567, 'n_words': 390, 'path': 'code/KNN.ipynb', 'repo_name': 'sophie99999/machine-learning-challenge', 'size': '4468'}