2017年3月21日 星期二

[ Intro2ML ] Ch5. Representing Data and Engineering Features - Part2

Interactions and Polynomials 
Another way to enrich a feature representation, in particular for linear models, is adding interaction features and polynomial features of the original data. This kind of feature engineering is often used in statistical modelling, but also common in many practical machine learning applications. As a first example, look again at Figure linear_binning above. The linear model learned a constant value for each bin on the wave dataset. We know, however, that linear models can not only learn offsets, but also slopes. One way to add a slope to the linear model on the binned data, is to add the original feature (the x axis in the plot) back in. 

This leads to a 11 dimensional dataset, as seen in Figure 4-3: 
>>> from ch5_t04 import *
>>> X_combined = np.hstack([X, X_binned]) # API:hstack stack arrays in sequence horizontally (column wise).
>>> print(X_combined.shape)
(100, 11)
>>> X.shape
(100, 1)
>>> X_binned.shape
(100, 10)

- ch5_t05.py 
  1. #!/usr/bin/env python  
  2. import numpy as np  
  3. import mglearn  
  4.   
  5. from sklearn.linear_model import LinearRegression  
  6. from sklearn.tree import DecisionTreeRegressor  
  7. X, y = mglearn.datasets.make_wave(n_samples=100)  
  8. line = np.linspace(-331000, endpoint=False).reshape(-11)  
  9. reg = DecisionTreeRegressor(min_samples_split=3).fit(X, y)  
  10.   
  11. bins = np.linspace(-3311)  
  12. print "bins: {}".format(bins)  
  13.   
  14. which_bin = np.digitize(X, bins=bins)  
  15. print "\nData points:\n%s\n" % X[:5]  
  16. print "\nBin membership for data points:\n%s\n" % which_bin[:5]  
  17.   
  18. from sklearn.preprocessing import OneHotEncoder  
  19.   
  20. # Transform using the OneHotEncoder  
  21. encoder = OneHotEncoder(sparse=False)  
  22.   
  23. # encoder.fit finds the unique values that appear in what_bin  
  24. encoder.fit(which_bin)  
  25.   
  26. # Transform creates the one-hot encoding  
  27. X_binned = encoder.transform(which_bin)  
  28. print "Top 5 rows in X_binned:\n%s\n" % X_binned[:5]  
  29.   
  30. print "X_binned.shape = %s\n" % str(X_binned.shape)  
  31.   
  32. line_binned = encoder.transform(np.digitize(line, bins=bins))  
  33. print "line_binned:\n%s\n" % (line_binned[:10])  
  34. X_combined = np.hstack([X, X_binned])  
  35. print(X_combined.shape)  
  36. reg = LinearRegression().fit(X_combined, y)  
  37.   
  38. import matplotlib.pyplot as plt  
  39. line_combined = np.hstack([line, line_binned])  
  40. plt.plot(line, reg.predict(line_combined), label='linear regression binned')  
  41.   
  42. for bin in bins:  
  43.     plt.plot([bin, bin], [-33], ':', c='k')  
  44.   
  45. plt.plot(X[:, 0], y, 'o', c='k')  
  46. plt.legend(loc="best")  
  47. plt.ylabel("Regression output")  
  48. plt.xlabel("Input feature")  
  49. plt.show()  
Figure 4-3. Linear regression using binned features and a single global slope 

In this example, the model learned an offset for each bin, together with a slope. The learned slope is downward, and shared access all the bins - there is a single x-axis feature, which has a single slope. Because the slope is shared across all bins, it doesn't seem to be very helpful. We would rather have a separate slope for each bin! We can achieve this by adding an interaction or product feature feature that indicates which bin a data point is in and where it lies on the x-axis. This feature is a product of the bin indicator and the original feature. Let's create this dataset: 
  1. X_product = np.hstack([X_binned, X * X_binned])  
  2. print(X_product.shape)  
Execution output: 
(100, 20)

