zoukankan      html  css  js  c++  java
  • 机器学习sklearn(四): 数据处理(一)数据集拆分(一)train_test_split

    train_test_split

    In scikit-learn a random split into training and test sets can be quickly computed with the train_test_split helper function. Let’s load the iris data set to fit a linear support vector machine on it:

    >>> import numpy as np
    >>> from sklearn.model_selection import train_test_split
    >>> from sklearn import datasets
    >>> from sklearn import svm
    
    >>> X, y = datasets.load_iris(return_X_y=True)
    >>> X.shape, y.shape
    ((150, 4), (150,))

    We can now quickly sample a training set while holding out 40% of the data for testing (evaluating) our classifier:

    >>> X_train, X_test, y_train, y_test = train_test_split(
    ...     X, y, test_size=0.4, random_state=0)
    
    >>> X_train.shape, y_train.shape
    ((90, 4), (90,))
    >>> X_test.shape, y_test.shape
    ((60, 4), (60,))
    
    >>> clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train)
    >>> clf.score(X_test, y_test)
    0.96...

    API

    sklearn.model_selection.train_test_split(*arraystest_size=Nonetrain_size=Nonerandom_state=Noneshuffle=Truestratify=None)[source]

    Split arrays or matrices into random train and test subsets

    Quick utility that wraps input validation and next(ShuffleSplit().split(X, y)) and application to input data into a single call for splitting (and optionally subsampling) data in a oneliner.

    Read more in the User Guide.

    Parameters
    *arrayssequence of indexables with same length / shape[0]

    Allowed inputs are lists, numpy arrays, scipy-sparse matrices or pandas dataframes.

    test_sizefloat or int, default=None

    If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is set to the complement of the train size. If train_size is also None, it will be set to 0.25.

    train_sizefloat or int, default=None

    If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size.

    random_stateint, RandomState instance or None, default=None

    Controls the shuffling applied to the data before applying the split. Pass an int for reproducible output across multiple function calls. See Glossary.

    shufflebool, default=True

    Whether or not to shuffle the data before splitting. If shuffle=False then stratify must be None.

    stratifyarray-like, default=None

    If not None, data is split in a stratified fashion, using this as the class labels. Read more in the User Guide.

    Returns
    splittinglist, length=2 * len(arrays)

    List containing train-test split of inputs.

    New in version 0.16: If the input is sparse, the output will be a scipy.sparse.csr_matrix. Else, output type is the same as the input type.

    Examples

    >>> import numpy as np
    >>> from sklearn.model_selection import train_test_split
    >>> X, y = np.arange(10).reshape((5, 2)), range(5)
    >>> X
    array([[0, 1],
           [2, 3],
           [4, 5],
           [6, 7],
           [8, 9]])
    >>> list(y)
    [0, 1, 2, 3, 4]
    >>> X_train, X_test, y_train, y_test = train_test_split(
    ...     X, y, test_size=0.33, random_state=42)
    ...
    >>> X_train
    array([[4, 5],
           [0, 1],
           [6, 7]])
    >>> y_train
    [2, 0, 3]
    >>> X_test
    array([[2, 3],
           [8, 9]])
    >>> y_test
    [1, 4]
    >>> train_test_split(y, shuffle=False)
    [[0, 1, 2], [3, 4]]
  • 相关阅读:
    (转) asp.net中使用ajax中的三种方式
    转ASP.NET 防盗链的实现[HttpHandler]
    (转)ADO.net,Linq to SQL和Entity Framework性能实测分析
    (转) JS日历控件集合附效果图、源代码
    正则表达式收集
    Asp.net 打开页面错误 (无法显示 XML 页。使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。)
    SQL优化原则
    转载 25个优秀的 ASP.NET MVC教程及文章
    SQL 时间格式格式化
    任务失败,原因是未找到“LC.exe”,或未安装正确的 Microsoft Windows SDK。
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14883662.html
Copyright © 2011-2022 走看看