手机看片欧美日韩,久久精品国产主播一区二区,欧美亚洲中日韩中文字幕在线

通過Scikit-learn進行機器學習的介紹

An introduction to machine learning with scikit-learn

Section contents

In this section, we introduce the?machine learning?vocabulary that we use throughout scikit-learn and give a simple learning example.

?

在這一章節,我們將介紹機器學習中的 scikit-learn 以及一些學習例子。

Machine learning: the problem setting? 機器學習:問題設置

In general, a learning problem considers a set of n?samples?of data and then tries to predict properties of unknown data. If each sample is more than a single number and, for instance, a multi-dimensional entry (aka?multivariate?data), it is said to have several attributes or?features.

通常來講,一個學習問題涉及到一個含有n個樣本數據的集合,從而去預測數據中一些未知的特征。如果每個樣本中有多于一個數字并且,比方說,一個多維輸入(又叫做多元變量數據),它可以被稱作有多種特征、貢獻。

We can separate learning problems in a few large categories:?我們可以把學習問題分為幾個類別。

  • supervised????? learning, in which the data comes with additional????? attributes that we want to predict (Click here?to????? go to the scikit-learn supervised learning page).This problem can be????? either:

監督學習,其中數據帶有我們想要預測的額外屬性(點擊此處轉到scikit學習監督學習頁面)。這個問題可以是:

  • classification:????? samples belong to two or more classes and we want to learn from already????? labeled data how to predict the class of unlabeled data. An example of????? classification problem would be the handwritten digit recognition example,????? in which the aim is to assign each input vector to one of a finite number????? of discrete categories. Another way to think of classification is as a????? discrete (as opposed to continuous) form of supervised learning where one????? has a limited number of categories and for each of the n samples provided,????? one is to try to label them with the correct category or class.

分類:樣本屬于兩個或更多個類,我們想從已標記的數據中學習如何預測未標記數據的類別。 分類問題的一個例子是手寫數字識別示例,其目的是將每個輸入向量分配給有限數目的離散類別之一。 分類的另一種方式是作為監督學習的離散(而不是連續的)形式,其中提供的n個樣本中的每一個樣本都有一個有限數量的類別,另一方式是嘗試用正確的類別或類別來標記它們。

  • regression:? if the desired output consists of one or more continuous variables, then????? the task is called?regression.? An example of a regression problem would be the prediction of the length? of a salmon as a function of its age and weight.

回歸:如果期望的輸出由一個或多個連續變量組成,則該任務稱為回歸。 回歸問題的一個例子是鮭魚年齡和體重的函數預測其長度。

  • unsupervised????? learning, in which the training data consists of a set? of input vectors x without any corresponding target values. The goal in? such problems may be to discover groups of similar examples within the? data, where it is called?clustering, or to determine the distribution of data within the input space, known as?density????? estimation, or to project the data from a high-dimensional space down to two or three dimensions for the purpose of?visualization?(Click here?to go to the Scikit-Learn unsupervised learning page).

無監督學習,其中訓練數據由一組沒有任何相應目標值的輸入向量x組成。 這些問題的目標可能是在數據中發現類似示例的組,稱為聚類,或者確定輸入空間內的數據分布,稱為密度估計,或從高維數據投影數據 空間縮小到二維或三維以進行可視化(點擊此處轉到Scikit-Learn無人值守學習頁面)。

Training set and testing set

培訓集和測試集

Machine learning is about learning some properties of a data set and applying them to new data. This is why a common practice in machine learning to evaluate an algorithm is to split the data at hand into two sets, one that we call thetraining set?on which we learn data properties and one that we call the?testing set?on which we test these properties.

機器學習是關于學習數據集的某些屬性并將其應用于新數據。這就是為什么機器學習評估算法的常見做法是將手頭的數據拆分成兩組,用于學習數據屬性的我們稱之為訓練集,用于測試這些屬性的我們稱之為測試集。

Loading an example dataset

加載示例數據集

scikit-learn comes with a few standard datasets, for instance the?iris?and?digits?datasets for classification and the?boston house prices dataset?for regression.

scikit-learn提供了幾個標準數據集,例如用于分類的虹膜和數字數據集和波士頓房價回歸數據集。

In the following, we start a Python interpreter from our shell and then load the iris and digits datasets. Our notational convention is that $ denotes the shell prompt while >>> denotes the Python interpreter prompt:

在下文中,我們從我們的shell啟動一個Python解釋器,然后加載虹膜和數字數據集。我們的符號約定是$表示shell提示符,而>>>表示Python解釋器提示符:

