2019年9月1日 星期日

[ Py DS ] Ch5 - Machine Learning (Part2)

Source From Here 


Introducing Scikit-Learn 
There are several Python libraries that provide solid implementations of a range of machine learning algorithms. One of the best known is Scikit-Learna package that provides efficient versions of a large number of common algorithms. Scikit-Learn is characterized by a clean, uniform, and streamlined API, as well as by very useful and complete online documentation. A benefit of this uniformity is that once you understand the basic use and syntax of Scikit-Learn for one type of model, switching to a new model or algorithm is very straightforward. 

This section provides an overview of the Scikit-Learn API; a solid understanding of these API elements will form the foundation for understanding the deeper practical discussion of machine learning algorithms and approaches in the following chapters. 

We will start by covering data representation in Scikit-Learn, followed by covering the Estimator API, and finally go through a more interesting example of using these tools for exploring a set of images of handwritten digits. 

Data Representation in Scikit-Learn 
Machine learning is about creating models from data: for that reason, we’ll start by discussing how data can be represented in order to be understood by the computer. The best way to think about data within Scikit-Learn is in terms of tables of data. 

Data as table 
A basic table is a two-dimensional grid of data, in which the rows represent individual elements of the dataset, and the columns represent quantities related to each of these elements. For example, consider the Iris dataset, famously analyzed by Ronald Fisher in 1936. We can download this dataset in the form of a Pandas DataFrame using the Seaborn library

  1. import seaborn as sns  
  2.   
  3. iris = sns.load_dataset('iris')  
  4. iris.head()  


Here each row of the data refers to a single observed flower, and the number of rows is the total number of flowers in the dataset. In general, we will refer to the rows of the matrix as samples, and the number of rows as n_samples. Likewise, each column of the data refers to a particular quantitative piece of information that describes each sample. In general, we will refer to the columns of the matrix as features, and the number of columns as n_features. 

Features matrix 
This table layout makes clear that the information can be thought of as a two dimensional numerical array or matrix, which we will call the features matrix. By convention, this features matrix is often stored in a variable named X. The features matrix is assumed to be two-dimensional, with shape [n_samples, n_features], and is most often contained in a NumPy array or a Pandas DataFrame, though some Scikit-Learn models also accept SciPy sparse matrices. 

