加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

【Scikit-Learn 中文文档】无监督学习: 寻求数据表示 - 关于科学

发布时间:2020-12-14 04:54:49 所属栏目:大数据 来源:网络整理
导读:中文文档:?http://sklearn.apachecn.org/cn/stable/tutorial/statistical_inference/unsupervised_learning.html 英文文档:?http://sklearn.apachecn.org/en/stable/tutorial/statistical_inference/unsupervised_learning.html 官方文档:? http://scikit-le

中文文档:?http://sklearn.apachecn.org/cn/stable/tutorial/statistical_inference/unsupervised_learning.html

英文文档:?http://sklearn.apachecn.org/en/stable/tutorial/statistical_inference/unsupervised_learning.html

官方文档:?http://scikit-learn.org/stable/

GitHub:?https://github.com/apachecn/scikit-learn-doc-zh(觉得不错麻烦给个 Star,我们一直在努力)

贡献者:?https://github.com/apachecn/scikit-learn-doc-zh#贡献者

关于我们:?http://www.apachecn.org/organization/209.html



无监督学习: 寻求数据表示

聚类: 对样本数据进行分组

可以利用聚类解决的问题

对于 iris 数据集来说,我们知道所有样本有 3 种不同的类型,但是并不知道每一个样本是那种类型:此时我们可以尝试一个?clustering task(聚类任务)?聚类算法: 将样本进行分组,相似的样本被聚在一起,而不同组别之间的样本是有明显区别的,这样的分组方式就是?clusters(聚类)

K-means 聚类算法

关于聚类有很多不同的聚类标准和相关算法,其中最简便的算法是?K-means?。

>>>
>>> from sklearn import cluster, datasets
>>> iris = datasets.load_iris()
>>> X_iris = iris.data
>>> y_iris .target

>>> k_means = cluster.KMeans(n_clusters=3)
>>> k_means.fit(X_iris) 
KMeans(algorithm='auto',copy_x=True,init='k-means++',...
>>> print(k_means.labels_[::10])
[1 1 1 1 1 0 0 0 0 0 2 2 2 2 2]
print(y_iris[::[0 0 0 0 0 1 1 1 1 1 2 2 2 2 2]

Warning

?

k_means 算法无法保证聚类结果完全绝对真实的反应实际情况。首先,选择正确合适的聚类数量不是一件容易的事情,第二,该算法对初始值的设置敏感,容易陷入局部最优。尽管 scikit-learn 采取了不同的方式来缓解以上问题,目前仍没有完美的解决方案。

Bad initialization 8 clusters Ground truth

Don’t over-interpret clustering results(不要过分解读聚类结果)

Application example: vector quantization(应用案例:向量量化(vector quantization))

一般来说聚类,特别是 K_means 聚类可以作为一种用少量样本来压缩信息的方式。这种方式就是?vector quantization?。例如,K_means 算法可以用于对一张图片进行色调分离:

>>>
matplotlib.pyplot plt

sklearn.feature_extraction.image import grid_to_graph
sklearn.cluster import AgglomerativeClustering


# #############################################################################
# Generate data
try:  # SciPy >= 0.16 have face in misc
    scipy.misc import face
    face = face(grayTrue)
ImportError:
    face True)

# Resize it to 10% of the original size to speed up the processing
face .misc.imresize(face, 0.10) / 255.

X .reshape(face, (1))

# Define the structure A of the data. Pixels connected to their neighbors.
connectivity = grid_to_graph(*face.shape)

# #############################################################################

特征聚集

我们已经知道,稀疏性可以缓解特征维度带来的问题,i.e?即与特征数量相比,样本数量太少。 另一个解决该问题的方式是合并相似的维度:feature agglomeration(特征聚集)。该方法可以通过对特征聚类来实现。换 句话说,就是对样本数据转置后进行聚类。

>>> digits .load_digits() >>> images = digits.images .reshape(images, (len(images), 1)) >>> connectivity *images[0].shape) >>> agglo .FeatureAgglomeration(connectivity=connectivity, ... n_clusters32) >>> aggloFeatureAgglomeration(affinity='euclidean',compute_full_tree='auto',9); font-weight:bold">>>> X_reduced = agglo.transform(X) >>> X_approx .inverse_transform(X_reduced) >>> images_approx .reshape(X_approx, images.shape)

transform?and?inverse_transform?methods

Some estimators expose a?transform?method,for instance to reduce the dimensionality of the dataset.

分解: 将一个信号转换成多个成份并且加载

Components and loadings(成分和载荷)

如果 X 是多维数据,那么我们试图解决的问题是在不同的观察基础上对数据进行重写。我们希望学习得到载荷 L 和成分 C 使得?X = L C?。提取成分 C 有多种不同的方法。

主成份分析: PCA

主成分分析(PCA)?将能够解释数据信息最大方差的的连续成分提取出来

?

上图中样本点的分布在一个方向上是非常平坦的:即三个单变量特征中的任何一个都可以有另外两个特征来表示。主成分分析法(PCA)可以找到使得数据分布不?flat?的矢量方向(可以反映数据主要信息的特征)。

当用主成分分析(PCA)来?transform(转换)?数据时,可以通过在子空间上投影来降低数据的维数。

>>> # Create a signal with only 2 useful dimensions >>> x1 .random.normal(size100) >>> x2 >>> x3 = x1 + x2 .c_[x1, x2, x3] import decomposition >>> pca = decomposition.PCA() >>> pca.fit(X) PCA(copy=True,iterated_power='auto',n_components=None,random_state=None, svd_solver='auto',tol=0.0,whiten=False) print(pca.explained_variance_) [ 2.18565811e+00 1.19346747e+00 8.43026679e-32] # As we can see,only the 2 first components are useful .n_components = 2 = pca.fit_transform(X) >>> X_reduced.shape (100,2)

独立成分分析: ICA

独立成分分析(ICA)?可以提取数据信息中的独立成分,这些成分载荷的分布包含了最多的 的独立信息。该方法能够恢复?non-Gaussian(非高斯)?独立信号:

# Generate sample data numpy np import signal >>> time .linspace(0,80)">10,80)">2000) >>> s1 .sin(2 * time) # Signal 1 : sinusoidal signal >>> s2 .sign(np3 * time)) # Signal 2 : square signal >>> s3 = signal.sawtooth(* np.pi # Signal 3: saw tooth signal >>> S .c_[s1, s2, s3] += 0.2 =S.shape) # Add noise /= S.std(axis0) # Standardize data # Mix data >>> A .array([[1], [0.5,80)">2,80)">1.5,80)">2]]) # Mixing matrix .dot(S, A.T) # Generate observations # Compute ICA >>> ica .FastICA() >>> S_ = ica.fit_transform(X) # Get the estimated sources >>> A_ .mixing_.T >>> np.allclose(X, np.dot(S_, A_) + ica.mean_) True

有兴趣的们也可以和我们一起来维护,持续更新中 。。。

机器学习交流群:?629470233

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读