$ python

>>> from sklearn import datasets

>>> iris = datasets.load_iris()

>>> digits = datasets.load_digits()

A dataset is a dictionary-like object that holds all the data and some metadata about the data. This data is stored in the.data member, which is a n_samples, n_features array. In the case of supervised problem, one or more response variables are stored in the .target member. More details on the different datasets can be found in the?dedicated section.

數據集是一個類似字典的對象,它保存有關數據的所有數據和一些元數據。該數據存儲在.data成員中,它是一個n_samples,n_features數組。在監督問題的情況下,一個或多個響應變量存儲在.target成員中。有關不同數據集的更多詳細信息,請參見專用部分。

For instance, in the case of the digits dataset, digits.data gives access to the features that can be used to classify the digits samples:

例如,在數字數據集的情況下,digits.data可以訪問用于對數字樣本進行分類的功能:

>>>

>>>?print(digits.data)??

[[??0.?? 0.?? 5. ...,?? 0.?? 0.?? 0.]

?[? 0.?? 0.?? 0. ...,? 10.?? 0.?? 0.]

?[? 0.?? 0.?? 0. ...,? 16.?? 9.?? 0.]

?...,

?[? 0.?? 0.?? 1. ...,?? 6.?? 0.?? 0.]

?[? 0.?? 0.?? 2. ...,? 12.?? 0.?? 0.]

?[? 0.?? 0.? 10. ...,? 12.?? 1.?? 0.]]

and digits.target gives the ground truth for the digit dataset, that is the number corresponding to each digit image that we are trying to learn:

而digit.target為數字數據集提供了實質,即我們正在嘗試學習的每個數字圖像對應的數字:

>>>

>>>?digits.target

array([0, 1, 2, ..., 8, 9, 8])

Shape of the data arrays

數據陣列的形狀

The data is always a 2D array, shape (n_samples, n_features), although the original data may have had a different shape. In the case of the digits, each original sample is an image of shape (8, 8) and can be accessed using:

數據總是2D數組,形狀(n_samples,n_features),盡管原始數據可能具有不同的形狀。 在數字的情況下,每個原始樣本是形狀(8,8)的圖像,可以使用以下方式訪問:

>>>

>>>?digits.images[0]

array([[??0.,?? 0.,?? 5.,? 13.,?? 9.,?? 1.,?? 0.,?? 0.],

???????[? 0.,?? 0.,? 13.,? 15.,? 10.,? 15.,?? 5.,?? 0.],

???????[? 0.,?? 3.,? 15.,?? 2.,?? 0.,? 11.,?? 8.,?? 0.],

???????[? 0.,?? 4.,? 12.,?? 0.,?? 0.,?? 8.,?? 8.,?? 0.],

???????[? 0.,?? 5.,?? 8.,?? 0.,?? 0.,?? 9.,?? 8.,?? 0.],

???????[? 0.,?? 4.,? 11.,?? 0.,?? 1.,? 12.,?? 7.,?? 0.],

???????[? 0.,?? 2.,? 14.,?? 5.,? 10.,? 12.,?? 0.,?? 0.],

???????[? 0.,?? 0.,?? 6.,? 13.,? 10.,?? 0.,?? 0.,?? 0.]])

The?simple example on this dataset?illustrates how starting from the original problem one can shape the data for consumption in scikit-learn.

這個數據集的簡單例子說明了如何從原始問題開始,通過scikit-learn形成消費數據。

Loading from external datasets

從外部數據集加載

To load from an external dataset, please refer to?loading external datasets.

要從外部數據集加載,請參閱加載外部數據集。

?

Learning and predicting

學習和預測

In the case of the digits dataset, the task is to predict, given an image, which digit it represents. We are given samples of each of the 10 possible classes (the digits zero through nine) on which we?fit?an?estimator?to be able to?predict?the classes to which unseen samples belong.

在數字數據集的情況下,我們的任務是用已給的圖像來預測其表示的數字。我們給出了10個可能的類別(數字0到9)中的每一個的樣本,在這些類別上我們擬合一個估計器來預測不可見樣本所屬的類別。

In scikit-learn, an estimator for classification is a Python object that implements the methods fit(X, y) and predict(T).

在scikit-learn中,分類的估計器是一個Python對象,它實現了fit(X,y)和predict(T)的方法。

An example of an estimator is the class sklearn.svm.SVC that implements?support vector classification. The constructor of an estimator takes as arguments the parameters of the model, but for the time being, we will consider the estimator as a black box:

估計器的一個例子是實現支持向量分類的類sklearn.svm.SVC。估計器的構造函數以模型的參數為參數,但目前我們將把估計器視為黑盒子:

>>>

>>>?from?sklearn?import?svm

>>>?clf?=?svm.SVC(gamma=0.001, C=100.)

Choosing the parameters of the model

選擇模型的參數

In this example we set the value of gamma manually. It is possible to automatically find good values for the parameters by using tools such as?grid search?and?cross validation.

在這個例子中,我們手動設置gamma值。通過使用諸如網格搜索和交叉驗證等工具,可以自動找到參數的良好值。

We call our estimator instance clf, as it is a classifier. It now must be fitted to the model, that is, it must?learn?from the model. This is done by passing our training set to the fit method. As a training set, let us use all the images of our dataset apart from the last one. We select this training set with the [:-1] Python syntax, which produces a new array that contains all but the last entry of digits.data:

我們稱我們的估計器為實例clf,因為它是一個分類器。現在它必須適應模型,也就是說,它必須從模型中學習。這是通過我們的訓練集過渡到適合的方法來完成的。作為一個訓練集,讓我們使用除最后一個數據集的所有圖像。我們用[:-1] Python語法選擇這個訓練集,它產生一個包含除去digits.data的最后一個數據的新數組:

>>>

>>>?clf.fit(digits.data[:-1], digits.target[:-1])??

SVC(C=100.0, cache_size=200, class_weight=None, coef0=0.0,

??decision_function_shape=None, degree=3, gamma=0.001, kernel='rbf',

??max_iter=-1, probability=False, random_state=None, shrinking=True,

??tol=0.001, verbose=False)

Now you can predict new values, in particular, we can ask to the classifier what is the digit of our last image in the digitsdataset, which we have not used to train the classifier:

現在你可以預測新的值,特別是我們可以向分類器詢問在digits數據集中我們最后一個圖像的數字是什么,我們還沒有用過它來訓練分類器:

>>>

>>>?clf.predict(digits.data[-1:])

array([8])

The corresponding image is the following:

相應的圖像如下:

?

As you can see, it is a challenging task: the images are of poor resolution. Do you agree with the classifier?

A complete example of this classification problem is available as an example that you can run and study:?Recognizing hand-written digits.

正如你所看到的,這是一項具有挑戰性的任務:圖像的分辨率差。你同意分類器嗎?

這個分類問題的一個完整例子可以用來作為一個例子來運行和學習:識別手寫數字。

?

Model persistence

模型持久性

It is possible to save a model in the scikit by using Python’s built-in persistence model, namely?pickle:

可以通過使用Python的內置持久性模型(即pickle)將模型保存在scikit中:

>>>

>>>?from?sklearn?import?svm

>>>?from?sklearn?import?datasets

>>>?clf?=?svm.SVC()

>>>?iris?=?datasets.load_iris()

>>>?X, y?=?iris.data, iris.target

>>>?clf.fit(X, y)??

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

??decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',

??max_iter=-1, probability=False, random_state=None, shrinking=True,

??tol=0.001, verbose=False)

>>>?import?pickle

>>>?s?=?pickle.dumps(clf)

>>>?clf2?=?pickle.loads(s)

>>>?clf2.predict(X[0:1])

array([0])

>>>?y[0]

0

In the specific case of the scikit, it may be more interesting to use joblib’s replacement of pickle (joblib.dump &joblib.load), which is more efficient on big data, but can only pickle to the disk and not to a string:

在scikit的具體情況下,使用joblib替換pickle(joblib.dump和joblib.load)可能會更有意思,這對大數據更有效,但只能pickle(腌制)到磁盤而不是字符串:

>>>

>>>?from?sklearn.externals?import?joblib

>>>?joblib.dump(clf,?'filename.pkl')?

Later you can load back the pickled model (possibly in another Python process) with:

稍后,您可以使用以下方式加載腌制模型(可能在另一個Python進程中):

>>>

>>>?clf?=?joblib.load('filename.pkl')?

Note?注意

joblib.dump and joblib.load functions also accept file-like object instead of filenames. More information on data persistence with Joblib is available?here.

Note that pickle has some security and maintainability issues. Please refer to section?Model persistence?for more detailed information about model persistence with scikit-learn.

joblib.dump和joblib.load函數也接受類似文件的對象而不是文件名。 有關Joblib數據持久性的更多信息,請點擊?here

