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