Python машинное обучение scikit

Getting Started¶

The purpose of this guide is to illustrate some of the main features that scikit-learn provides. It assumes a very basic working knowledge of machine learning practices (model fitting, predicting, cross-validation, etc.). Please refer to our installation instructions for installing scikit-learn .

Scikit-learn is an open source machine learning library that supports supervised and unsupervised learning. It also provides various tools for model fitting, data preprocessing, model selection, model evaluation, and many other utilities.

Fitting and predicting: estimator basics¶

Scikit-learn provides dozens of built-in machine learning algorithms and models, called estimators . Each estimator can be fitted to some data using its fit method.

Here is a simple example where we fit a RandomForestClassifier to some very basic data:

>>> from sklearn.ensemble import RandomForestClassifier >>> clf = RandomForestClassifier(random_state=0) >>> X = [[ 1, 2, 3], # 2 samples, 3 features . [11, 12, 13]] >>> y = [0, 1] # classes of each sample >>> clf.fit(X, y) RandomForestClassifier(random_state=0) 

The fit method generally accepts 2 inputs:

  • The samples matrix (or design matrix) X . The size of X is typically (n_samples, n_features) , which means that samples are represented as rows and features are represented as columns.
  • The target values y which are real numbers for regression tasks, or integers for classification (or any other discrete set of values). For unsupervised learning tasks, y does not need to be specified. y is usually a 1d array where the i th entry corresponds to the target of the i th sample (row) of X .
Читайте также:  Red dot для css

Both X and y are usually expected to be numpy arrays or equivalent array-like data types, though some estimators work with other formats such as sparse matrices.

Once the estimator is fitted, it can be used for predicting target values of new data. You don’t need to re-train the estimator:

>>> clf.predict(X) # predict classes of the training data array([0, 1]) >>> clf.predict([[4, 5, 6], [14, 15, 16]]) # predict classes of new data array([0, 1]) 

Transformers and pre-processors¶

Machine learning workflows are often composed of different parts. A typical pipeline consists of a pre-processing step that transforms or imputes the data, and a final predictor that predicts target values.

In scikit-learn , pre-processors and transformers follow the same API as the estimator objects (they actually all inherit from the same BaseEstimator class). The transformer objects don’t have a predict method but rather a transform method that outputs a newly transformed sample matrix X :

>>> from sklearn.preprocessing import StandardScaler >>> X = [[0, 15], . [1, -10]] >>> # scale data according to computed scaling values >>> StandardScaler().fit(X).transform(X) array([[-1., 1.], [ 1., -1.]]) 

Sometimes, you want to apply different transformations to different features: the ColumnTransformer is designed for these use-cases.

Pipelines: chaining pre-processors and estimators¶

Transformers and estimators (predictors) can be combined together into a single unifying object: a Pipeline . The pipeline offers the same API as a regular estimator: it can be fitted and used for prediction with fit and predict . As we will see later, using a pipeline will also prevent you from data leakage, i.e. disclosing some testing data in your training data.

In the following example, we load the Iris dataset , split it into train and test sets, and compute the accuracy score of a pipeline on the test data:

>>> from sklearn.preprocessing import StandardScaler >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.pipeline import make_pipeline >>> from sklearn.datasets import load_iris >>> from sklearn.model_selection import train_test_split >>> from sklearn.metrics import accuracy_score . >>> # create a pipeline object >>> pipe = make_pipeline( . StandardScaler(), . LogisticRegression() . ) . >>> # load the iris dataset and split it into train and test sets >>> X, y = load_iris(return_X_y=True) >>> X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) . >>> # fit the whole pipeline >>> pipe.fit(X_train, y_train) Pipeline(steps=[('standardscaler', StandardScaler()), ('logisticregression', LogisticRegression())]) >>> # we can now use it like any other estimator >>> accuracy_score(pipe.predict(X_test), y_test) 0.97. 

Model evaluation¶

Fitting a model to some data does not entail that it will predict well on unseen data. This needs to be directly evaluated. We have just seen the train_test_split helper that splits a dataset into train and test sets, but scikit-learn provides many other tools for model evaluation, in particular for cross-validation .

We here briefly show how to perform a 5-fold cross-validation procedure, using the cross_validate helper. Note that it is also possible to manually iterate over the folds, use different data splitting strategies, and use custom scoring functions. Please refer to our User Guide for more details:

>>> from sklearn.datasets import make_regression >>> from sklearn.linear_model import LinearRegression >>> from sklearn.model_selection import cross_validate . >>> X, y = make_regression(n_samples=1000, random_state=0) >>> lr = LinearRegression() . >>> result = cross_validate(lr, X, y) # defaults to 5-fold CV >>> result['test_score'] # r_squared score is high because dataset is easy array([1., 1., 1., 1., 1.]) 

Automatic parameter searches¶

All estimators have parameters (often called hyper-parameters in the literature) that can be tuned. The generalization power of an estimator often critically depends on a few parameters. For example a RandomForestRegressor has a n_estimators parameter that determines the number of trees in the forest, and a max_depth parameter that determines the maximum depth of each tree. Quite often, it is not clear what the exact values of these parameters should be since they depend on the data at hand.

Scikit-learn provides tools to automatically find the best parameter combinations (via cross-validation). In the following example, we randomly search over the parameter space of a random forest with a RandomizedSearchCV object. When the search is over, the RandomizedSearchCV behaves as a RandomForestRegressor that has been fitted with the best set of parameters. Read more in the User Guide :

>>> from sklearn.datasets import fetch_california_housing >>> from sklearn.ensemble import RandomForestRegressor >>> from sklearn.model_selection import RandomizedSearchCV >>> from sklearn.model_selection import train_test_split >>> from scipy.stats import randint . >>> X, y = fetch_california_housing(return_X_y=True) >>> X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) . >>> # define the parameter space that will be searched over >>> param_distributions = 'n_estimators': randint(1, 5), . 'max_depth': randint(5, 10)> . >>> # now create a searchCV object and fit it to the data >>> search = RandomizedSearchCV(estimator=RandomForestRegressor(random_state=0), . n_iter=5, . param_distributions=param_distributions, . random_state=0) >>> search.fit(X_train, y_train) RandomizedSearchCV(estimator=RandomForestRegressor(random_state=0), n_iter=5, param_distributions=  'n_estimators': . >, random_state=0) >>> search.best_params_ >>> # the search object now acts like a normal random forest estimator >>> # with max_depth=9 and n_estimators=4 >>> search.score(X_test, y_test) 0.73. 