請注意,pickle有一些安全性和可維護性問題。 有關使用scikit-learn的模型持久性的更多詳細信息,請參閱?Model persistence

?

Conventions

規則

scikit-learn estimators follow certain rules to make their behavior more predictive.

scikit-learn估計器遵循某些規則,使其行為更具預測性。

Type casting

類型鑄造

Unless otherwise specified, input will be cast to float64:

除非另有說明,否則輸入將被轉換為float64:

>>>

>>>?import?numpy?as?np

>>>?from?sklearn?import?random_projection

>>>?rng?=?np.random.RandomState(0)

>>>?X?=?rng.rand(10,?2000)

>>>?X?=?np.array(X, dtype='float32')

>>>?X.dtype

dtype('float32')

>>>?transformer?=?random_projection.GaussianRandomProjection()

>>>?X_new?=?transformer.fit_transform(X)

>>>?X_new.dtype

dtype('float64')

In this example, X is float32, which is cast to float64 by fit_transform(X).

Regression targets are cast to float64, classification targets are maintained:

在這個例子中,X是float32,它被fit_transform(X)轉換為float64。

回歸目標被轉換為float64,維護分類目標:

>>>

>>>?from?sklearn?import?datasets

>>>?from?sklearn.svm?import?SVC

>>>?iris?=?datasets.load_iris()

>>>?clf?=?SVC()

>>>?clf.fit(iris.data, iris.target)??

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

??decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',

??max_iter=-1, probability=False, random_state=None, shrinking=True,

??tol=0.001, verbose=False)

>>>?list(clf.predict(iris.data[:3]))

[0, 0, 0]

>>>?clf.fit(iris.data, iris.target_names[iris.target])??

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

??decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',

??max_iter=-1, probability=False, random_state=None, shrinking=True,

??tol=0.001, verbose=False)

>>>?list(clf.predict(iris.data[:3]))??

['setosa', 'setosa', 'setosa']

Here, the first predict() returns an integer array, since iris.target (an integer array) was used in fit. The secondpredict() returns a string array, since iris.target_names was for fitting.

這里,第一個predict()返回一個整數數組,因為使用了iris.target(一個整數數組)。 Secondpredict()返回一個字符串數組,因為iris.target_names是用于擬合的。

Refitting and updating parameters

修改和更新參數

Hyper-parameters of an estimator can be updated after it has been constructed via thesklearn.pipeline.Pipeline.set_params?method. Calling fit() more than once will overwrite what was learned by any previous fit():

估計器的超參數可以在通過sklearn.pipeline.Pipeline.set_params方法構建后進行更新。?多次調用fit()將覆蓋以前的fit()中學到的內容:

>>>

>>>?import?numpy?as?np

>>>?from?sklearn.svm?import?SVC

>>>?rng?=?np.random.RandomState(0)

>>>?X?=?rng.rand(100,?10)

>>>?y?=?rng.binomial(1,?0.5,?100)

>>>?X_test?=?rng.rand(5,?10)

>>>?clf?=?SVC()

>>>?clf.set_params(kernel='linear').fit(X, y)??

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

??decision_function_shape=None, degree=3, gamma='auto', kernel='linear',

??max_iter=-1, probability=False, random_state=None, shrinking=True,

??tol=0.001, verbose=False)

>>>?clf.predict(X_test)

array([1, 0, 1, 1, 0])

>>>?clf.set_params(kernel='rbf').fit(X, y)??

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,

??decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',

??max_iter=-1, probability=False, random_state=None, shrinking=True,

??tol=0.001, verbose=False)

>>>?clf.predict(X_test)

array([0, 0, 0, 1, 0])

Here, the default kernel rbf is first changed to linear after the estimator has been constructed via SVC(), and changed back to rbf to refit the estimator and to make a second prediction.

這里,在通過SVC()構造估計器之后,默認內核rbf首先被改變為線性,并且改回rbf以重新設計估計器并作出第二預測。

Multiclass vs. multilabel fitting

多類與多標簽擬合

When using?multiclass classifiers, the learning and prediction task that is performed is dependent on the format of the target data fit upon:

當使用多類分類器時,執行的學習和預測任務取決于適合的目標數據的格式:

>>>

>>>?from?sklearn.svm?import?SVC

>>>?from?sklearn.multiclass?import?OneVsRestClassifier

>>>?from?sklearn.preprocessing?import?LabelBinarizer

