Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132923 views
License: OTHER
1
import matplotlib.pyplot as plt
2
import numpy as np
3
from sklearn.datasets import make_blobs
4
from sklearn.preprocessing import (StandardScaler, MinMaxScaler, Normalizer,
5
RobustScaler)
6
from .plot_helpers import cm2
7
8
9
def plot_scaling():
10
X, y = make_blobs(n_samples=50, centers=2, random_state=4, cluster_std=1)
11
X += 3
12
13
plt.figure(figsize=(15, 8))
14
main_ax = plt.subplot2grid((2, 4), (0, 0), rowspan=2, colspan=2)
15
16
main_ax.scatter(X[:, 0], X[:, 1], c=y, cmap=cm2, s=60)
17
maxx = np.abs(X[:, 0]).max()
18
maxy = np.abs(X[:, 1]).max()
19
20
main_ax.set_xlim(-maxx + 1, maxx + 1)
21
main_ax.set_ylim(-maxy + 1, maxy + 1)
22
main_ax.set_title("Original Data")
23
other_axes = [plt.subplot2grid((2, 4), (i, j))
24
for j in range(2, 4) for i in range(2)]
25
26
for ax, scaler in zip(other_axes, [StandardScaler(), RobustScaler(),
27
MinMaxScaler(), Normalizer(norm='l2')]):
28
X_ = scaler.fit_transform(X)
29
ax.scatter(X_[:, 0], X_[:, 1], c=y, cmap=cm2, s=60)
30
ax.set_xlim(-2, 2)
31
ax.set_ylim(-2, 2)
32
ax.set_title(type(scaler).__name__)
33
34
other_axes.append(main_ax)
35
36
for ax in other_axes:
37
ax.spines['left'].set_position('center')
38
ax.spines['right'].set_color('none')
39
ax.spines['bottom'].set_position('center')
40
ax.spines['top'].set_color('none')
41
ax.xaxis.set_ticks_position('bottom')
42
ax.yaxis.set_ticks_position('left')
43
44