In practice, you almost always want to search over a pipeline , instead of a single estimator. One of the main reasons is that if you apply a pre-processing step to the whole dataset without using a pipeline, and then perform any kind of cross-validation, you would be breaking the fundamental assumption of independence between training and testing data. Indeed, since you pre-processed the data using the whole dataset, some information about the test sets are available to the train sets. This will lead to over-estimating the generalization power of the estimator (you can read more in this Kaggle post).

Using a pipeline for cross-validation and searching will largely keep you from this common pitfall.

Next steps¶

We have briefly covered estimator fitting and predicting, pre-processing steps, pipelines, cross-validation tools and automatic hyper-parameter searches. This guide should give you an overview of some of the main features of the library, but there is much more to scikit-learn !

Please refer to our User Guide for details on all the tools that we provide. You can also find an exhaustive list of the public API in the API Reference .

You can also look at our numerous examples that illustrate the use of scikit-learn in many different contexts.

The tutorials also contain additional learning resources.

Источник

scikit-learn

Classifier comparison

Predicting a continuous-valued attribute associated with an object.

Applications: Drug response, Stock prices. Algorithms: Gradient boosting, nearest neighbors, random forest, ridge, and more.

Decision Tree Regression with AdaBoost

Clustering

Automatic grouping of similar objects into sets.

Applications: Customer segmentation, Grouping experiment outcomes Algorithms: k-Means, HDBSCAN, hierarchical clustering, and more.

A demo of K-Means clustering on the handwritten digits data

Dimensionality reduction

Reducing the number of random variables to consider.

Applications: Visualization, Increased efficiency Algorithms: PCA, feature selection, non-negative matrix factorization, and more.

PCA example with Iris Data-set

Model selection

Comparing, validating and choosing parameters and models.

Applications: Improved accuracy via parameter tuning Algorithms: grid search, cross validation, metrics, and more.

Demonstration of multi-metric evaluation on cross_val_score and GridSearchCV

Preprocessing

Feature extraction and normalization.

Applications: Transforming input data such as text for use with machine learning algorithms. Algorithms: preprocessing, feature extraction, and more.

Demonstrating the different strategies of KBinsDiscretizer

News

  • On-going development:What’s new (Changelog)
  • June 2023. scikit-learn 1.3.0 is available for download (Changelog).
  • March 2023. scikit-learn 1.2.2 is available for download (Changelog).
  • January 2023. scikit-learn 1.2.1 is available for download (Changelog).
  • December 2022. scikit-learn 1.2.0 is available for download (Changelog).

Community

  • About us: See authors and contributing
  • More Machine Learning: Find related projects
  • Questions? See FAQ and stackoverflow
  • Subscribe to themailing list
  • Gitter:gitter.im/scikit-learn
  • Blog:blog.scikit-learn.org
  • Logos & Branding:logos and branding
  • Calendar:calendar
  • Twitter:@scikit_learn
  • LinkedIn:linkedin/scikit-learn
  • YouTube:youtube.com/scikit-learn
  • Facebook:@scikitlearnofficial
  • Instagram:@scikitlearnofficial
  • TikTok:@scikit.learn
  • Communication on all channels should respect PSF’s code of conduct.

Who uses scikit-learn?

inria

«We use scikit-learn to support leading-edge basic research [. ]»

spotify

«I think it’s the most well-designed ML package I’ve seen so far.»

«scikit-learn’s ease-of-use, performance and overall variety of algorithms implemented has proved invaluable [. ].»

telecomparistech

«The great benefit of scikit-learn is its fast learning curve [. ]»

aweber

«It allows us to do AWesome stuff we would not otherwise accomplish»

yhat

«scikit-learn makes doing advanced analysis in Python accessible to anyone.»

scikit-learn development and maintenance are financially supported by

Источник

Оцените статью