>>>?X?=?[[1,?2], [2,?4], [4,?5], [3,?2], [3,?1]]

>>>?y?=?[0,?0,?1,?1,?2]

>>>?classif?=?OneVsRestClassifier(estimator=SVC(random_state=0))

>>>?classif.fit(X, y).predict(X)

array([0, 0, 1, 1, 2])

In the above case, the classifier is fit on a 1d array of multiclass labels and the predict() method therefore provides corresponding multiclass predictions. It is also possible to fit upon a 2d array of binary label indicators:

在上述情況下,分類器適合于一個多類標簽的1d陣列,因此,()方法提供了相應的多類預測。 還可以使用二進制標簽指示器的二維數組:

>>>

>>>?y?=?LabelBinarizer().fit_transform(y)

>>>?classif.fit(X, y).predict(X)

array([[1, 0, 0],

???????[1, 0, 0],

???????[0, 1, 0],

???????[0, 0, 0],

???????[0, 0, 0]])

Here, the classifier is fit() on a 2d binary label representation of y, using the?LabelBinarizer. In this casepredict() returns a 2d array representing the corresponding multilabel predictions.

Note that the fourth and fifth instances returned all zeroes, indicating that they matched none of the three labels fit upon. With multilabel outputs, it is similarly possible for an instance to be assigned multiple labels:

這里,分類器是使用LabelBinarizer對y的2d二進制標簽表示進行fit()。 在這個casepredict()中返回一個表示相應的多重標簽預測的2d數組。

請注意,第四和第五個實例返回所有零,表示它們與三個標簽不匹配。 對于多標簽輸出,類似地可以為實例分配多個標簽:

>>?from?sklearn.preprocessing?import?MultiLabelBinarizer

>>?y?=?[[0,?1], [0,?2], [1,?3], [0,?2,?3], [2,?4]]

>>?y?=?preprocessing.MultiLabelBinarizer().fit_transform(y)

>>?classif.fit(X, y).predict(X)

array([[1,?1,?0,?0,?0],

???????[1,?0,?1,?0,?0],

???????[0,?1,?0,?1,?0],

???????[1,?0,?1,?1,?0],

???????[0,?0,?1,?0,?1]])

In this case, the classifier is fit upon instances each assigned multiple labels. The?MultiLabelBinarizer?is used to binarize the 2d array of multilabels to fit upon. As a result, predict() returns a 2d array with multiple predicted labels for each instance.

在這種情況下,分類器適合每個分配多個標簽的實例。 MultiLabelBinarizer用于二值化二維數組的多邊形以適應。 因此,predict()會為每個實例返回具有多個預測標簽的2d數組。

以上內容來自?<http://scikit-learn.org/stable/tutorial/basic/tutorial.html>?

?

機器學習課程和教程推薦:The World's Best Machine Learning Courses & Tutorials in 2020

平臺原文介紹:We've compiled more than?10,000 student reviews?across 150+ of the web's top Machine Learning courses, tutorials in search of the best way to learn Machine Learning in 2020. The awards below, like Best Course Overall, Best YouTube Tutorial, are based on student reviews.

平臺已經積累有10000多個真實用戶評論,希望對正在研究機器學習的讀者有用!

蜀ICP備15035023號-4

<rp id="pptpi"><xmp id="pptpi"><th id="pptpi"></th><dl id="pptpi"><pre id="pptpi"><noframes id="pptpi"><code id="pptpi"></code><kbd id="pptpi"><strong id="pptpi"><pre id="pptpi"></pre></strong></kbd>
  • <var id="pptpi"><dl id="pptpi"></dl></var>
    <menu id="pptpi"></menu>

    
    <rt id="pptpi"></rt>
  • <rp id="pptpi"><strong id="pptpi"><meter id="pptpi"></meter></strong></rp>
  • <p id="pptpi"></p>
    主站蜘蛛池模板: 巍山| 通化县| 兰溪市| 永城市| 安乡县| 邵阳市| 手机| 双流县| 定远县| 仁怀市| 七台河市| 万全县| 佛冈县| 永康市| 巫山县| 齐齐哈尔市| 民丰县| 五家渠市| 鄂伦春自治旗| 武宁县| 望都县| 绥芬河市| 玛曲县| 石泉县| 安阳市| 胶州市| 陆河县| 贵州省| 大同县| 富锦市| 旌德县| 南川市| 德清县| 昌平区| 临漳县| 青神县| 湖南省| 汤阴县| 西畴县| 信丰县| 遵义县|