<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)<jupyter_code># Set features. This will also be used as your x values.
X = selected_features = df.drop(columns=["koi_disposition"])<jupyter_output><empty_output><jupyter_text>Create a Train Test SplitUse `koi_disposition` for the y values      NOTES:             Since our Y values will be in the categorical forma we will need to convert this to numbers(enconding)         This Encoded values will be in ordinal format [0,1,2]      Converting Categorical Data(target) Numerical -- Encoding<jupyter_code># Target Values
y = df['koi_disposition'].astype('category').cat.codes<jupyter_output><empty_output><jupyter_text>Encoded Categories<jupyter_code>pd.DataFrame({'Category':df['koi_disposition'],'Encoded':y}).value_counts()
# Splitting data
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
X_train.head()<jupyter_output><empty_output><jupyter_text>Pre-processingScale the data using the MinMaxScaler and perform some feature selection<jupyter_code>from sklearn.preprocessing import MinMaxScaler  #importing MinMaxScaler, scaling data from [0-1]
def scale_minmax(X_values):
    return MinMaxScaler().fit(X_values)
# creating the scaler instance
min_max_scaler = scale_minmax(X_train)
X_train_scaled = min_max_scaler.transform(X_train)
X_train_scaled
# The same instance of the transformer can then be applied to some new test data unseen during the fit call:
# the same scaling and shifting operations will be applied to be consistent with the transformation performed on the 
# train data --> https://scikit-learn.org/stable/modules/preprocessing.html#scaling-features-to-a-range
X_test_scaled = min_max_scaler.transform(X_test)
X_test_scaled<jupyter_output><empty_output><jupyter_text>Train the Model<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
# compute classification accuracy
from sklearn import metrics<jupyter_output><empty_output><jupyter_text>KNN Optimal K Value<jupyter_code>k_range = range(1,26)
scores = []
for k in (k_range):
    knn_ = KNeighborsClassifier(n_neighbors=k)
    knn_.fit(X_train_scaled,y_train)
    y_pred_ = knn_.predict(X_test_scaled)
    scores.append(metrics.accuracy_score(y_test,y_pred_))
print(scores)
optimal_K = scores.index(sorted(scores, reverse=True)[0]) +1
optimal_K
plt.plot(k_range,scores)
plt.xlabel("K Value for KNN")
plt.ylabel("Testing Accuracy")
knn_model = KNeighborsClassifier(n_neighbors=optimal_K)
knn_model.fit(X_train_scaled,y_train)
y_predict = knn_model.predict(X_test_scaled)
print(metrics.accuracy_score(y_test,y_predict))<jupyter_output>0.8289473684210527<jupyter_text>Hyperparameter TuningUse `GridSearchCV` to tune the model's parameters<jupyter_code># Create the GridSearchCV model
from sklearn.model_selection import GridSearchCV
# define the parameter values that should be searched
k_range = list(range(1, 31))
weight_options = ['uniform', 'distance']
print(k_range)
# create a parameter grid: map the parameter names to the values that should be searched
param_grid = dict(n_neighbors=k_range)
print(param_grid)
# instantiate the grid
grid = GridSearchCV(knn_model, param_grid, cv=20, scoring='accuracy',n_jobs = -1,verbose=5)
# Train the model with GridSearch
grid.fit(X_train_scaled, y_train)
# List the best parameters for this dataset
print(grid.best_params_)
print(grid.best_score_)
pd.DataFrame(grid.cv_results_)[['mean_test_score', 'std_test_score', 'params']]
# print the array of mean scores only
grid_mean_scores = grid.cv_results_['mean_test_score']
print(grid_mean_scores)
# examine the best model
print(grid.best_score_)
print(grid.best_params_)
print(grid.best_estimator_)<jupyter_output>0.8258547876817695
{'n_neighbors': 22}
KNeighborsClassifier(n_neighbors=22)<jupyter_text>Reducing Computational expense using RandomizedSearchCV<jupyter_code>from sklearn.model_selection import RandomizedSearchCV
# specify "parameter distributions" rather than a "parameter grid"
param_dist = dict(n_neighbors=k_range, weights=weight_options)
# n_iter controls the number of searches
rand = RandomizedSearchCV(knn_model, param_dist, cv=10, scoring='accuracy', n_iter=10, random_state=5)
rand.fit(X_train_scaled, y_train)
pd.DataFrame(rand.cv_results_)[['mean_test_score', 'std_test_score', 'params']]
# examine the best model
print(rand.best_score_)
print(rand.best_params_)<jupyter_output>0.825476917484551
{'weights': 'uniform', 'n_neighbors': 18}<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 = 'Juan_Castaneda_Model_2_KNN.sav'
joblib.dump(knn_model, filename)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09517321915142078, 'fraction_numerical': 0.015764889061891788, 'mean_word_length': 7.7101694915254235, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 39, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '476046', 'license': 'ADSL', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1733, 'n_words': 414, 'path': 'starter_code/model_2_Classification_KNN.ipynb', 'repo_name': 'crogar/machine-learning-challenge', 'size': '5138'}