Skip to content

Instantly share code, notes, and snippets.

@tfolkman
Created December 5, 2019 06:06
Show Gist options
  • Save tfolkman/1f122eb101011edc4a1456c70aef07fd to your computer and use it in GitHub Desktop.
Save tfolkman/1f122eb101011edc4a1456c70aef07fd to your computer and use it in GitHub Desktop.
neighbors
from sklearn.datasets import load_breast_cancer
from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import f1_score, classification_report, accuracy_score, mean_squared_error
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.20, random_state=42)
clf = KNeighborsClassifier()
gridsearch = GridSearchCV(clf, {"n_neighbors": [1, 3, 5, 7, 9, 11], "weights": ['uniform', 'distance'],
'p': [1, 2, 3]}, scoring='f1')
gridsearch.fit(X_train, y_train)
print("Best Params: {}".format(gridsearch.best_params_))
y_pred_train = gridsearch.predict(X_train)
print("Train F1: {}".format(f1_score(y_train, y_pred_train)))
print("Test Classification Report:")
y_pred_test = gridsearch.predict(X_test)
print(classification_report(y_test, y_pred_test))
print("Train Accuracy: {}\tTest accuracy: {}".format(accuracy_score(y_train, y_pred_train),
accuracy_score(y_test, y_pred_test)))
@rongche1
Copy link

Bro i learn something new from ur code thanks i really appreciate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment