Home > Backend Development > Python Tutorial > Cross validation and python code implementation

Cross validation and python code implementation

零到壹度
Release: 2018-04-16 11:45:33
Original
10296 people have browsed it

The content introduced in this article is about cross-validation and python code implementation. It has certain reference value. Now I share it with you. Friends in need can refer to the two types of model selection

Methods: regularization (typical method), cross-validation.

Here is an introduction to cross-validation and its python code implementation.

Cross-validation

If there is sufficient data for a given sample, a simple way to perform model selection is to randomly divide the data set It is divided into 3 parts, namely training set, verification set and test set.

Training set: Training model

##Validation set: Model selection

Test set: Final evaluation of the model

In the models learned with different complexities, choose the validation set that has Model with minimum prediction error. Since the validation set has enough data, it is also effective to use it for model selection. In many practical applications where data are insufficient, cross-validation methods can be used.

Basic idea: Use data repeatedly, segment the given data into training sets and test sets, and then repeatedly perform training, testing and model selection on this basis.

Simple cross-validation:

Randomly divide the data into two parts, the training set and the test set. Generally, 70% of the data is the training set and 30% is the test set.

Code (dividing training set, test set):

from sklearn.cross_validation import train_test_split
# data (全部数据)   labels(全部目标值)     X_train 训练集(全部特征)  Y_train 训练集的目标值
X_train, X_test, Y_train, Y_test = train_test_split(data,labels, test_size=0.25, random_state=0) #这里训练集75%:测试集25%
Copy after login

random_state

Source code explanation: int, RandomState instance or None, optional (default=None)

## int, RandomState instance or None, optional (default=None)If int, random_state is the seed used by the random number generator;

If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used
by `np.random`.


The main idea is: if you set a specific value, such as random_state=10 , the data after each division will be the same, even if it is run multiple times. If set to None, that is, random_state=None, the data after each division will be different, and the data will be different each time the division is run.

Code (dividing training set, validation set, test set):

from sklearn import cross_validation

train_and_valid, test = cross_validation.train_test_split(data, test_size=0.3,random_state=0)  # 先分为两部分:训练和验证  ,  测试集
train, valid = cross_validation.train_test_split(data, test_size=0.5,random_state=0)   # 再把训练和验证分为:训练集 ,验证集
Copy after login

Related recommendations:

Cross-validation

3 kinds of cross-validation

The use of cross-validation

Why use cross validation

The above is the detailed content of Cross validation and python code implementation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template