5.15 机器学习 · Machine Learning 学习笔记(一) by OTreeWEN

来源:https://uqer.io/community/share/55bc40caf9f06c91f918c604

  1. # 测试sklearn安装包是否已装好
  2. import sklearn as sk
  3. import numpy as np
  4. import matplotlib.pylab as plt
  5. # 加载数据
  6. from sklearn import datasets
  7. iris = datasets.load_iris()
  8. X_iris, y_iris = iris.data, iris.target
  9. # 将X 和 y 的shape 打印出来
  10. print X_iris.shape, y_iris.shape
  11. print 'X_iris =', X_iris[0]
  12. print 'y_iris =',y_iris[0]
  13. (150, 4) (150,)
  14. X_iris = [ 5.1 3.5 1.4 0.2]
  15. y_iris = 0

Any machine learning problem can be represented with the following three concepts:

• We will have to learn to solve a task T. For example, build a spam filter that learns to classify e-mails as spam or ham.

• We will need some experience E to learn to perform the task. Usually, experience is represented through a dataset. For the spam filter, experience comes as a set of e-mails, manually classified by a human as spam or ham.

• We will need a measure of performance P to know how well we are solving the task and also to know whether after doing some modifications, our results are improving or getting worse. The percentage of e-mails that our spam filtering is correctly classifying as spam or ham could be P for our spam-filtering task.

Our first machine learning method – linear classification

  1. # ex1 linear classification
  2. from sklearn.cross_validation import train_test_split
  3. from sklearn import preprocessing
  4. # Get dataset with only the first two attributes
  5. X, y = X_iris[:, :2], y_iris
  6. # Split the dataset into a training and a testing set
  7. # Test set will be the 25% taken randomly
  8. X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.25, random_state=33)
  9. # The train_test_split function automatically builds the training
  10. # and evaluation datasets, randomly selecting the samples.
  11. scaler = preprocessing.StandardScaler().fit(X_train) # Standardize the features
  12. X_train = scaler.transform(X_train)
  13. X_test = scaler.transform(X_test)
  14. print 'X_train',X_train.shape # 75% of sample
  15. print 'X_test',X_test.shape # 25% of sample
  16. import matplotlib.pyplot as plt
  17. colors = ['red', 'greenyellow', 'blue']
  18. for i in xrange(len(colors)):
  19. xs = X_train[:, 0][y_train == i]
  20. ys = X_train[:, 1][y_train == i]
  21. plt.scatter(xs, ys, c=colors[i])
  22. plt.legend(iris.target_names)
  23. plt.xlabel('Sepal length')
  24. plt.ylabel('Sepal width')
  25. # from sklearn.linear_modelsklearn._model import SGDClassifier
  26. from sklearn.linear_model import SGDClassifier
  27. # SGD stands for Stochastic Gradient Descent
  28. clf = SGDClassifier()
  29. clf.fit(X_train, y_train) # three-class problem
  30. print 'shape of coef =',clf.coef_.shape
  31. print 'shape of intercept =',clf.intercept_.shape
  32. X_train (112, 2)
  33. X_test (38, 2)
  34. shape of coef = (3, 2)
  35. shape of intercept = (3,)

5.15 机器学习 · Machine Learning 学习笔记(一) by OTreeWEN - 图1

The following code draws the three decision boundaries and lets us know if they worked as expected:

  1. # 设定画图的刻度边界
  2. x_min, x_max = X_train[:, 0].min() - .5, X_train[:, 0].max() + .5
  3. y_min, y_max = X_train[:, 1].min() - .5, X_train[:, 1].max() + .5
  4. xs = np.arange(x_min, x_max, 0.5)
  5. fig, axes = plt.subplots(1, 3)
  6. fig.set_size_inches(10, 6)
  7. for i in [0, 1, 2]:
  8. # 设定图表i的标识、刻度等
  9. axes[i].set_aspect('equal')
  10. axes[i].set_title('Class '+ str(i) + ' versus the rest')
  11. axes[i].set_xlabel('Sepal length')
  12. axes[i].set_ylabel('Sepal width')
  13. axes[i].set_xlim(x_min, x_max)
  14. axes[i].set_ylim(y_min, y_max)
  15. plt.sca(axes[i]) # 选择图表 i
  16. # 画散点图
  17. plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.prism)
  18. # 计算分割线
  19. ys = (- clf.intercept_[i] - xs * clf.coef_[i, 0]) / clf.coef_[i, 1]
  20. # 画分割线
  21. plt.plot(xs, ys, hold=True)

5.15 机器学习 · Machine Learning 学习笔记(一) by OTreeWEN - 图2

suppose that we have a new flower with a sepal width of 4.7 and a sepal length of 3.1, and we want to predict its class. We just have to apply our brand new classifier to it (after normalizing!).

  1. print clf.predict(scaler.transform([[4.7, 3.1]]))[0]
  2. print clf.decision_function(scaler.transform([[4.7, 3.1]]))
  3. 0
  4. [[ 19.77232705 8.13983962 -28.65250296]]

