Exemple #1
0
#
print('Dimension reductino on example dataset.')

# Plot the normalized dataset (returned from pca)
plt.figure()
plt.scatter(X_norm[:, 0],
            X_norm[:, 1],
            facecolors='none',
            edgecolors='b',
            s=20)
plt.axis('equal')
plt.axis([-4, 3, -4, 3])
# plt.show()
# Project the data onto K = 1 dimension
K = 1
Z = pd.project_data(X_norm, U, K)
print('Projection of the first example: {}'.format(Z[0]))
print('(this value should be about 1.481274)')

X_rec = rd.recover_data(Z, U, K)

print('Approximation of the first example: {}'.format(X_rec[0]))
print('(this value should be about [-1.047419 -1.047419])')

# Draw lines connecting the projected points to the original points
plt.scatter(X_rec[:, 0], X_rec[:, 1], facecolors='none', edgecolors='r', s=20)
for i in range(X_norm.shape[0]):
    rk.draw_line(X_norm[i], X_rec[i])
plt.axis([-4, 3, -4, 3])
plt.show()
input('Program paused. Press ENTER to continue')
Exemple #2
0
'''第3部分 得到降维后的样本点 再进行压缩重放'''
print('Dimension reductino on example dataset.')

# 可视化特征缩放后的数据集
plt.figure()
plt.scatter(X_norm[:, 0],
            X_norm[:, 1],
            facecolors='none',
            edgecolors='b',
            s=20)
plt.axis('equal')
plt.axis([-4, 3, -4, 3])

# 将2维数据映射到1维
K = 1
Z = pd.project_data(X_norm, U, K)
print('Projection of the first example: {}'.format(Z[0]))
print('(this value should be about 1.481274)')

X_rec = rd.recover_data(Z, U, K)  #将降维后的1维数据 转换为2维(在特征向量上的投影点)

print('Approximation of the first example: {}'.format(X_rec[0]))
print('(this value should be about [-1.047419 -1.047419])')

# 画出特征缩放后的样本在特征向量上的投影点 并在2者之间连线
plt.scatter(X_rec[:, 0], X_rec[:, 1], facecolors='none', edgecolors='r', s=20)
for i in range(X_norm.shape[0]):
    rk.draw_line(X_norm[i], X_rec[i])

input('Program paused. Press ENTER to continue')
'''第4部分 加载并可视化人脸数据集'''