使用 Python 进行主成分分析

http://img4.mukewang.com/60c964f40001a1ac19170597.jpg

是 2D 散点数据的示例,但是当我绘制特征向量时,该图被压缩为 1D image

http://img4.mukewang.com/60c965060001407314480548.jpg

我正在尝试对温度和应变数据进行 PCA。


为什么我有散射和绘制特征向量的组合的一维向量?


def process_data_PCA(temperature, strain):

    print("process data")

    T1 = temperature['T1'].tolist()

    T2 = temperature['T2'].tolist()

    T3 = temperature['T3'].tolist()

    T4 = temperature['T4'].tolist()

    T5 = temperature['T5'].tolist()

    T6 = temperature['T6'].tolist()

    T7 = temperature['T7'].tolist()

    T8 = temperature['T8'].tolist()

    T9 = temperature['T9'].tolist()

    T10 = temperature['T10'].tolist()


    W_A1 = strain[0]

    N = len(T1)

    xData =  T1

    yData =  W_A1

    xData = np.reshape(xData, (N, 1))

    yData = np.reshape(yData, (N, 1))


    data = np.hstack((xData, yData))

    print(data)

    mu = data.mean(axis=0)

    data = data - mu

    # data = (data - mu)/data.std(axis=0)  # Uncommenting this reproduces mlab.PCA results

    eigenvectors, eigenvalues, V = np.linalg.svd(data.T, full_matrices=False)

    projected_data = np.dot(data, eigenvectors)

    sigma = projected_data.std(axis=0).mean()

    print(eigenvectors)


    fig, ax = plt.subplots()

    ax.scatter(xData, yData, s= 0.1)

    for axis in eigenvectors:

        start, end = mu, mu + sigma * axis

        ax.annotate(

            '', xy=end, xycoords='data',

            xytext=start, textcoords='data',

            arrowprops=dict(facecolor='red', width=2.0))

    ax.set_aspect('equal')

    plt.show()

打印(数据)


[[14.25        0.        ]

 [14.25        0.        ]

 [14.26        0.        ]

 ...

 [12.51       -0.02470534]

 [12.51       -0.02540376]

 [12.52       -0.02542746]]

[[-0.99999927 -0.00120856]

 [-0.00120856  0.99999927]]

特征向量 [-0.99999927 -0.00120856] [-0.00120856 0.99999927]


开始,结束


 1.95096698e+01 -5.70968943e-03] [ 1.7057429e+01 -8.6733643e-03]

[ 1.95096698e+01 -5.70968943e-03] [19.50670611  2.44653112]


慕勒3428872
浏览 261回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python