The dataset now has 20 features: the indicator for which bin a data point is in, and a product of the original feature and the bin indicator. You can think of the product feature as a separate copy of the x-axis feature for each bin. It is the original feature within the bin, and zero everywhere else. Figure 4.4 shows the result of the linear model on this new representation: 
- ch5_t06.py 
  1. #!/usr/bin/env python  
  2. import numpy as np  
  3. import mglearn  
  4.   
  5. from sklearn.linear_model import LinearRegression  
  6. from sklearn.tree import DecisionTreeRegressor  
  7. X, y = mglearn.datasets.make_wave(n_samples=100)  
  8. line = np.linspace(-331000, endpoint=False).reshape(-11)  
  9. reg = DecisionTreeRegressor(min_samples_split=3).fit(X, y)  
  10.   
  11. bins = np.linspace(-3311)  
  12. print "bins: {}".format(bins)  
  13.   
  14. which_bin = np.digitize(X, bins=bins)  
  15. print "\nData points:\n%s\n" % X[:5]  
  16. print "\nBin membership for data points:\n%s\n" % which_bin[:5]  
  17.   
  18. from sklearn.preprocessing import OneHotEncoder  
  19.   
  20. # Transform using the OneHotEncoder  
  21. encoder = OneHotEncoder(sparse=False)  
  22.   
  23. # encoder.fit finds the unique values that appear in what_bin  
  24. encoder.fit(which_bin)  
  25.   
  26. # Transform creates the one-hot encoding  
  27. X_binned = encoder.transform(which_bin)  
  28. print "Top 5 rows in X_binned:\n%s\n" % X_binned[:5]  
  29.   
  30. print "X_binned.shape = %s\n" % str(X_binned.shape)  
  31.   
  32. line_binned = encoder.transform(np.digitize(line, bins=bins))  
  33. print "line_binned:\n%s\n" % (line_binned[:10])  
  34. #X_combined = np.hstack([X, X_binned])  
  35. X_product = np.hstack([X_binned, X * X_binned])  
  36. print(X_product.shape)  
  37.   
  38. reg = LinearRegression().fit(X_product, y)  
  39.   
  40. import matplotlib.pyplot as plt  
  41. line_product = np.hstack([line_binned, line * line_binned])  
  42. plt.plot(line, reg.predict(line_product), label='linear regression product')  
  43.   
  44. for bin in bins:  
  45.     plt.plot([bin, bin], [-33], ':', c='k')  
  46.   
  47. plt.plot(X[:, 0], y, 'o', c='k')  
  48. plt.legend(loc="best")  
  49. plt.ylabel("Regression output")  
  50. plt.xlabel("Input feature")  
  51. plt.show()  
Figure 4-4. Linear regression with a separate slope per bin 

As you can see, now each bin has its own offset and slope in this model. Using binning is one way to expand a continuous feature. Another one is to use polynomials of the original features. For a given feature x, we might want to consider x**2, x**3, x**4 and so on. This is implemented in PolynomialFeatures in the preprocessing module: 
  1. from sklearn.preprocessing import PolynomialFeatures  
  2.   
  3. # Include polynomials up to x**10  
  4. # the default "include_bias=True" adds a feature that's constantly 1  
  5. poly = PolynomialFeatures(degree=10, include_bias=False)  
  6. poly.fit(X)  
  7. X_poly = poly.transform(X)  
  8. print("X_poly.shape: {}".format(X_poly.shape))  
Output: 
X_poly.shape: (100, 10)

Let's compare the entries of X_poly to those of X
  1. print("Top 5 entries of X:\n{}\n".format(X[:5]))  
  2. print("Top 5 entries of X_poly:\n{}\n".format(X_poly[:5]))  
Output: 

You can obtain the semantics of the features by calling the get_feature_names method, which provide the exponent for each feature: 
  1. print("Polynomial feature names:\n{}\n".format(poly.get_feature_names()))  
Output: 
Polynomial feature names:
['x0', 'x0^2', 'x0^3', 'x0^4', 'x0^5', 'x0^6', 'x0^7', 'x0^8', 'x0^9', 'x0^10']

