Random selection of training data and test data of Sklearn

Source: Internet
Author: User

When we classify, we need to divide the data into two parts, part of which is the test data, part of the training data. Sklearn can randomly select the training data and test data according to the set proportion, and the sample and label are the corresponding groupings.
The experimental code is as follows:

#!/usr/bin/env python
#-*-coding:utf-8-*-"" "
Feature: Datasets are scaled to training sets and test set
times: March 11, 2017 12:48:57
" " From

sklearn.cross_validation import train_test_split

# Generates 200 sentences, the first 100 and the last 100 categories correspond to 1 and 2
X respectively = [[u] This is ", U" 1th ", U" Test "]] * + [[u" This is ", U" 2nd ", U" Test "]]] *
y = [1] * + [2] *

The test set of randomly selected 20%
x_train, X_test, Y_ Train, Y_test = Train_test_split (X, y, test_size=0.2)
print (Len (x_train), Len (x_test))

# See if the sentences and labels still correspond
for I in range (len (x_test)):
    print ("". Join (X_test[i]), y_test[i])

if __name__ = = "__main__":
    Pass

The experimental results are as follows:

160 40 This is the 2nd Test 2 This is the 2nd Test 2 This is the 2nd Test 2 This is the 1th Test 1 This is the 1th Test 1 This is the 1th Test 1 This is the 1th Test 1 This is the 1th Test 1 This is the 2nd Test 2 This is the 1th Test 1 This is the 2nd Test 2 This is the 1th Test 1 This is the 1th Test 1 This is the 1th Test 1 This is the 1th Test 1 This is 1th Test 1 This is the 2nd Test 2 This is the 2nd Test 2 This is the 2nd Test 2 This is the 1th Test 1 This is the 2nd Test 2 this is 1th Test 1 This is the 1th Test 1 This is the 2nd Test 2 This is the 1th Test 1 This is the 1th Test 1 This is 2nd Test 2 This is 1th Test 1 This is the 2nd Test 2 This is the 2nd Test 2 This is the 2nd Test 2 This is the 1th Test 1 This is the 2nd Test 2 this is 2nd Test 2 This is the 1th Test 1 This is the 1th Test 1 This is the 1th Test 1 This is the 2nd Test 2 This is the 1th Test 1 This is the 2nd Test 2 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.