np.newaxis和np.power

1.np.power

numpy.power(x1, x2)

数组的元素分别求n次方。x2可以是数字,也可以是数组,但是x1和x2的列数要相同。

x1 = range(6)
>>> x1
[0, 1, 2, 3, 4, 5]
>>> np.power(x1, 3)
array([  0,   1,   8,  27,  64, 125])
>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.power(x1, x2)
array([  0.,   1.,   8.,  27.,  16.,   5.])
>>> x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
>>> x2
array([[1, 2, 3, 3, 2, 1],
       [1, 2, 3, 3, 2, 1]])
>>> np.power(x1, x2)
array([[ 0,  1,  8, 27, 16,  5],
       [ 0,  1,  8, 27, 16,  5]])

2.np.newaxis()

np.newaxis,增加维度

np.linspace(1, 10, 10)
  array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
np.linspace(1, 10, 10)[np.newaxis,:]
 array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]])
 np.linspace(1, 10, 10)[:,np.newaxis]
  array
 ([[ 1.],
  [ 2.],
  [ 3.],
  [ 4.],
  [ 5.],
  [ 6.],
  [ 7.],
  [ 8.],
  [ 9.],
  [ 10.]])

In [4]: np.linspace(1, 10, 10).shape
Out[4]: (10,)%数组

In [5]: np.linspace(1, 10, 10)[np.newaxis,:].shape
Out[5]: (1, 10)%矩阵

In [6]: np.linspace(1, 10, 10)[:,np.newaxis].shape
Out[6]: (10, 1)
import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体 plt.rcParams['axes.unicode_minus'] = False # 正确显示负号 # 1. 生成二维数据 n = 10000 # 数据点数量 x1 = np.random.uniform(-1, 1, n) v = np.random.normal(0, 0.04, n) x2 = x1**2 + v X = np.column_stack((x1, x2)) # 2. 核矩阵计算与中心化 def compute_kernel_matrix(X, d): n = X.shape[0] K = np.dot(X, X.T) # 初始点积矩阵 if d > 1: K = np.power(K, d) return K def center_kernel_matrix(K): n = K.shape[0] ones_n = np.ones((n, n)) / n return K - ones_n @ K - K @ ones_n + ones_n @ K @ ones_n # 3. 特征值分析(使用对称矩阵特征分解) def get_top_eigenvalues(K, top_k=3): eigenvalues, eigenvectors = np.linalg.eigh(K) # 改用eigh return np.sort(eigenvalues)[::-1][:top_k], eigenvectors # 4. 执行KPCA并记录结果 d_values = [1, 2, 3, 4] eigenvalues_dict = {} eigenvectors_dict = {} for d in d_values: K = compute_kernel_matrix(X, d) centered_K = center_kernel_matrix(K) eigenvalues, eigenvectors = get_top_eigenvalues(centered_K) eigenvalues_dict[d] = eigenvalues eigenvectors_dict[d] = eigenvectors # 5. 可视化结果 plt.figure(figsize=(10, 8)) # 特征值对比图 plt.subplot(2, 1, 1) for d in d_values: eigenvalues = eigenvalues_dict[d] plt.plot(range(1, len(eigenvalues)+1), eigenvalues, marker='o', label=f'd={d}') plt.xlabel('主成分序号') plt.ylabel('特征值') plt.title('不同核函数次数下的特征值对比') plt.legend() # 数据分布与主成分方向图 plt.subplot(2, 1, 2) plt.scatter(X[:, 0], X[:, 1], alpha=0.5, label='原始数据') # 绘制主成分方向(以d=2为例) d = 2 K = compute_kernel_matrix(X, d) centered_K = center_kernel_matrix(K) eigenvalues, eigenvectors = get_top_eigenvalues(centered_K) sorted_indices = np.argsort(eigenvalues)[::-1] top_eigenvectors = eigenvectors[:, sorted_indices[:2]] # 转换为特征空间的主成分方向 phi_vectors = (top_eigenvectors.T @ centered_K) / np.sqrt(eigenvalues[sorted_indices[:2]])[:, np.newaxis] # 归一化方向向量 phi_vectors_normalized = phi_vectors / np.linalg.norm(phi_vectors, axis=1)[:, np.newaxis] # 绘制主成分方向 for i i
最新发布
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值