[Machine Learning with Python] Cross Validation and Grid Search: An Example of KNN

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

原文地址:https://www.cnblogs.com/sherrydatascience/p/10206790.html

时间: 2024-08-02 05:02:33

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

Machine learning with python - Linear Regression

Machine learning with python Linear Regression 数据来自 cs229  Problem Set 1 (pdf) Data: q1x.dat, q1y.dat, q2x.dat, q2y.dat PS1 Solution (pdf) 从左上往右下 batchGradientDescent的cost随迭代次数的增加而下降,和收敛结果 stochasticGradientDescent的cost随迭代次数的增加而下降,和收敛结果 normalEquatio

Getting started with machine learning in Python

Getting started with machine learning in Python Machine learning is a field that uses algorithms to learn from data and make predictions. Practically, this means that we can feed data into an algorithm, and use it to make predictions about what might

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

Step 1: Basic Python Skills install Anacondaincluding numpy, scikit-learn, and matplotlib Step 2: Foundational Machine Learning Skills Unofficial Andrew Ng course notes Tom Mitchell Machine Learning Lectures Step 3: Scientific Python Packages Overvie

【Machine learning(python篇)】-几种常用的数据结构

python中有多种数据结构,数据结构之间也可以相互转化,概念一多就容易使人混淆,对于初学者来说本来很容的概念,最终却变成了噩梦,很难区分不同数据结构之间的用法,这样就会造成乱用数据结构,致使运行效率低下.对于较简单的程序来说乱用数据结构不会有太大的问题,但涉及到大数据运算,可能一个数据类型就会导致内存吃满,这时善用数据结构就会变的尤为重要. 一.list列表类型 list类型是Python中内置的, list中包含的数据之间的类型可以不相同,并且list中数据保存的是数据的指针,因为数据类型不

machine learning in python:根据关键字合并多个表(构建组合feature)

三张表:train_set.csv:test_set.csv:feature.csv.三张表通过object_id关联. <pre name="code" class="python"><strong><span style="font-size:18px;">import pandas as pd import numpy as np # load training and test datasets tra

Python Machine Learning 中文版

Python机器学习 机器学习,如今最令人振奋的计算机领域之一.看看那些大公司,Google.Facebook.Apple.Amazon早已展开了一场关于机器学习的军备竞赛.从手机上的语音助手.垃圾邮件过滤到逛淘宝时的物品推荐,无一不用到机器学习技术. 如果你对机器学习感兴趣,甚至是想从事相关职业,那么这本书非常适合作为你的第一本机器学习资料.市面上大部分的机器学习书籍要么是告诉你如何推导模型公式要么就是如何代码实现模型算法,这对于零基础的新手来说,阅读起来相当困难.而这本书,在介绍必要的基础概

Awesome Machine Learning

Awesome Machine Learning  A curated list of awesome machine learning frameworks, libraries and software (by language). Inspired by awesome-php. If you want to contribute to this list (please do), send me a pull request or contact me @josephmisiti Als

machine learning in action , part 1

We should think in below four questions: the decription of machine learning key tasks in machine learning why you need to learn about machine learning why python is so great for machine learning 1.The author talked some examples about machine learnin

How to Grid Search Hyperparameters for Deep Learning Models in Python With Keras

Hyperparameter optimization is a big part of deep learning. The reason is that neural networks are notoriously difficult to configure and there are a lot of parameters that need to be set. On top of that, individual models can be very slow to train.