機器學習-降低維度演算法
阿新 • • 發佈:2021-01-19
像聚類演算法一樣,降低維度演算法試圖分析資料的內在結構,不過降低維度演算法是以非監督學習的方式試圖利用較少的資訊來歸納或者解釋資料。這類演算法可以用於高維資料的視覺化或者用來簡化資料以便監督式學習使用。
常見的演算法包括:主成份分析,偏最小二乘迴歸, Sammon對映,多維尺度, 投影追蹤等。
from __future__ import print_function from sklearn import datasets import matplotlib.pyplot as plt import matplotlib.cm as cmx import matplotlib.colorsas colors import numpy as np def shuffle_data(X, y, seed=None): if seed: np.random.seed(seed) idx = np.arange(X.shape[0]) np.random.shuffle(idx) return X[idx], y[idx] # 正規化資料集 X def normalize(X, axis=-1, p=2): lp_norm = np.atleast_1d(np.linalg.norm(X, p, axis)) lp_norm[lp_norm== 0] = 1 return X / np.expand_dims(lp_norm, axis) # 標準化資料集 X def standardize(X): X_std = np.zeros(X.shape) mean = X.mean(axis=0) std = X.std(axis=0) # 做除法運算時請永遠記住分母不能等於0的情形 # X_std = (X - X.mean(axis=0)) / X.std(axis=0) for col in range(np.shape(X)[1]): if std[col]: X_std[:, col]= (X_std[:, col] - mean[col]) / std[col] return X_std # 劃分資料集為訓練集和測試集 def train_test_split(X, y, test_size=0.2, shuffle=True, seed=None): if shuffle: X, y = shuffle_data(X, y, seed) n_train_samples = int(X.shape[0] * (1-test_size)) x_train, x_test = X[:n_train_samples], X[n_train_samples:] y_train, y_test = y[:n_train_samples], y[n_train_samples:] return x_train, x_test, y_train, y_test # 計算矩陣X的協方差矩陣 def calculate_covariance_matrix(X, Y=np.empty((0,0))): if not Y.any(): Y = X n_samples = np.shape(X)[0] covariance_matrix = (1 / (n_samples-1)) * (X - X.mean(axis=0)).T.dot(Y - Y.mean(axis=0)) return np.array(covariance_matrix, dtype=float) # 計算資料集X每列的方差 def calculate_variance(X): n_samples = np.shape(X)[0] variance = (1 / n_samples) * np.diag((X - X.mean(axis=0)).T.dot(X - X.mean(axis=0))) return variance # 計算資料集X每列的標準差 def calculate_std_dev(X): std_dev = np.sqrt(calculate_variance(X)) return std_dev # 計算相關係數矩陣 def calculate_correlation_matrix(X, Y=np.empty([0])): # 先計算協方差矩陣 covariance_matrix = calculate_covariance_matrix(X, Y) # 計算X, Y的標準差 std_dev_X = np.expand_dims(calculate_std_dev(X), 1) std_dev_y = np.expand_dims(calculate_std_dev(Y), 1) correlation_matrix = np.divide(covariance_matrix, std_dev_X.dot(std_dev_y.T)) return np.array(correlation_matrix, dtype=float) class PCA(): """ 主成份分析演算法PCA,非監督學習演算法. """ def __init__(self): self.eigen_values = None self.eigen_vectors = None self.k = 2 def transform(self, X): """ 將原始資料集X通過PCA進行降維 """ covariance = calculate_covariance_matrix(X) # 求解特徵值和特徵向量 self.eigen_values, self.eigen_vectors = np.linalg.eig(covariance) # 將特徵值從大到小進行排序,注意特徵向量是按列排的,即self.eigen_vectors第k列是self.eigen_values中第k個特徵值對應的特徵向量 idx = self.eigen_values.argsort()[::-1] eigenvalues = self.eigen_values[idx][:self.k] eigenvectors = self.eigen_vectors[:, idx][:, :self.k] # 將原始資料集X對映到低維空間 X_transformed = X.dot(eigenvectors) return X_transformed def main(): # Load the dataset data = datasets.load_iris() X = data.data y = data.target # 將資料集X對映到低維空間 X_trans = PCA().transform(X) x1 = X_trans[:, 0] x2 = X_trans[:, 1] cmap = plt.get_cmap('viridis') colors = [cmap(i) for i in np.linspace(0, 1, len(np.unique(y)))] class_distr = [] # Plot the different class distributions for i, l in enumerate(np.unique(y)): _x1 = x1[y == l] _x2 = x2[y == l] _y = y[y == l] class_distr.append(plt.scatter(_x1, _x2, color=colors[i])) # Add a legend plt.legend(class_distr, y, loc=1) # Axis labels plt.xlabel('Principal Component 1') plt.ylabel('Principal Component 2') plt.show() if __name__ == "__main__": main()