You can see that the first column of X_poly corresponds exactly to X, while the other columns are the powers of the first entry. It's interesting to see how large some of the values can get. The second column has entries above 20,000, orders of magnitude different than the rest. Using polynomial features together with a linear regression model yields the classic model of polynomial regression (see Figure 4-5): 
- ch5_t07.py 
  1. #!/usr/bin/env python  
  2. import numpy as np  
  3. import mglearn  
  4.   
  5. from sklearn.linear_model import LinearRegression  
  6. from sklearn.tree import DecisionTreeRegressor  
  7. X, y = mglearn.datasets.make_wave(n_samples=100)  
  8. line = np.linspace(-331000, endpoint=False).reshape(-11)  
  9. reg = DecisionTreeRegressor(min_samples_split=3).fit(X, y)  
  10.   
  11. bins = np.linspace(-3311)  
  12. print "bins: {}".format(bins)  
  13.   
  14. which_bin = np.digitize(X, bins=bins)  
  15. print "\nData points:\n%s\n" % X[:5]  
  16. print "\nBin membership for data points:\n%s\n" % which_bin[:5]  
  17.   
  18.   
  19. from sklearn.preprocessing import PolynomialFeatures  
  20.   
  21. # Include polynomials up to x**10  
  22. # the default "include_bias=True" adds a feature that's constantly 1  
  23. poly = PolynomialFeatures(degree=10, include_bias=False)  
  24. poly.fit(X)  
  25. X_poly = poly.transform(X)  
  26. print("X_poly.shape: {}".format(X_poly.shape))  
  27.   
  28. print("Top 5 entries of X:\n{}\n".format(X[:5]))  
  29. print("Top 5 entries of X_poly:\n{}\n".format(X_poly[:5]))  
  30.   
  31. print("Polynomial feature names:\n{}\n".format(poly.get_feature_names()))  
  32.   
  33. reg = LinearRegression().fit(X_poly, y)  
  34. line_poly = poly.transform(line)  
  35.   
  36. import matplotlib.pyplot as plt  
  37. plt.plot(line, reg.predict(line_poly), label='Polynomial linear regression')  
  38. plt.plot(X[:,0], y, 'o', c='k')  
  39. plt.ylabel("Regression output")  
  40. plt.xlabel("Input feature")  
  41. plt.legend(loc='best')  
  42. plt.show()  
Figure 4-5. Linear regression with tenth-degree polynomial features 

As you can see, polynomial features yield a very smooth fit on this one-dimensional data. However, polynomials of high degree tend to behave in extreme ways on the boundaries or in regions with little data. As a comparison, here is a kernel SVM model (svm.SVR) learned on the original data, without any transformation (see Figure 4-6): 
- ch5_t08.py 
  1. #!/usr/bin/env python  
  2. import numpy as np  
  3. import mglearn  
  4.   
  5. X, y = mglearn.datasets.make_wave(n_samples=100)  
  6. line = np.linspace(-331000, endpoint=False).reshape(-11)  
  7. reg = DecisionTreeRegressor(min_samples_split=3).fit(X, y)  
  8.   
  9. bins = np.linspace(-3311)  
  10. print "bins: {}".format(bins)  
  11.   
  12. which_bin = np.digitize(X, bins=bins)  
  13. print "\nData points:\n%s\n" % X[:5]  
  14. print "\nBin membership for data points:\n%s\n" % which_bin[:5]  
  15.   
  16.   
  17. from sklearn.svm import SVR  
  18. import matplotlib.pyplot as plt  
  19.   
  20. for gamma in [110]:  
  21.     svr = SVR(gamma=gamma).fit(X, y)  
  22.     plt.plot(line, svr.predict(line), label='SVR gamma={}'.format(gamma))  
  23. plt.plot(X[:,0], y, 'o', c='k')  
  24. plt.ylabel("SVR output")  
  25. plt.xlabel("Input feature")  
  26. plt.legend(loc='best')  
  27. plt.show()  
Figure 4-6. Comparison of different gamma parameters for an SVM with RBE kernel 

Using a more complex model, a kernel SVM, we can able to learn a similarity complex prediction to the polynomial regression without an explicit transformation of the features. As a more realistic application of interactions and polynomials, let's look again at the Boston Housing dataset. We already used polynomial features on this dataset before (Chapter2 - Linear model). Now let's have a look at how these features were constructed, and at how much the polynomial features help. First we load the data, and rescale it to be between 0 and 1 using MinMaxScaler
  1. import numpy as np  
  2. import mglearn  
  3. from sklearn.datasets import load_boston  
  4. from sklearn.model_selection import train_test_split  
  5. from sklearn.preprocessing import MinMaxScaler  
  6.   
  7. boston = load_boston()  
  8. X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=0)  
  9.   
  10. # Rescale data  
  11. scaler = MinMaxScaler()  
  12. X_train_scaled = scaler.fit_transform(X_train)  
  13. X_test_scaled = scaler.fit_transform(X_test)  
