Skip to content

Instantly share code, notes, and snippets.

@marskar
Created August 30, 2019 18:41
Show Gist options
  • Select an option

  • Save marskar/d1f53e65d8d92adc3f3069576a8cad66 to your computer and use it in GitHub Desktop.

Select an option

Save marskar/d1f53e65d8d92adc3f3069576a8cad66 to your computer and use it in GitHub Desktop.
import pandas as pd
from sklearn.model_selection import KFold, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
# Get data
df = pd.read_csv("data/diamonds.csv")
df = df[df.cut.isin(["Ideal", "Good"])]
X = df.select_dtypes(["int64", "float64"])
y = df.cut
# Scaling
sc = StandardScaler()
X_scaled = sc.fit_transform(X)
# Create kf instance
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# Create knn instance
knn = KNeighborsClassifier()
# Create grid search instance
gscv = GridSearchCV(knn, {"n_neighbors": range(1, 52)}, cv=kf, n_jobs=-1)
gscv.fit(X_scaled, y)
# Get cross-validation data
cv_df = pd.DataFrame(gscv.cv_results_)
# Get plot data
plot_df = cv_df.loc[:, ["param_n_neighbors", "mean_test_score"]]
# Get k values versus mean_test_scores
plot_df.plot.line(x="param_n_neighbors", y="mean_test_score");
new_diamond = pd.DataFrame({
'carat': 0.24,
'depth': 60,
'table': 64,
'price': 400,
'x': 3,
'y': 3,
'z': 3
}, index=[0])
# Instantiate the best estimator
knn = gscv.best_estimator_
# Scale the new data in the same way as the training data (X)
nd_scaled = sc.transform(new_diamond)
# Predict the cut of the new diamond
knn.predict(nd_scaled)[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment