<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 無監督學習: 尋求數據表示 校驗者: [@片刻](https://github.com/apachecn/scikit-learn-doc-zh) 翻譯者: [@X](https://github.com/apachecn/scikit-learn-doc-zh) ## 聚類: 對樣本數據進行分組 可以利用聚類解決的問題 對于 iris 數據集來說,我們知道所有樣本有 3 種不同的類型,但是并不知道每一個樣本是那種類型:此時我們可以嘗試一個 **clustering task(聚類任務)** 聚類算法: 將樣本進行分組,相似的樣本被聚在一起,而不同組別之間的樣本是有明顯區別的,這樣的分組方式就是 *clusters(聚類)* ### K-means 聚類算法 關于聚類有很多不同的聚類標準和相關算法,其中最簡便的算法是 [K-means](../../modules/clustering.html#k-means) 。 [![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_cluster_iris_002.png](https://box.kancloud.cn/14895cd95bce329b798fa1e6609d9f97_400x300.jpg)](../../auto_examples/cluster/plot_cluster_iris.html) ``` >>> 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) 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[::10]) [0 0 0 0 0 1 1 1 1 1 2 2 2 2 2] ``` Warning k\_means 算法無法保證聚類結果完全絕對真實的反應實際情況。首先,選擇正確合適的聚類數量不是一件容易的事情,第二,該算法對初始值的設置敏感,容易陷入局部最優。盡管 scikit-learn 采取了不同的方式來緩解以上問題,目前仍沒有完美的解決方案。 [![k_means_iris_bad_init](https://box.kancloud.cn/11305ca270ac5523da46742ee936a429_400x300.jpg)](../../auto_examples/cluster/plot_cluster_iris.html)[![k_means_iris_8](https://box.kancloud.cn/d03ca88d5a883190a493900813bbb773_400x300.jpg)](../../auto_examples/cluster/plot_cluster_iris.html)[![cluster_iris_truth](https://box.kancloud.cn/79156eaae0727c099eabbaed4c227c9f_400x300.jpg)](../../auto_examples/cluster/plot_cluster_iris.html)**Bad initialization****8 clusters****Ground truth****Don’t over-interpret clustering results(不要過分解讀聚類結果)** **Application example: vector quantization(應用案例:向量量化(vector quantization))** 一般來說聚類,特別是 K\_means 聚類可以作為一種用少量樣本來壓縮信息的方式。這種方式就是 [vector quantization](https://en.wikipedia.org/wiki/Vector_quantization) 。例如,K\_means 算法可以用于對一張圖片進行色調分離: ``` >>> import scipy as sp >>> try: ... face = sp.face(gray=True) ... except AttributeError: ... from scipy import misc ... face = misc.face(gray=True) >>> X = face.reshape((-1, 1)) # We need an (n_sample, n_feature) array >>> k_means = cluster.KMeans(n_clusters=5, n_init=1) >>> k_means.fit(X) KMeans(algorithm='auto', copy_x=True, init='k-means++', ... >>> values = k_means.cluster_centers_.squeeze() >>> labels = k_means.labels_ >>> face_compressed = np.choose(labels, values) >>> face_compressed.shape = face.shape ``` [![face](https://box.kancloud.cn/3d53ac5e00cb8b52e42f58483cf8499e_300x220.jpg)](../../auto_examples/cluster/plot_face_compress.html)[![face_compressed](https://box.kancloud.cn/4f88e6a46d6f5abc88a7cf1c0f580a5d_300x220.jpg)](../../auto_examples/cluster/plot_face_compress.html)[![face_regular](https://box.kancloud.cn/12f3a557ca5e95794c8fb7c4361ce6b6_300x220.jpg)](../../auto_examples/cluster/plot_face_compress.html)[![face_histogram](https://box.kancloud.cn/14eeac1ffb3d82e600872afe8739245a_300x220.jpg)](../../auto_examples/cluster/plot_face_compress.html)Raw imageK-means quantizationEqual binsImage histogram ### 分層聚類算法: 謹慎使用 [層次聚類](../../modules/clustering.html#hierarchical-clustering) (分層聚類算法)是一種旨在構建聚類層次結構的分析方法,一般來說,實現該算法的大多數方法有以下兩種:- **Agglomerative(聚合)** - 自底向上的方法: 初始階段,每一個樣本將自己作為單獨的一個簇,聚類的簇以最小 化距離的標準進行迭代聚合。當感興趣的簇只有少量的樣本時,該方法是很合適的。如果需要聚類的 簇數量很大,該方法比K\_means算法的計算效率也更高。 \* **Divisive(分裂)** - 自頂向下的方法: 初始階段,所有的樣本是一個簇,當一個簇下移時,它被迭代的進 行分裂。當估計聚類簇數量較大的數據時,該算法不僅效率低(由于樣本始于一個簇,需要被遞歸的進行 分裂),而且從統計學的角度來講也是不合適的。 #### 連接約束聚類 對于逐次聚合聚類,通過連接圖可以指定哪些樣本可以被聚合在一個簇。在 scikit 中,圖由鄰接矩陣來表示,通常該矩陣是一個稀疏矩陣。這種表示方法是非常有用的,例如在聚類圖像時檢索連接區域(有時也被稱為連接要素): [![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_face_ward_segmentation_001.png](https://box.kancloud.cn/b702086b3787009da12f7ecb36482398_500x500.jpg)](../../auto_examples/cluster/plot_face_ward_segmentation.html) ``` import matplotlib.pyplot as plt from sklearn.feature_extraction.image import grid_to_graph from sklearn.cluster import AgglomerativeClustering # ############################################################################# # Generate data try: # SciPy >= 0.16 have face in misc from scipy.misc import face face = face(gray=True) except ImportError: face = sp.face(gray=True) # Resize it to 10% of the original size to speed up the processing face = sp.misc.imresize(face, 0.10) / 255. X = np.reshape(face, (-1, 1)) # ############################################################################# # Define the structure A of the data. Pixels connected to their neighbors. connectivity = grid_to_graph(*face.shape) # ############################################################################# ``` #### 特征聚集 我們已經知道,稀疏性可以緩解特征維度帶來的問題,*i.e* 即與特征數量相比,樣本數量太少。 另一個解決該問題的方式是合并相似的維度:**feature agglomeration(特征聚集)**。該方法可以通過對特征聚類來實現。換 句話說,就是對樣本數據轉置后進行聚類。 [![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_digits_agglomeration_001.png](https://box.kancloud.cn/749a3b0ab6a9e68568cc8405a7dca65b_400x350.jpg)](../../auto_examples/cluster/plot_digits_agglomeration.html) ``` >>> 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) FeatureAgglomeration(affinity='euclidean', compute_full_tree='auto',... >>> X_reduced = agglo.transform(X) >>> X_approx = agglo.inverse_transform(X_reduced) >>> images_approx = np.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)](../../modules/decomposition.html#pca) 將能夠解釋數據信息最大方差的的連續成分提取出來 [![pca_3d_axis](https://box.kancloud.cn/09913b3a40b1b27a58b3f9c3ffc79349_400x300.jpg)](../../auto_examples/decomposition/plot_pca_3d.html) [![pca_3d_aligned](https://box.kancloud.cn/c5cbdd9a4a914015dad0ef5caeab14ee_400x300.jpg)](../../auto_examples/decomposition/plot_pca_3d.html) 上圖中樣本點的分布在一個方向上是非常平坦的:即三個單變量特征中的任何一個都可以有另外兩個特征來表示。主成分分析法(PCA)可以找到使得數據分布不 *flat* 的矢量方向(可以反映數據主要信息的特征)。 當用主成分分析(PCA)來 *transform(轉換)* 數據時,可以通過在子空間上投影來降低數據的維數。 ``` >>> # Create a signal with only 2 useful dimensions >>> 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) 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 >>> pca.n_components = 2 >>> X_reduced = pca.fit_transform(X) >>> X_reduced.shape (100, 2) ``` ### 獨立成分分析: ICA [獨立成分分析(ICA)](../../modules/decomposition.html#ica) 可以提取數據信息中的獨立成分,這些成分載荷的分布包含了最多的 的獨立信息。該方法能夠恢復 **non-Gaussian(非高斯)** 獨立信號: [![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_ica_blind_source_separation_001.png](https://box.kancloud.cn/3ccbca1dc1319339a91e1cd309a2ea6c_566x424.jpg)](../../auto_examples/decomposition/plot_ica_blind_source_separation.html) ``` >>> # Generate sample data >>> import numpy as np >>> from scipy import signal >>> time = np.linspace(0, 10, 2000) >>> s1 = np.sin(2 * time) # Signal 1 : sinusoidal signal >>> s2 = np.sign(np.sin(3 * time)) # Signal 2 : square signal >>> s3 = signal.sawtooth(2 * np.pi * time) # Signal 3: saw tooth signal >>> S = np.c_[s1, s2, s3] >>> S += 0.2 * np.random.normal(size=S.shape) # Add noise >>> S /= S.std(axis=0) # Standardize data >>> # Mix data >>> A = np.array([[1, 1, 1], [0.5, 2, 1], [1.5, 1, 2]]) # Mixing matrix >>> X = np.dot(S, A.T) # Generate observations >>> # Compute ICA >>> ica = decomposition.FastICA() >>> S_ = ica.fit_transform(X) # Get the estimated sources >>> A_ = ica.mixing_.T >>> np.allclose(X, np.dot(S_, A_) + ica.mean_) True ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看