<jupyter_start><jupyter_text>Unsupervised learning  1 k-means clusteringThere is absolutely no guarantee of recovering a groynd truth. Firstly, choosing a right number of clusters is difficult. Secondly, the algorithm is sensitive to initialization, and can fall into local minima<jupyter_code>from sklearn import cluster, datasets
iris = datasets.load_iris()
X_iris = iris.data
y_iris = iris.target
k_means = cluster.KMeans(n_clusters=3)
k_means.fit(X_iris, y_iris)
print(k_means.labels_[::10])
print(y_iris[::10])<jupyter_output>[0 0 0 0 0 1 1 1 1 1 2 2 2 2 2]<jupyter_text>ApplicationClustering can be seen as a way of choosing a small number of the examples to compress the information.<jupyter_code>import matplotlib.pyplot as plt
import scipy as sp
import numpy as np
try:
    face = sp.face(gray=True)
except AttributeError:
    from scipy import misc
    face = misc.face(gray=True)
X = face.reshape((-1, 1))
k_means = cluster.KMeans(n_clusters=5, n_init=1)
k_means.fit(X)
values = k_means.cluster_centers_.squeeze()
labels = k_means.labels_
face_compressed = np.choose(labels, values)
face_compressed.shape = face.shape
plt.subplot(121)
plt.imshow(face)
plt.subplot(122)
plt.imshow(face_compressed)
plt.show()<jupyter_output><empty_output><jupyter_text>2 Hierarchical agglomerative clusteringAims to build a hierarchy of clusters. In general, There two typical methods in hierarchical clustering. One is Agglomerative which clusters from bottom to up and another does from top to down.<jupyter_code>from sklearn import datasets
from sklearn.cluster import AgglomerativeClustering
from sklearn.feature_extraction.image import grid_to_graph
import matplotlib.pyplot as plt
digits = datasets.load_digits()
images = digits.images
X = np.reshape(images, (len(images), -1))
connectivity = grid_to_graph(*images[0].shape)
agglo = cluster.FeatureAgglomeration(connectivity=connectivity, n_clusters=32)
agglo.fit(X)
X_reduced = agglo.transform(X)
x_approx = agglo.inverse_transform(X_reduced)
image_approx = np.reshape(x_approx, images.shape)<jupyter_output><empty_output><jupyter_text>3 Principal Component Analysis(PCA)select the successive components that explain tehe maximum variance in signal<jupyter_code>x1 = np.random.normal(size=100)
x2 = np.random.normal(size=100)
x3 = x1 + x2
X = np.c_[x1, x2, x3]
from sklearn import decomposition
pca = decomposition.PCA()
pca.fit(X)
print(pca.explained_variance_)
pca.n_components=2
X_reduce = pca.fit_transform(X)
X_reduce.shape<jupyter_output><empty_output><jupyter_text>4 Independent Component Analysis(ICA)Select components so that the distribution of their loading carries a maximum amount of idependent infomation.<jupyter_code>time = np.linspace(0, 10, 2000)
s1 = np.sin(2*time)
s2 = np.sign(np.sin(3*time))
S = np.c_[s1, s2]
S += 0.2*np.random.normal(size=S.shape)
S /= S.std(axis=0)
A = np.array([[1,1],[0.5, 2]])
X = np.dot(S, A.T)
ica = decomposition.FastICA()
S_ = ica.fit_transform(X)
A_ = ica.mixing_.T
np.allclose(X, np.dot(S_,A_)+ica.mean_)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10543657331136738, 'fraction_numerical': 0.02438220757825371, 'mean_word_length': 6.422982885085575, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '160898', 'license': 'MIT', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1080, 'n_words': 278, 'path': 'Scikit_Learning/Tutorial/Unsupervised_learning.ipynb', 'repo_name': 'gaufung/Data_Analytics_Learning_Note', 'size': '3035'}