The samples (i.e., rowsalways refer to the individual objects described by the dataset. For example, the sample might be a flower, a person, a document, an image, a sound file, a video, an astronomical object, or anything else you can describe with a set of quantitative measurements; The features (i.e., columnsalways refer to the distinct observations that describe each sample in a quantitative manner. Features are generally real-valued, but may be Boolean or discrete-valued in some cases. 

Target array 
In addition to the feature matrix X, we also generally work with a label or target array, which by convention we will usually call y. The target array is usually one dimensional, with length n_samples, and is generally contained in a NumPy array or Pandas Series. The target array may have continuous numerical values, or discrete classes/labels. While some Scikit-Learn estimators do handle multiple target values in the form of a two-dimensional [n_samples, n_targets] target array, we will primarily be working with the common case of a one-dimensional target array. 

Often one point of confusion is how the target array differs from the other features columns. The distinguishing feature of the target array is that it is usually the quantity we want to predict from the data: in statistical terms, it is the dependent variable. For example, in the preceding data we may wish to construct a model that can predict the species of flower based on the other measurements; in this case, the species column would be considered the target. 

With this target array in mind, we can use Seaborn (discussed earlier in “Visualization with Seaborn” on page 311) to conveniently visualize the data (see Figure 5-12): 

Figure 5-12. A visualization of the Iris dataset 

For use in Scikit-Learn, we will extract the features matrix and target array from the DataFrame, which we can do using some of the Pandas DataFrame operations discussed in Chapter 3: 

  1. X_iris = iris.drop('species', axis=1)  
  2. print('X_iris.shape={}'.format(X_iris.shape))  # X_iris.shape=(1504)  
  3.   
  4. y_iris = iris['species']  
  5. print('y_iris.shape={}'.format(y_iris.shape))  # y_iris.shape=(150,)  
With this data properly formatted, we can move on to consider the estimator API of Scikit-Learn

Scikit-Learn’s Estimator API 
The Scikit-Learn API is designed with the following guiding principles in mind, as outlined in the Scikit-Learn API paper
* Consistency 

All objects share a common interface drawn from a limited set of methods, with consistent documentation.

* Inspection 

All specified parameter values are exposed as public attributes.

* Limited object hierarchy 

Only algorithms are represented by Python classes; datasets are represented in standard formats (NumPy arrays, Pandas DataFrames, SciPy sparse matrices) and parameter names use standard Python strings.

* Composition 

Many machine learning tasks can be expressed as sequences of more fundamental algorithms, and Scikit-Learn makes use of this wherever possible.

* Sensible defaults 

When models require user-specified parameters, the library defines an appropriate default value.

In practice, these principles make Scikit-Learn very easy to use, once the basic principles are understood. Every machine learning algorithm in Scikit-Learn is implemented via the Estimator API, which provides a consistent interface for a wide range of machine learning applications

Basics of the API 
Most commonly, the steps in using the Scikit-Learn estimator API are as follows (we will step through a handful of detailed examples in the sections that follow): 

1. Choose a class of model by importing the appropriate estimator class from Scikit-Learn.
2. Choose model hyperparameters by instantiating this class with desired values.
3. Arrange data into a features matrix and target vector following the discussion from before.
4. Fit the model to your data by calling the fit() method of the model instance.
5. Apply the model to new data:
 • For supervised learning, often we predict labels for unknown data using the predict() method.
 • For unsupervised learning, we often transform or infer properties of the data using the transform() or predict() method.

We will now step through several simple examples of applying supervised and unsupervised learning methods. 

Supervised learning example: Simple linear regression 
As an example of this process, let’s consider a simple linear regression—that is, the common case of fitting a line to xy data. We will use the following simple data for our regression example (Figure 5-14): 

  1. import matplotlib.pyplot as plt  
  2. import numpy as np  
  3.   
  4. rng = np.random.RandomState(42)  
  5. x = 10 * rng.rand(50)  
  6. y = 2 * x - 1 + rng.randn(50)  
  7. plt.scatter(x, y);  

Figure 5-14. Data for linear regression 

With this data in place, we can use the recipe outlined earlier. Let’s walk through the process: 

1. Choose a class of model. 

In Scikit-Learn, every class of model is represented by a Python class. So, for example, if we would like to compute a simple linear regression model, we can import the linear regression class:
  1. from sklearn.linear_model import LinearRegression  
Note that other, more general linear regression models exist as well; you can read more about them in the sklearn.linear_model module documentation.

2. Choose model hyperparameters. 

An important point is that a class of model is not the same as an instance of a model. Once we have decided on our model class, there are still some options open to us. Depending on the model class we are working with, we might need to answer one or more questions like the following:
• Would we like to fit for the offset (i.e., intercept)?
• Would we like the model to be normalized?
• Would we like to preprocess our features to add model flexibility?
• What degree of regularization would we like to use in our model?
• How many model components would we like to use?

These are examples of the important choices that must be made once the model class is selected. These choices are often represented as hyperparameters, or parameters that must be set before the model is fit to data. In Scikit-Learn, we choose hyperparameters by passing values at model instantiation. We will explore how you can quantitatively motivate the choice of hyperparameters in “Hyperparameters and Model Validation” on page 359. For our linear regression example, we can instantiate the LinearRegression class and specify that we would like to fit the intercept using the fit_intercept hyperparameter:
  1. model = LinearRegression(fit_intercept=True)  
Keep in mind that when the model is instantiated, the only action is the storing of these hyperparameter values. In particular, we have not yet applied the model to any data: the Scikit-Learn API makes very clear the distinction between choice of model and application of model to data.


3. Arrange data into a features matrix and target vector. 

Previously we detailed the Scikit-Learn data representation, which requires a two-dimensional features matrix and a one-dimensional target array. Here our target variable y is already in the correct form (a length-n_samples array), but we need to massage the data x to make it a matrix of size [n_samples, n_features]. In this case, this amounts to a simple reshaping of the one-dimensional array:
  1. X = x[:, np.newaxis]  
  2. print('X.shape={}; x.shape={}'.format(X.shape, x.shape))  # X.shape=(501); X.shape=(501); x.shape=(50,)  

4. Fit the model to your data. 

Now it is time to apply our model to data. This can be done with the fit() method of the model:
  1. model.fit(X, y)  
This fit() command causes a number of model-dependent internal computations to take place, and the results of these computations are stored in model specific attributes that the user can explore. In Scikit-Learn, by convention all model parameters that were learned during the fit() process have trailing underscores; for example, in this linear model, we have the following:
  1. print(model.coef_)        # [1.9776566]  
  2. print(model.intercept_)   # -0.903310725531111  
These two parameters represent the slope and intercept of the simple linear fit to the data. Comparing to the data definition, we see that they are very close to the input slope of 2 and intercept of –1.

One question that frequently comes up regards the uncertainty in such internal model parameters. In general, Scikit-Learn does not provide tools to draw conclusions from internal model parameters themselves: interpreting model parameters is much more a statistical modeling question than a machine learning question. Machine learning rather focuses on what the model predicts. If you would like to dive into the meaning of fit parameters within the model, other tools are available, including the [urlhttp://www.statsmodels.org/stable/index.html]StatsModels Python package[/url].

5. Predict labels for unknown data. 

Once the model is trained, the main task of supervised machine learning is to evaluate it based on what it says about new data that was not part of the training set. In Scikit-Learn, we can do this using the predict() method. For the sake of this example, our “new data” will be a grid of x values, and we will ask what y values the model predicts:
  1. xfit = np.linspace(-111)  
  2. Xfit = xfit[:, np.newaxis]  
  3. print("xfit.shape={}; Xfit.shape={}".format(xfit.shape, Xfit.shape))  # xfit.shape=(50,); Xfit.shape=(501)  
  4. yfit = model.predict(Xfit)  
Finally, let’s visualize the results by plotting first the raw data, and then this model fit (Figure 5-15):
  1. plt.scatter(x, y)  
  2. plt.plot(xfit, yfit, 'r');  

Figure 5-15. A simple linear regression fit to the data

Typically one evaluates the efficacy of the model by comparing its results to some known baseline, as we will see in the next example.


Supervised learning example: Iris classification 
Let’s take a look at another example of this process, using the Iris dataset we discussed earlier. Our question will be this: given a model trained on a portion of the Iris data, how well can we predict the remaining labels? 

For this task, we will use an extremely simple generative model known as Gaussian naive Bayes, which proceeds by assuming each class is drawn from an axis-aligned Gaussian distribution (see “In Depth: Naive Bayes Classification” on page 382 for more details). Because it is so fast and has no hyperparameters to choose, Gaussian naive Bayes is often a good model to use as a baseline classification, before you explore whether improvements can be found through more sophisticated models. 

We would like to evaluate the model on data it has not seen before, and so we will split the data into a training set and a testing set. This could be done by hand, but it is more convenient to use the train_test_split utility function: 

  1. from sklearn.model_selection import train_test_split  
  2.   
  3. Xtrain, Xtest, ytrain, ytest = train_test_split(X_iris, y_iris, random_state=1)  
  4. print('Xtrain.shape={}; Xtest.shape={}'.format(Xtrain.shape, Xtest.shape)) # Xtrain.shape=(1124); Xtest.shape=(384)  
With the data arranged, we can follow our recipe to predict the labels: 
  1. from sklearn.naive_bayes import GaussianNB # 1. choose model class  
  2.   
  3. model = GaussianNB() # 2. instantiate model  
  4. model.fit(Xtrain, ytrain) # 3. fit model to data  
  5. y_model = model.predict(Xtest) # 4. predict on new data  
Finally, we can use the accuracy_score utility to see the fraction of predicted labels that match their true value: 
  1. from sklearn.metrics import accuracy_score  
  2. accuracy_score(ytest, y_model)  # 0.9736842105263158  
With an accuracy topping 97%, we see that even this very naive classification algorithm is effective for this particular dataset! 

Unsupervised learning example: Iris dimensionality 
As an example of an unsupervised learning problem, let’s take a look at reducing the dimensionality of the Iris data so as to more easily visualize it. Recall that the Iris data is four dimensional: there are four features recorded for each sample. 

The task of dimensionality reduction is to ask whether there is a suitable lowerdimensional representation that retains the essential features of the data. Often dimensionality reduction is used as an aid to visualizing data; after all, it is much easier to plot data in two dimensions than in four dimensions or higher! 

Here we will use principal component analysis (PCA; see “In Depth: Principal Component Analysis” on page 433), which is a fast linear dimensionality reduction technique. We will ask the model to return two components—that is, a two-dimensional representation of the data. 

Following the sequence of steps outlined earlier, we have: 

  1. from sklearn.decomposition import PCA  # 1. Choose the model class  
  2.   
  3. model = PCA(n_components=2)            # 2. Instantiate the model with hyperparameters  
  4. model.fit(X_iris)                      # 3. Fit to data. Notice y is not specified!  
  5. X_2D = model.transform(X_iris)         # 4. Transform the data to two dimensions  
Now let’s plot the results. A quick way to do this is to insert the results into the original Iris DataFrame, and use Seaborn’s lmplot to show the results (Figure 5-16): 
  1. iris['PCA1'] = X_2D[:, 0]  
  2. iris['PCA2'] = X_2D[:, 1]  
  3. sns.lmplot("PCA1""PCA2", hue='species', data=iris, fit_reg=False);  

Figure 5-16. The Iris data projected to two dimensions 

Unsupervised learning: Iris clustering 
Let’s next look at applying clustering to the Iris data. A clustering algorithm attempts to find distinct groups of data without reference to any labels. Here we will use a powerful clustering method called a Gaussian mixture model (GMM), discussed in more detail in “In Depth: Gaussian Mixture Models” on page 476. A GMM attempts to model the data as a collection of Gaussian blobs. 

We can fit the Gaussian mixture model as follows: 

  1. from sklearn.mixture import GaussianMixture                      # 1. Choose the model class  
  2. model = GaussianMixture(n_components=3, covariance_type='full'   # 2. Instantiate the model w/ hyperparameters  
  3. model.fit(X_iris)                                                # 3. Fit to data. Notice y is not specified!  
  4. y_gmm = model.predict(X_iris)                                    # 4. Determine cluster labels  
As before, we will add the cluster label to the Iris DataFrame and use Seaborn to plot the results (Figure 5-17): 
  1. iris['cluster'] = y_gmm  
  2. sns.lmplot("PCA1""PCA2", data=iris, hue='species', col='cluster', fit_reg=False);  

Figure 5-17. k-means clusters within the Iris data 

By splitting the data by cluster number, we see exactly how well the GMM algorithm has recovered the underlying label: the setosa species is separated perfectly within cluster 0, while there remains a small amount of mixing between versicolor and virginicaThis means that even without an expert to tell us the species labels of the individual flowers, the measurements of these flowers are distinct enough that we could automatically identify the presence of these different groups of species with a simple clustering algorithm! This sort of algorithm might further give experts in the field clues as to the relationship between the samples they are observing. 

Application: Exploring Handwritten Digits 
To demonstrate these principles on a more interesting problem, let’s consider one piece of the optical character recognition problem: the identification of handwritten digits. In the wild, this problem involves both locating and identifying characters in an image. Here we’ll take a shortcut and use Scikit-Learn’s set of preformatted digits, which is built into the library. 

Loading and visualizing the digits data 
We’ll use Scikit-Learn’s data access interface and take a look at this data: 

  1. from sklearn.datasets import load_digits  
  2.   
  3. digits = load_digits()  
  4. print(digits.images.shape)  # (179788)  
The images data is a three-dimensional array: 1,797 samples, each consisting of an 8×8 grid of pixels. Let’s visualize the first hundred of these (Figure 5-18): 
  1. import matplotlib.pyplot as plt  
  2.   
  3. fig, axes = plt.subplots(1010, figsize=(88),  
  4.                          subplot_kw={'xticks':[], 'yticks':[]},  
  5.                          gridspec_kw=dict(hspace=0.1, wspace=0.1))  
  6.   
  7. for i, ax in enumerate(axes.flat):  
  8.     ax.imshow(digits.images[i], cmap='binary', interpolation='nearest')  
  9.     ax.text(0.050.05, str(digits.target[i]), transform=ax.transAxes, color='green')  

Figure 5-18. The handwritten digits data; each sample is represented by one 8×8 grid of pixels 

In order to work with this data within Scikit-Learn, we need a two-dimensional, [n_samples, n_features] representation. We can accomplish this by treating each pixel in the image as a feature—that is, by flattening out the pixel arrays so that we have a length-64 array of pixel values representing each digit. Additionally, we need the target array, which gives the previously determined label for each digit. These two quantities are built into the digits dataset under the data and target attributes, respectively: 

  1. X = digits.data  
  2. y = digits.target  
  3. print('X.shape={}; y.shape={}'.format(X.shape, y.shape))  # X.shape=(179764); y.shape=(1797,)  
We see here that there are 1,797 samples and 64 features. 

Unsupervised learning: Dimensionality reduction 
We’d like to visualize our points within the 64-dimensional parameter space, but it’s difficult to effectively visualize points in such a high-dimensional space. Instead we’ll reduce the dimensions to 2, using an unsupervised method. Here, we’ll make use of a manifold learning algorithm called Isomap (see “In-Depth: Manifold Learning” on page 445), and transform the data to two dimensions: 

  1. from sklearn.manifold import Isomap  
  2.   
  3. iso = Isomap(n_components=2)  
  4. iso.fit(digits.data)  
  5. data_projected = iso.transform(digits.data)  
  6. print("data_projected.shape={}".format(data_projected.shape))  # data_projected.shape=(17972)  
We see that the projected data is now two-dimensional. Let’s plot this data to see if we can learn anything from its structure (Figure 5-19): 
  1. plt.rcParams["figure.figsize"] = (157)  
  2. plt.scatter(data_projected[:, 0], data_projected[:, 1], c=digits.target,  
  3.             edgecolor='none', alpha=0.7, cmap=plt.cm.get_cmap('seismic'10))  
  4. plt.colorbar(label='digit label', ticks=range(10))  
  5. plt.clim(-0.59.5);  

Figure 5-19. An Isomap embedding of the digits data 

This plot gives us some good intuition into how well various numbers are separated in the larger 64-dimensional space. For example, zeros (in dark blue) and seven (in red) have very little overlap in parameter space. Intuitively, this makes sense: a zero is empty in the middle of the image, while a seven will generally have ink in the middle. On the other hand, there seems to be a more or less continuous spectrum between twos and sevens while they some how look quite similar. 

Overall, however, the different groups appear to be fairly well separated in the parameter space: this tells us that even a very straightforward supervised classification algorithm should perform suitably on this data. Let’s give it a try. 

Classification on digits 
Let’s apply a classification algorithm to the digits. As with the Iris data previously, we will split the data into a training and test set, and fit a Gaussian naive Bayes model: 

  1. from sklearn.naive_bayes import GaussianNB  
  2.   
  3. Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, random_state=0)  
  4. model = GaussianNB()  
  5. model.fit(Xtrain, ytrain)  
  6. y_model = model.predict(Xtest)  
Now that we have predicted our model, we can gauge its accuracy by comparing the true values of the test set to the predictions: 
  1. from sklearn.metrics import accuracy_score  
  2. print("accuracy={:.02f}%".format(accuracy_score(ytest, y_model) * 100)) # accuracy=83.33%  
With even this extremely simple model, we find about 80% accuracy for classification of the digits! However, this single number doesn’t tell us where we’ve gone wrong— one nice way to do this is to use the confusion matrix, which we can compute with Scikit-Learn and plot with Seaborn (Figure 5-20): 
  1. from sklearn.metrics import confusion_matrix  
  2.   
  3. mat = confusion_matrix(ytest, y_model)  
  4. sns.heatmap(mat, square=True, annot=True, cbar=True, cmap='gist_heat')  
  5. plt.xlabel('predicted value')  
  6. plt.ylabel('true value');  

Figure 5-20. A confusion matrix showing the frequency of misclassifications by our classifier 

This shows us where the mislabeled points tend to be: for example, a large number of twos here are misclassified as either ones or eights. Another way to gain intuition into the characteristics of the model is to plot the inputs again, with their predicted labels. We’ll use green for correct labels, and red for incorrect labels (Figure 5-21): 

  1. fig, axes = plt.subplots(1010, figsize=(88), subplot_kw={'xticks':[], 'yticks':[]},  
  2.                          gridspec_kw=dict(hspace=0.1, wspace=0.1))  
  3. for i, ax in enumerate(axes.flat):  
  4.     ax.imshow(digits.images[i], cmap='binary', interpolation='nearest')  
  5.     ax.text(0.050.05, str(y_model[i]),  
  6.     transform=ax.transAxes, color='green' if (ytest[i] == y_model[i]) else 'red')  

Figure 5-21. Data showing correct (green) and incorrect (red) labels 

Examining this subset of the data, we can gain insight regarding where the algorithm might not be performing optimally. To go beyond our 80% classification rate, we might move to a more sophisticated algorithm, such as support vector machines (see “In-Depth: Support Vector Machines” on page 405) or random forests (see “In-Depth: Decision Trees and Random Forests” on page 421), or another classification approach. 

Summary 
In this section we have covered the essential features of the Scikit-Learn data representation, and the estimator API. Regardless of the type of estimator, the same import/instantiate/fit/predict pattern holds. Armed with this information about the estimator API, you can explore the Scikit-Learn documentation and begin trying out various models on your data. 

In the next section, we will explore perhaps the most important topic in machine learning: how to select and validate your model. 

Supplement 
Scikit-learn doc - Preprocessing data 
Scikit-learn doc - Choosing Colormaps in Matplotlib

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...