当前位置:网站首页>KNN, cross validation, grid search
KNN, cross validation, grid search
2022-04-22 04:47:00 【weixin_ thirty-eight million eight hundred and seventy-one thou】
What is cross validation (cross validation)
Cross validation : The training data you will get , Divided into training and validation sets . The following is an example : Divide the data into 4 Share , One of them is used as verification set . And then pass by 4 Time ( Group ) Test of , Change the validation set every time . Or get 4 The results of the group model , Take the average as the final result . also called 4 Crossover verification .
The training set is divided into verification set and training set
Fixed test set
Can't improve the accuracy of the model
Intelligence increases credibility
The grid search ------- Tuning parameters
Confirmation of hyperparametric problems, etc
Usually , There are many parameters that need to be specified manually ( Such as k- In the nearest neighbor algorithm K value ), This is called superparameter . But the manual process is complicated , So we need to preset several super parameter combinations for the model . Each group of super parameters was evaluated by cross validation . Finally, the optimal combination of parameters is selected to establish the model .
API
sklearn.model_selection.GridSearchCV(estimator, param_grid=None,cv=None)
Detailed search for the specified parameter value of the estimator
estimator: Estimator objects
param_grid: Estimator parameters (dict){“n_neighbors”:[1,3,5]}
cv: Specify a few fold cross validation
fit: Input training data
score: Accuracy rate
Result analysis :
bestscore__: The best results in cross validation
bestestimator: The best parametric model
cvresults: The accuracy results of verification set and training set after each cross validation
explain :
For example, ten
Take nine at a time for training , Take one to verify
Each time take a different one as the verification set , You can do 10 Fold validation
Get ten different results , Average.

If it is n Share , You can do n Secondary cross validation
The grid search : The optimal combination is obtained by cross validation

Here are some Inline code slice .
iris = load_iris()
# 2、 Basic data processing -- Divide the data set
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
# 3、 Feature Engineering : Standardization
# Instantiate a converter class
transfer = StandardScaler()
# call fit_transform
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4、KNN The predictor process
# 4.1 Instantiate the predictor class
estimator = KNeighborsClassifier()
# 4.2 Model selection and tuning —— Grid search and cross validation
# Prepare the hyper parameters to be adjusted
param_dict = {"n_neighbors": [1, 3, 5]}
estimator = GridSearchCV(estimator, param_grid=param_dict, cv=3)
# 4.3 fit Data for training
estimator.fit(x_train, y_train)
# 5、 Evaluate the effect of the model
# Method a: Compare the predicted results with the real values
y_predict = estimator.predict(x_test)
print(" Compare the predicted results with the real values :\n", y_predict == y_test)
# Method b: Direct calculation accuracy
score = estimator.score(x_test, y_test)
print(" Direct calculation accuracy :\n", score)
print(" The best results in cross validation :\n", estimator.best_score_)
print(" The best parametric model :\n", estimator.best_estimator_)
print(" Accuracy results after each cross validation :\n", estimator.cv_results_)

版权声明
本文为[weixin_ thirty-eight million eight hundred and seventy-one thou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210804321358.html
边栏推荐
- 2022T电梯修理考试试题及在线模拟考试
- Error encountered when installing opencv
- Es next related
- Cloud network integration - computing power center - roce / rmda and nvme / nvme of
- Linked list Chapter 4
- 仿真生成随机数计算生成每个同学生日
- [untitled]
- 12. Libevent loop function and exit test
- labelme的常用命令
- How to realize different values and display different colors in MATLAB
猜你喜欢

2022山东省安全员C证特种作业证考试题库及答案

【S32K3_STM&PIT_MCAL】

kaggle實戰4.1--時間序列預測問題

Queue II

Carina's foundation and birth background | deeply understand the first issue of carina series

Carina local storage selected into the CNCF cloud native panorama

CommDGI: Community detection oriented deep graph infomax 2020 CIKM

Boyun beyondcmp cloud management platform version 5.6 release

博云 BeyondCMP 云管理平台 5.6 版本发布
![[stack and queue] simple application of C language ⌊ stack and queue realize each other, circular queue ⌉](/img/ac/d0d76fd109a53da73c624631f90c00.png)
[stack and queue] simple application of C language ⌊ stack and queue realize each other, circular queue ⌉
随机推荐
Queue summary (Part I)
C-随手写10
10.libevent接收和处理服务器消息
crypto-js加密算法库【安装教程、缓存加密】
排序一
JVM tuning notes
Pycharm + Anaconda installation package
kaggle实战4.1--时间序列预测问题
Leetcode refers to offer 18 Delete the node of the linked list
jsp hello world中文乱码
Cloud network integration - computing power center - roce / rmda and nvme / nvme of
2022 question bank and answers of Shandong Province safety officer C certificate special operation certificate examination
2022T电梯修理考试练习题及在线模拟考试
15. Bufferevent client test connection server
How the CPU calls the process
2022t elevator repair test exercises and online simulation test
2022G2电站锅炉司炉考试练习题及在线模拟考试
14.buffferevent超时事件处理
C language simple [stack and queue] (bracket matching problem)
【板栗糖GIS】supermap—如何為數據制造超鏈接