## 2020年3月30日 星期一

### [LeetCode] Medium - 12. Integer to Roman (G,M,A,F)

Source From Here
Question
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
1. Symbol       Value
2. I             1
3. V             5
4. X             10
5. L             50
6. C             100
7. D             500
8. M             1000
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
* I can be placed before V (5) and X (10) to make 4 and 9.
* X can be placed before L (50) and C (100) to make 40 and 90.
* C can be placed before D (500) and M (1000) to make 400 and 900.
* Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Example 1:
Input: 3
Output: "III"

Example 2:
Input: 4
Output: "IV"

Example 3:
Input: 9
Output: "IX"

Example 4:
Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 5:
Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Solution

Naive
1. class Solution:
2.     def intToRoman(self, num: int) -> str:
3.         def i2r(d, roman_digits=['I', 'V', 'X']):
4.             if d < 4:
5.                 return roman_digits * d
6.             elif d == 4:
7.                 return roman_digits + roman_digits
8.             elif d == 5:
9.                 return roman_digits
10.             elif d <= 8:
11.                 return roman_digits + roman_digits * (d - 5)
12.             else:
13.                 return roman_digits + roman_digits
14.
15.
16.         roman_level_digits = [['I', 'V', 'X'], ['X', 'L', 'C'], ['C', 'D', 'M'], ['M', '-', '-']]
17.         roman_num = ''
18.         level = 0
19.         while num > 0:
20.             remainder = num % 10
21.             roman_num = i2r(remainder, roman_level_digits[level]) + roman_num
22.             num //= 10
23.             level += 1
24.
25.         return roman_num
Runtime: 44 ms, faster than 82.16% of Python3 online submissions for Integer to Roman.
Memory Usage: 13.9 MB, less than 6.15% of Python3 online submissions for Integer to Roman.

