Train model:

from sklearn.model_selection import GridSearchCV

param_grid = [
# try 6 (3×2) combinations of hyperparameters
{'n_neighbors': [3, 5, 7], 'weights': ['uniform','distance']}
] knn_clf = KNeighborsClassifier()
# train across 3 folds, that's a total of 6*3=18 rounds of training
grid_search = GridSearchCV(knn_clf, param_grid, cv=3,
scoring='accuracy', return_train_score=True, n_jobs=-1)
grid_search.fit(X_train, y_train)

Show parameters of best model:

grid_search.best_params_

Show the score of train set:

grid_search.best_score_

Fit on test set:

y_pred = grid_search.predict(X_test)

Show the score of test set:

from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)

More about GridSearchCV: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html

[Machine Learning with Python] Cross Validation and Grid Search: An Example of KNN的更多相关文章

  1. 【Machine Learning】Python开发工具:Anaconda+Sublime

    Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...

  2. Python (1) - 7 Steps to Mastering Machine Learning With Python

    Step 1: Basic Python Skills install Anacondaincluding numpy, scikit-learn, and matplotlib Step 2: Fo ...

  3. Getting started with machine learning in Python

    Getting started with machine learning in Python Machine learning is a field that uses algorithms to ...

  4. 《Learning scikit-learn Machine Learning in Python》chapter1

    前言 由于实验原因,准备入坑 python 机器学习,而 python 机器学习常用的包就是 scikit-learn ,准备先了解一下这个工具.在这里搜了有 scikit-learn 关键字的书,找 ...

  5. 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)

    机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...

  6. Machine Learning的Python环境设置

    Machine Learning目前经常使用的语言有Python.R和MATLAB.如果采用Python,需要安装大量的数学相关和Machine Learning的包.一般安装Anaconda,可以把 ...

  7. [Machine Learning with Python] Familiar with Your Data

    Here I list some useful functions in Python to get familiar with your data. As an example, we load a ...

  8. [Machine Learning with Python] My First Data Preprocessing Pipeline with Titanic Dataset

    The Dataset was acquired from https://www.kaggle.com/c/titanic For data preprocessing, I firstly def ...

  9. [Machine Learning with Python] Data Preparation through Transformation Pipeline

    In the former article "Data Preparation by Pandas and Scikit-Learn", we discussed about a ...

随机推荐

  1. 牛客第四次多校Maximum Mode

    链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 题目描述 The mode of an integer sequence is the valu ...

  2. 初识Java之入门学习(扫盲)

    一,开发环境的配置 1. jdk1.8的安装 2. 环境变量的配置 3.MyEclipse8.5的安装 jdk是什么: JDK 是Java开发工具包 (Java Development Kit ) 的 ...

  3. CodeForces 781E Andryusha and Nervous Barriers 线段树 扫描线

    题意: 有一个\(h \times w\)的矩形,其中有\(n\)个水平的障碍.从上往下扔一个小球,遇到障碍后会分裂成两个,分别从障碍的两边继续往下落. 如果从太高的地方落下来,障碍会消失. 问从每一 ...

  4. C#+VisionPro连接相机获取图像的两种方式

    两种比较常用的方式. C#直接连接相机获取图像(GIGE) 在获取图像前,需要先创建一个相机对象,再使用这个相机对象的Acquire方法拍摄照片. ICogAcqFifo macqfifo;//定义相 ...

  5. Helloworld 在jvm 内存图

    HelloWorld.java源码如下:   public class HelloWorld { public static void main(String[] args) { String s ; ...

  6. day38--MySQL基础二

    1.数据库连表 1.1, 一对多 使用外键做约束.注意:外键列的数据类型要一致. 命令的方式创建外键CREATE table part1( nid int not null auto_incremen ...

  7. cf984e Elevator

    ref我好菜啊 #include <iostream> #include <cstring> #include <cstdio> #include <cmat ...

  8. 编程高手解读什么是NodeJs?

    首先在搞清楚什么NodeJs之前,我们先来聊聊JavaScript,只要做过开发的人都应该知道JavaScript是目前最为流行的前端(客户端)脚 本语言,JavaScript在Web项目中的使用率可 ...

  9. 【word ladder】cpp

    题目: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transform ...

  10. Python基础-week01 Python安装/变量/输入/及循环语句使用

      一.Python介绍 (1).目前Python主要应用领域: 云计算: 云计算最火的语言, 典型应用OpenStack WEB开发: 众多优秀的WEB框架,众多大型网站均为Python开发,You ...