Now, we extract polynomial features and interactions up to a degree of 2: 
  1. from sklearn.preprocessing import PolynomialFeatures  
  2.   
  3. poly = PolynomialFeatures(degree=2).fit(X_train_scaled)  
  4. X_train_poly = poly.transform(X_train_scaled)  
  5. X_test_poly = poly.transform(X_test_scaled)  
  6. print("X_train.shape={}".format(X_train.shape))  
  7. print("X_train_poly.shape={}".format(X_train_poly.shape))  
Output: 
X_train.shape=(379, 13)
X_train_poly.shape=(379, 105)

The data originally had 13 features, which were expanded to 105 interaction features. These new features represent all possible interactions between two different original features, as well as the square of each original feature. degree=2 here means that we look at all features are the product of up to two original features. The exact correspondence between input and output features can be found using the get_feature_names method: 
  1. print("Polynomial feature names:\n{}\n".format(poly.get_feature_names()))  
Output: 
Polynomial feature names:
['1', 'x0', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9', 'x10', 'x11', 'x12', 'x0^2', 'x0 x1', 'x0 x2'... 'x9 x12', 'x10^2', 'x10 x11', 'x10 x12', 'x11^2', 'x11 x12', 'x12^2']

The first new feature is a constant feature, called "1" here. The next 13 features are the original features (called "x0" to "x12"). Then follows the first feature squared ("x0^2") and combinations of the first and the other features. Let's compare the performance using Ridge on the data with and without interactions: 
  1. from sklearn.linear_model import Ridge  
  2. ridge = Ridge().fit(X_train_scaled, y_train)  
  3. print("Score without interactions: {:.3f}".format(ridge.score(X_test_scaled, y_test)))  
  4. ridge = Ridge().fit(X_train_poly, y_train)  
  5. print("Score with interactions: {:.3f}".format(ridge.score(X_test_poly, y_test)))  
Output: 
Score without interactions: 0.577
Score with interactions: 0.741

Clearly, the interactions and polynomial features gave us a good boost in performance when using Ridge. When using a more complex model like a random forest (RandomForestRegressor), the story is a bit different, though: 
  1. from sklearn.ensemble import RandomForestRegressor  
  2. rf = RandomForestRegressor(n_estimators=100).fit(X_train_scaled, y_train)  
  3. print("Score without interactions: {:.3f}".format(rf.score(X_test_scaled, y_test)))  
  4. rf = RandomForestRegressor(n_estimators=100).fit(X_train_poly, y_train)  
  5. print("Score with interactions: {:.3f}".format(rf.score(X_test_poly, y_test)))  
Output: 
Score without interactions: 0.799
Score with interactions: 0.761

You can see that even without additional features, the random forest beats the performance of Ridge. Adding interactions and polynomials actually decreases performance slightly. 

Univariate Nonlinear Transformations 
We just saw that adding squared or cubed features can help linear models for regression. There are other transformations that often prove useful for transforming certain features: in particular, applying mathematical functions like logexp, or sin. While tree-based models only care about the ordering of the features, linear models and neural networks are very tied to the scale and distribution of each feature, and if there is a nonlinear relation between the feature and the target, that becomes hard to model - particularly in regression. The functions log and exp can help by adjusting the relative scales in the data so that they can captured better by a linear model or neural network. We saw an application of that in Chapter 2 with memory price data. The sin and cos functions can come in handy when dealing with data that encodes periodic parameters. 

Most models work best when each feature (and in regression also the target) is loosely Gaussian distributed - that is , a histogram of each feature should have something resembling the familiar "bell curve" shape. Using transformations like log and exp is a hacky but simple and efficient way to achieve this. A particularly common case when such a transformation can be helpful is when dealing with integer count data. By count data, we mean features like "how often did user A log in?" Counts are never negative, and often follow particular statistic patterns. We are using a synthetic dataset of counts here that has properties similar to those you can find in the wild. The features are all integer-valued, while the response is continuous. 
  1. import numpy as np  
  2. import mglearn  
  3. from sklearn.datasets import load_boston  
  4. from sklearn.model_selection import train_test_split  
  5. from sklearn.preprocessing import MinMaxScaler  
  6.   
  7. rnd = np.random.RandomState(0)  
  8. X_org = rnd.normal(size=(10003))  
  9. w = rnd.normal(size=3)  
  10.   
  11. X = rnd.poisson(10 * np.exp(X_org))  
  12. y = np.dot(X_org, w)  
Let's take a look at the first 10 entries of the first feature. All are integer values and positive, but apart from that it's hard to make out a particular pattern. If we count the appearance of each value, the distribution of values becomes clear: 
  1. print("Number of eature appearance:\n{}\n".format(np.bincount(X[:,0])))  

The value 2 seems to be the most common, with 6 appearances (bincount always starts at 0), and the counts for higher values fall quickly. However, there are some very high values, like 84 and 85, that are appearing twice. We visualize the counts in Figure 4-7: 
  1. bins = np.bincount(X[:,0])  
  2. import matplotlib.pyplot as plt  
  3. plt.bar(range(len(bins)), bins, color='w')  
  4. plt.ylabel("Number of appearance")  
  5. plt.xlabel("Value")  
  6. plt.show()  
Figure 4-7. Histogram of feature values for X[0] 

Features X[:1] and X[:2] have similar properties. This kind of distribution of values (many small ones and a few vary large ones) is very common in practice. However, it is something most linear models can't handle very well. Let's try to fit a Ridge regression to this model as below sample code: 
  1. from sklearn.linear_model import Ridge  
  2. X_train, X_test,y_train,y_test = train_test_split(X, y, random_state=0)  
  3. ridge = Ridge().fit(X_train, y_train)  
  4. print("Score of Ridge:{:.3f}\n".format(ridge.score(X_test, y_test)))  
Output: 
Score of Ridge:0.622

As you can see from the relatively low R^2 score, Ridge was not able to really capture the relationship between X and y. Applying a logarithmic transformation can help, through. Because the value 0 appears in the data (and the logarithm is not defined as 0), we can't actually apply log, but we have to compute log(X + 1)
  1. X_train_log = np.log(X_train + 1)  
  2. X_test_log = np.log(X_test + 1)  
After the transformation, the distribution of the data is less asymmetrical and doesn't have very large outliers anymore (see Figure 4-8): 
  1. import matplotlib.pyplot as plt  
  2. plt.hist(X_train_log[:,0], bins=25, color='gray')  
  3. plt.ylabel("Number of appearance")  
  4. plt.xlabel("Value")  
  5. plt.show()  
Figure 4-8. Histogram of feature values for X[0] after logarithm transformation 

Building a Ridge model on the new data provides a much better fit: 
  1. ridge = Ridge().fit(X_train_log, y_train)  
  2. print("Score of Ridge(log version):{:.3f}\n".format(ridge.score(X_test_log, y_test)))  
Output: 
Score of Ridge(log version):0.875

Finding the transformation that works best for each combination of dataset and model is somewhat of an art. In this example, all the features had the same properties. This is rarely the case in practice, and usually only a subset of the features should be transformed, or sometimes each feature needs to be transformed in a different way. As we mentioned earlier, these kinds of transformations are irrelevant for tree-based models but might be essential for linear models. Sometimes it is also a good idea to transform the target variable y in regression. Trying to predict counts (say, number of orders) is a fairly common task, and using the log(y+1) transformation often helps. 

As you saw in the previous example, binning, polynomials, and interactions can have a huge influence on how models perform on a given dataset. This is particular true for less complex models like linear models and naive Bayes models. Tree-based models, on the other hand, are often able to discover important interaction themselves, and don't require transforming the data explicitly most of the time. Other models, like SVMs, nearest neighbors, and neural networks, might sometimes benefit from using binning, interactions, or polynomials, but the implications there are usually much less clear than in the case of linear models.

沒有留言:

張貼留言

[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...