1. def intToRoman(self, num: int) -> str:
2.     thousands = ["", "M", "MM", "MMM"]
3.     hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
4.     tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
5.     ones = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
6.     return thousands[num // 1000] + hundreds[num % 1000 // 100] + tens[num % 100 // 10] + ones[num % 10]
Runtime: 44 ms, faster than 82.16% of Python3 online submissions for Integer to Roman.
Memory Usage: 13 MB, less than 98.46% of Python3 online submissions for Integer to Roman.

## 2020年3月29日 星期日

### [Py DS] Ch5 - Machine Learning (Part10)

In-Depth: Manifold Learning
We have seen how principal component analysis can be used in the dimensionality reduction task—reducing the number of features of a dataset while maintaining the essential relationships between the points. While PCA is flexible, fast, and easily interpretable, it does not perform so well when there are nonlinear relationships within the data; we will see some examples of these below.

To address this deficiency, we can turn to a class of methods known as manifold learning— a class of unsupervised estimators that seeks to describe datasets as low-dimensional manifolds embedded in high-dimensional spaces. When you think of a manifold, I’d suggest imagining a sheet of paper: this is a two-dimensional object that lives in our familiar three-dimensional world, and can be bent or rolled in two dimensions. In the parlance of manifold learning, we can think of this sheet as a two-dimensional manifold embedded in three-dimensional space.

Here we will demonstrate a number of manifold methods, going most deeply into a couple techniques: multidimensional scaling (MDS), locally linear embedding (LLE), and isometric mapping (Isomap). We begin with the standard imports:
1. %matplotlib inline
2. import matplotlib.pyplot as plt
3. import seaborn as sns; sns.set()
4. import numpy as np

Manifold Learning: “HELLO”
To make these concepts more clear, let’s start by generating some two-dimensional data that we can use to define a manifold. Here is a function that will create data in the shape of the word “HELLO”:
1. def make_hello(N=1000, rseed=42):
2.     # Make a plot with "HELLO" text; save as PNG
3.     fig, ax = plt.subplots(figsize=(41))
5.     ax.axis('off')
6.     ax.text(0.50.4'HELLO', va='center', ha='center', weight='bold', size=85)
7.     fig.savefig('hello.png')
8.     plt.close(fig)
9.     # Open this PNG and draw random points from it
11.     data = imread('hello.png')[::-1, :, 0].T
12.     rng = np.random.RandomState(rseed)
13.     X = rng.rand(4 * N, 2)
14.     i, j = (X * data.shape).astype(int).T
15.     mask = (data[i, j] < 1)
17.     X[:, 0] *= (data.shape / data.shape)
18.     X = X[:N]
19.     return X[np.argsort(X[:, 0])]
Let’s call the function and visualize the resulting data (Figure 5-94):
1. X = make_hello(1000)
2. colorize = dict(c=X[:, 0], cmap=plt.cm.get_cmap('rainbow'5))
3. plt.scatter(X[:, 0], X[:, 1], **colorize)
4. plt.axis('equal');

Figure 5-94. Data for use with manifold learning

The output is two dimensional, and consists of points drawn in the shape of the word “HELLO”. This data form will help us to see visually what these algorithms are doing.

Multidimensional Scaling (MDS)
Looking at data like this, we can see that the particular choice of x and y values of the dataset are not the most fundamental description of the data: we can scale, shrink, or rotate the data, and the “HELLO” will still be apparent. For example, if we use a rotation matrix to rotate the data, the x and y values change, but the data is still fundamentally the same (Figure 5-95):
1. def rotate(X, angle):
3.     R = [[np.cos(theta), np.sin(theta)],
4.     [-np.sin(theta), np.cos(theta)]]
5.     return np.dot(X, R)
6.
7. X2 = rotate(X, 20) + 5
8. plt.scatter(X2[:, 0], X2[:, 1], **colorize)
9. plt.axis('equal');

Figure 5-95. Rotated dataset

This tells us that the x and y values are not necessarily fundamental to the relationships in the data. What is fundamental, in this case, is the distance between each point and the other points in the dataset. A common way to represent this is to use a distance matrix: for N points, we construct an N × N array such that entry ij contains the distance between point i and point j. Let’s use Scikit-Learn’s efficient pairwise_distances function to do this for our original data:
1. from sklearn.metrics import pairwise_distances
2. D = pairwise_distances(X)
3. D.shape  # (10001000)
As promised, for our N=1,000 points, we obtain a 1,000×1,000 matrix, which can be visualized as shown in Figure 5-96:
1. plt.imshow(D, zorder=2, cmap='Blues', interpolation='nearest')
2. plt.colorbar();

Figure 5-96. Visualization of the pairwise distances between points

If we similarly construct a distance matrix for our rotated and translated data, we see that it is the same:
1. D2 = pairwise_distances(X2)
2. np.allclose(D, D2)  # True
This distance matrix gives us a representation of our data that is invariant to rotations and translations, but the visualization of the matrix is not entirely intuitive. In the representation presented in Figure 5-96, we have lost any visible sign of the interesting structure in the data: the “HELLO” that we saw before.

Further, while computing this distance matrix from the (x, y) coordinates is straightforward, transforming the distances back into x and y coordinates is rather difficult. This is exactly what the multidimensional scaling algorithm aims to do: given a distance matrix between points, it recovers a D-dimensional coordinate representation of the data. Let’s see how it works for our distance matrix, using the precomputed dissimilarity to specify that we are passing a distance matrix (Figure 5-97):
1. from sklearn.manifold import MDS
2.
3. model = MDS(n_components=2, dissimilarity='precomputed', random_state=1)
4. out = model.fit_transform(D)
5. plt.scatter(out[:, 0], out[:, 1], **colorize)
6. plt.axis('equal');

Figure 5-97. An MDS embedding computed from the pairwise distances

The MDS algorithm recovers one of the possible two-dimensional coordinate representations of our data, using only the N × N distance matrix describing the relationship between the data points.

MDS as Manifold Learning
The usefulness of this becomes more apparent when we consider the fact that distance matrices can be computed from data in any dimension. So, for example, instead of simply rotating the data in the two-dimensional plane, we can project it into three dimensions using the following function (essentially a three-dimensional generalization of the rotation matrix used earlier):
1. def random_projection(X, dimension=3, rseed=42):
2.     assert dimension >= X.shape
3.     rng = np.random.RandomState(rseed)
4.     C = rng.randn(dimension, dimension)
5.     e, V = np.linalg.eigh(np.dot(C, C.T))
6.     return np.dot(X, V[:X.shape])
7.
8. X3 = random_projection(X, 3)
9. X3.shape  # (10003)
Let’s visualize these points to see what we’re working with (Figure 5-98):
1. from mpl_toolkits import mplot3d
2. ax = plt.axes(projection='3d')
3. ax.scatter3D(X3[:, 0], X3[:, 1], X3[:, 2],
4. **colorize)
5. ax.view_init(azim=70, elev=50)

Figure 5-98. Data embedded linearly into three dimensions

We can now ask the MDS estimator to input this three-dimensional data, compute the distance matrix, and then determine the optimal two-dimensional embedding for this distance matrix. The result recovers a representation of the original data (Figure 5-99):
1. model = MDS(n_components=2, random_state=1)
2. out3 = model.fit_transform(X3)
3. plt.scatter(out3[:, 0], out3[:, 1], **colorize)
4. plt.axis('equal');

Figure 5-99. The MDS embedding of the three-dimensional data recovers the input up to a rotation and reflection

This is essentially the goal of a manifold learning estimator: given high-dimensional embedded data, it seeks a low-dimensional representation of the data that preserves certain relationships within the data. In the case of MDS, the quantity preserved is the distance between every pair of points.

Nonlinear Embeddings: Where MDS Fails
Our discussion so far has considered linear embeddings, which essentially consist of rotations, translations, and scalings of data into higher-dimensional spaces. Where MDS breaks down is when the embedding is nonlinear—that is, when it goes beyond this simple set of operations. Consider the following embedding, which takes the input and contorts it into an “S” shape in three dimensions:
1. def make_hello_s_curve(X):
2.     t = (X[:, 0] - 2) * 0.75 * np.pi
3.     x = np.sin(t)
4.     y = X[:, 1]
5.     z = np.sign(t) * (np.cos(t) - 1)
6.     return np.vstack((x, y, z)).T
7.
8. XS = make_hello_s_curve(X)
This is again three-dimensional data, but we can see that the embedding is much more complicated (Figure 5-100):
1. from mpl_toolkits import mplot3d
2.
3. ax = plt.axes(projection='3d')
4. ax.scatter3D(XS[:, 0], XS[:, 1], XS[:, 2], **colorize);

Figure 5-100. Data embedded nonlinearly into three dimensions

The fundamental relationships between the data points are still there, but this time the data has been transformed in a nonlinear way: it has been wrapped up into the shape of an “S.” If we try a simple MDS algorithm on this data, it is not able to “unwrap” this nonlinear embedding, and we lose track of the fundamental relationships in the embedded manifold (Figure 5-101):
1. from sklearn.manifold import MDS
2.
3. model = MDS(n_components=2, random_state=2)
4. outS = model.fit_transform(XS)
5. plt.scatter(outS[:, 0], outS[:, 1], **colorize)
6. plt.axis('equal');

Figure 5-101. The MDS algorithm applied to the nonlinear data; it fails to recover the underlying structure

The best two-dimensional linear embedding does not unwrap the S-curve, but instead throws out the original y-axis.

Nonlinear Manifolds: Locally Linear Embedding
How can we move forward here? Stepping back, we can see that the source of the problem is that MDS tries to preserve distances between faraway points when constructing the embedding. But what if we instead modified the algorithm such that it only preserves distances between nearby points? The resulting embedding would be closer to what we want.

Visually, we can think of it as illustrated in Figure 5-102.

Figure 5-102. Representation of linkages between points within MDS and LLE (figure source)

Here each faint line represents a distance that should be preserved in the embedding. On the left is a representation of the model used by MDS: it tries to preserve the distances between each pair of points in the dataset. On the right is a representation of the model used by a manifold learning algorithm called locally linear embedding (LLE): rather than preserving all distances, it instead tries to preserve only the distances between neighboring points: in this case, the nearest 100 neighbors of each point.

Thinking about the left panel, we can see why MDS fails: there is no way to flatten this data while adequately preserving the length of every line drawn between the two points. For the right panel, on the other hand, things look a bit more optimistic. We could imagine unrolling the data in a way that keeps the lengths of the lines approximately the same. This is precisely what LLE does, through a global optimization of a cost function reflecting this logic.

LLE comes in a number of flavors; here we will use the modified LLE algorithm to recover the embedded two-dimensional manifold. In general, modified LLE does better than other flavors of the algorithm at recovering well-defined manifolds with very little distortion (Figure 5-103):
1. from sklearn.manifold import LocallyLinearEmbedding
2.
3. model = LocallyLinearEmbedding(n_neighbors=100, n_components=2, method='modified',
4. eigen_solver='dense')
5. out = model.fit_transform(XS)
6. fig, ax = plt.subplots()
7. ax.scatter(out[:, 0], out[:, 1], **colorize)
8. ax.set_ylim(0.15, -0.15);

Figure 5-103. Locally linear embedding can recover the underlying data from a nonlinearly embedded input

The result remains somewhat distorted compared to our original manifold, but captures the essential relationships in the data!

Some Thoughts on Manifold Methods
Though this story and motivation is compelling, in practice manifold learning techniques tend to be finicky enough that they are rarely used for anything more than simple qualitative visualization of high-dimensional data. The following are some of the particular challenges of manifold learning, which all contrast poorly with PCA:
* In manifold learning, there is no good framework for handling missing data. In contrast, there are straightforward iterative approaches for missing data in PCA.
* In manifold learning, the presence of noise in the data can “short-circuit” the manifold and drastically change the embedding. In contrast, PCA naturally filters noise from the most important components.
* The manifold embedding result is generally highly dependent on the number of neighbors chosen, and there is generally no solid quantitative way to choose an optimal number of neighbors. In contrast, PCA does not involve such a choice.
* In manifold learning, the globally optimal number of output dimensions is difficult to determine. In contrast, PCA lets you find the output dimension based on the explained variance.
* In manifold learning, the meaning of the embedded dimensions is not always clear. In PCA, the principal components have a very clear meaning.
* In manifold learning the computational expense of manifold methods scales as O[N^2] or O[N^3]. For PCA, there exist randomized approaches that are generally much faster (though see the megaman package for some more scalable implementations of manifold learning).

With all that on the table, the only clear advantage of manifold learning methods over PCA is their ability to preserve nonlinear relationships in the data; for that reason I tend to explore data with manifold methods only after first exploring them with PCA. Scikit-Learn implements several common variants of manifold learning beyond Isomap and LLE: the Scikit-Learn documentation has a nice discussion and comparison of them. Based on my own experience, I would give the following recommendations:
* For toy problems such as the S-curve we saw before, locally linear embedding (LLE) and its variants (especially modified LLE), perform very well. This is implemented in sklearn.manifold.LocallyLinearEmbedding.
* For high-dimensional data from real-world sources, LLE often produces poor results, and isometric mapping (Isomap) seems to generally lead to more meaningful embeddings. This is implemented in sklearn.manifold.Isomap.
* For data that is highly clustered, t-distributed stochastic neighbor embedding (t-SNE) seems to work very well, though can be very slow compared to other methods. This is implemented in sklearn.manifold.TSNE.

If you’re interested in getting a feel for how these work, I’d suggest running each of the methods on the data in this section.

Example: Isomap on Faces
One place manifold learning is often used is in understanding the relationship between high-dimensional data points. A common case of high-dimensional data is images; for example, a set of images with 1,000 pixels each can be thought of as collection of points in 1,000 dimensions—the brightness of each pixel in each image defines the coordinate in that dimension.

Here let’s apply Isomap on some faces data. We will use the Labeled Faces in the Wild dataset, which we previously saw in “In-Depth: Support Vector Machines” on page 405 and “In Depth: Principal Component Analysis” on page 433. Running this command will download the data and cache it in your home directory for later use:
1. from sklearn.datasets import fetch_lfw_people
2.
3. faces = fetch_lfw_people(min_faces_per_person=30)
4. faces.data.shape  # (23702914)
We have 2,370 images, each with 2,914 pixels. In other words, the images can be thought of as data points in a 2,914-dimensional space! Let’s quickly visualize several of these images to see what we’re working with (Figure 5-104):
1. fig, ax = plt.subplots(48, subplot_kw=dict(xticks=[], yticks=[]))
2. for i, axi in enumerate(ax.flat):
3.     axi.imshow(faces.images[i], cmap='gray')

Figure 5-104. Examples of the input faces

We would like to plot a low-dimensional embedding of the 2,914-dimensional data to learn the fundamental relationships between the images. One useful way to start is to compute a PCA, and examine the explained variance ratio, which will give us an idea of how many linear features are required to describe the data (Figure 5-105):
1. from sklearn.decomposition import PCA
2.
3. model = PCA(100, svd_solver='randomized').fit(faces.data)
4. plt.plot(np.cumsum(model.explained_variance_ratio_))
5. plt.xlabel('n components')
6. plt.ylabel('cumulative variance');

Figure 5-105. Cumulative variance from the PCA projection

We see that for this data, nearly 100 components are required to preserve 90% of the variance. This tells us that the data is intrinsically very high dimensional—it can’t be described linearly with just a few components. When this is the case, nonlinear manifold embeddings like LLE and Isomap can be helpful. We can compute an Isomap embedding on these faces using the same pattern shown before:
1. %%time
2. from sklearn.manifold import Isomap
3.
4. model = Isomap(n_components=2)
5. proj = model.fit_transform(faces.data) # Take around 32s
6. display(proj.shape)  # (23702)
The output is a two-dimensional projection of all the input images. To get a better idea of what the projection tells us, let’s define a function that will output image thumbnails at the locations of the projections:
1. from matplotlib import offsetbox
2.
3. def plot_components(data, model, images=None, ax=None, thumb_frac=0.05, cmap='gray'):
4.     ax = ax or plt.gca()
5.     proj = model.fit_transform(data)
6.     ax.plot(proj[:, 0], proj[:, 1], '.k')
7.     if images is not None:
8.         min_dist_2 = (thumb_frac * max(proj.max(0) - proj.min(0))) ** 2
9.         shown_images = np.array([2 * proj.max(0)])
10.         for i in range(data.shape):
11.             dist = np.sum((proj[i] - shown_images) ** 21)
12.             if np.min(dist) < min_dist_2:
13.                 # don't show points that are too close
14.                 continue
15.
16.             shown_images = np.vstack([shown_images, proj[i]])
17.             imagebox = offsetbox.AnnotationBbox(
18.             offsetbox.OffsetImage(images[i], cmap=cmap), proj[i])
Calling this function now, we see the result (Figure 5-106):
1. fig, ax = plt.subplots(figsize=(1010))
2. plot_components(faces.data, model=Isomap(n_components=2), images=faces.images[:, ::2, ::2])

Figure 5-106. Isomap embedding of the faces data

The result is interesting: the first two Isomap dimensions seem to describe global image features: the overall darkness or lightness of the image from left to right, and the general orientation of the face from bottom to top. This gives us a nice visual indication of some of the fundamental features in our data.

We could then go on to classify this data, perhaps using manifold features as inputs to the classification algorithm as we did in “In-Depth: Support Vector Machines” on page 405.

Example: Visualizing Structure in Digits
As another example of using manifold learning for visualization, let’s take a look at the MNIST handwritten digits set. This data is similar to the digits we saw in “In-Depth: Decision Trees and Random Forests” on page 421, but with many more pixels per image. It can be downloaded from http://mldata.org/ with the Scikit-Learn utility:
1. from sklearn.datasets import fetch_mldata
2.
3. mnist = fetch_mldata('MNIST original')
4. mnist.data.shape
This consists of 70,000 images, each with 784 pixels (i.e., the images are 28×28). As before, we can take a look at the first few images (Figure 5-107):
1. fig, ax = plt.subplots(68, subplot_kw=dict(xticks=[], yticks=[]))
2. for i, axi in enumerate(ax.flat):
3.     axi.imshow(mnist.data[1250 * i].reshape(2828), cmap='gray_r')

Figure 5-107. Examples of the MNIST digits

This gives us an idea of the variety of handwriting styles in the dataset.

Let’s compute a manifold learning projection across the data, illustrated in Figure 5-108. For speed here, we’ll only use 1/30 of the data, which is about ~2,000 points (because of the relatively poor scaling of manifold learning, I find that a few thousand samples is a good number to start with for relatively quick exploration before moving to a full calculation):
1. # use only 1/30 of the data: full dataset takes a long time!
2. data = mnist.data[::30]
3. target = mnist.target[::30]
4. model = Isomap(n_components=2)
5. proj = model.fit_transform(data)
6. plt.scatter(proj[:, 0], proj[:, 1], c=target, cmap=plt.cm.get_cmap('jet'10))
7. plt.colorbar(ticks=range(10))
8. plt.clim(-0.59.5);

Figure 5-108. Isomap embedding of the MNIST digit data

The resulting scatter plot shows some of the relationships between the data points, but is a bit crowded. We can gain more insight by looking at just a single number at a time (Figure 5-109):
1. from sklearn.manifold import Isomap
2. # Choose 1/4 of the "1" digits to project
3. data = mnist.data[mnist.target == 1][::4]
4. fig, ax = plt.subplots(figsize=(1010))
5.
6. model = Isomap(n_neighbors=5, n_components=2, eigen_solver='dense')
7. plot_components(data, model, images=data.reshape((-12828)), ax=ax, thumb_frac=0.05, cmap='gray_r')

Figure 5-109. Isomap embedding of only the 1s within the digits data

The result gives you an idea of the variety of forms that the number “1” can take within the dataset. The data lies along a broad curve in the projected space, which appears to trace the orientation of the digit. As you move up the plot, you find ones that have hats and/or bases, though these are very sparse within the datasetThe projection lets us identify outliers that have data issues (i.e., pieces of the neighboring digits that snuck into the extracted images).

Now, this in itself may not be useful for the task of classifying digits, but it does help us get an understanding of the data, and may give us ideas about how to move forward, such as how we might want to preprocess the data before building a classification pipeline.

Supplement
scikit-learn 使用 fetch_mldata 无法下载 MNIST 数据集问题解决方法
1. 直接下载MNST数据集文件。
https://github.com/amplab/datascience-sp14/raw/mas...lab7/mldata/mnist-original.mat

2. 将下载的数据集文件放到scikit数据根目录下的 mldata目录。

1. from sklearn.datasets.base import get_data_home
2. print (get_data_home()) # 如我的电脑上的目录为： C:\Users\95232\scikit_learn_data

This message was edited 72 times. Last update was at 29/03/2020 16:01:04

### [ Python 常見問題 ] When using unittest.mock.patch, why is autospec not True by default?

Source From  Here Question When you patch a function using  mock , you have the option to specify  autospec  as True: If you set  autospec...