We want to be a little more formal when we talk about a good classifier. What does that mean? The performance of a classifier is a measure of its effectiveness. The simplest performance measure is accuracy: given a classifier and an evaluation dataset, it measures the proportion of instances correctly classified by the classifier.

  1. # Evaluating the results
  2. from sklearn import metrics
  3. y_train_pred = clf.predict(X_train)
  4. print metrics.accuracy_score(y_train, y_train_pred)
  5. 0.821428571429

Probably, the most important thing you should learn from this chapter is that measuring accuracy on the training set is really a bad idea. You have built your model using this data, and it is possible that your model adjusts well to them but performs poorly in future (previously unseen data), which is its purpose. This phenomenon is called overfitting, and you will see it now and again while you read this book. If you measure based on your training data, you will never detect overfitting. So, never measure based on your training data. This is why we have reserved part of the original dataset (the testing partition)—we want to evaluate performance on previously unseen data. Let's check the accuracy again, now on the evaluation set (recall that it was already scaled):

  1. y_pred = clf.predict(X_test)
  2. print metrics.accuracy_score(y_test, y_pred)
  3. 0.684210526316

Precision: This computes the proportion of instances predicted as positives that were correctly evaluated (it measures how right our classifier is when it says that an instance is positive).

Recall: This counts the proportion of positive instances that were correctly evaluated (measuring how right our classifier is when faced with a positive instance).

F1-score: This is the harmonic mean of precision and recall, and tries to combine both in a single number.

  1. print metrics.classification_report(y_test, y_pred, target_names=iris.target_names)
  2. precision recall f1-score support
  3. setosa 1.00 1.00 1.00 8
  4. versicolor 0.43 0.27 0.33 11
  5. virginica 0.65 0.79 0.71 19
  6. avg / total 0.66 0.68 0.66 38

Another useful metric (especially for multi-class problems) is the confusion matrix: in its (i, j) cell, it shows the number of class instances i that were predicted to be in class j. A good classifier will accumulate the values on the confusion matrix diagonal, where correctly classified instances belong.

  1. print metrics.confusion_matrix(y_test, y_pred)

To finish our evaluation process, we will introduce a very useful method known as cross-validation. As we explained before, we have to partition our dataset into a training set and a testing set. However, partitioning the data, results such that there are fewer instances to train on, and also, depending on the particular partition we make (usually made randomly), we can get either better or worse results. Cross-validation allows us to avoid this particular case, reducing result variance and producing a more realistic score for our models. The usual steps for k-fold cross-validation are the following:

  • Partition the dataset into k different subsets.
  • Create k different models by training on k-1 subsets and testing on the remaining subset.
  • Measure the performance on each of the k models and take the average measure.
  1. from sklearn.cross_validation import cross_val_score, KFold
  2. from sklearn.preprocessing import StandardScaler
  3. from sklearn.pipeline import Pipeline
  4. # create a composite estimator made by a pipeline of the
  5. # standarization and the linear model
  6. clf = Pipeline([('scaler', StandardScaler()),('linear_model', SGDClassifier())])
  7. # create a k-fold cross validation iterator of k=5 folds
  8. cv = KFold(X.shape[0], 5, shuffle=True, random_state=33)
  9. # by default the score used is the one returned by score
  10. # method of the estimator (accuracy)
  11. scores = cross_val_score(clf, X, y, cv=cv)
  12. print scores
  13. [ 0.73333333 0.63333333 0.73333333 0.66666667 0.6 ]

We obtained an array with the k scores. We can calculate the mean and the standard error to obtain a final figure:

  1. from scipy.stats import sem
  2. def mean_score(scores):
  3. return ("Mean score: {0:.3f} (+/-{1:.3f})").format(np.mean(scores), sem(scores))
  4. print mean_score(scores)
  5. Mean score: 0.673 (+/-0.027)

Machine learning categories

Classification is only one of the possible machine learning problems that can be addressed with scikit-learn. We can organize them in the following categories:

• In the previous example, we had a set of instances (that is, a set of data collected from a population) represented by certain features and with a particular target attribute. Supervised learning algorithms try to build a model from this data, which lets us predict the target attribute for new instances, knowing only these instance features. When the target class belongs to a discrete set (such as a list of flower species), we are facing a classification problem.

• Sometimes the class we want to predict, instead of belonging to a discrete set, ranges on a continuous set, such as the real number line. In this case, we are trying to solve a regression problem (the term was coined by Francis Galton, who observed that the heights of tall ancestors tend to regress down towards a normal value, the average human height). For example, we could try to predict the petal width based on the other three features. We will see that the methods used for regression are quite different from those used for classification.

• Another different type of machine learning problem is that of unsupervised learning. In this case, we do not have a target class to predict but instead want to group instances according to some similarity measure based on the available set of features. For example, suppose you have a dataset composed of e-mails and want to group them by their main topic (the task of grouping instances is called clustering). We can use it as features, for example, the different words used in each of them.