sklearn.model_selection.train_test_split使用

sklearn.model_selection.train_test_split随机划分训练集和测试集
官网文档:http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html#sklearn.model_selection.train_test_split
一般形式:
train_test_split是交叉验证中常用的函数,功能是从样本中随机的按比例选取train_data和test_data,形式为:

X_train,X_test, y_train, y_test =cross_validation.train_test_split(train_data,train_target,test_size=0.4, random_state=0)

cross_validatio为交叉验证

参数解释:
train_data:所要划分的样本特征集
train_target:所要划分的样本结果
test_size:样本占比,如果是整数的话就是样本的数量
random_state:是随机数的种子。
随机数种子:其实就是该组随机数的编号,在需要重复试验的时候,保证得到一组一样的随机数。比如你每次都填1,其他参数一样的情况下你得到的随机数组是一样的。但填0或不填,每次都会不一样。
随机数的产生取决于种子,随机数和种子之间的关系遵从以下两个规则:

种子不同,产生不同的随机数;种子相同,即使实例不同也产生相同的随机数。

[python]  view plain  copy
  1. import numpy as np  
  2. from sklearn.model_selection import train_test_split  
  3.   
  4. X,y=np.arange(10).reshape((5,2)),range  
  5. X=np.array([[0,1],[2,3],[4,5],[6,7],[8,9]])  
  6. y=[0,1,2,3,4]  
  7. print(X)  
  8. print(y)  
  9. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)  
  10. print(X_train)  
  11. print(y_train)  
  12. print(X_test)  
  13. print(y_test)  
结果为

[python]  view plain  copy
  1. [[0 1]  
  2.  [2 3]  
  3.  [4 5]  
  4.  [6 7]  
  5.  [8 9]]  
  6. [01234]  
  7. [[2 3]  
  8.  [6 7]  
  9.  [8 9]]  
  10. [134]  
  11. [[4 5]  
  12.  [0 1]]  
  13. [20]  

猜你喜欢

转载自blog.csdn.net/scc_722/article/details/80642655
今日推荐