from numpy import *import numpyasnp
import matplotlib.pyplotaspltfromscipy.io import loadmat
cx= mat([[2.5, 2.4],
[0.5, 0.7],
[2.2, 2.9],
[1.9, 2.2],
[3.1, 3.0],
[2.3, 2.7],
[2, 1.6],
[1, 1.1],
[1.5, 1.6],
[1.1, 0.9]])
# print(cx.shape)
sz=cx.shape
m= sz[0]
n= sz[1]
# 显示原数据
def plot_oridata( cx ):
plt.figure(num='原数据图', figsize=(6, 6))
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.xlim((-1, 4))
plt.ylim((-1, 4))
new_ticks= np.arange(-1, 4, 0.5)
plt.xticks(new_ticks)
plt.yticks(new_ticks)
plt.scatter(cx[:,0].tolist(), cx[:, 1].tolist(), c='r', marker='+')
plt.plot([0, 0], [-1, 4], 'k-')
plt.plot([-1, 4], [0, 0], 'k-')
plt.show()return#求协方差矩阵
def get_covMat( cx ):
print('+++++++++++++ 求协方差矩阵 +++++++++++++++')
# 零均值化
ecol= np.mean(cx, axis=0)
cx1= (cx[:, 0]) - ecol[0, 0]
cx2= cx[:, 1] - ecol[0, 1]
Mcx=np.column_stack((cx1, cx2))
Covx= np.transpose(Mcx)*Mcx/(m-1)
# print(Covx)returnCovx, Mcx
#计算特征值和特征向量
def get_eign(Covx, k):
eVals, eVecs=np.linalg.eig(Covx)
# print(eVals)
# print(eVecs,' ', eVecs.shape)
sorted_indices=np.argsort(eVals)
topk_evecs= eVecs[:, sorted_indices[:-k-1:-1]]
# print(topk_evecs)
plt.figure(num='特征向量', figsize=(6, 6))
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.xlim((-4, 4))
plt.ylim((-4, 4))
new_ticks= np.arange(-4, 4, 1)
plt.xticks(new_ticks)
plt.yticks(new_ticks)
plt.scatter(cx[:,0].tolist(), cx[:, 1].tolist(), c='r', marker='+')
plt.plot([0, 0], [-4, 4], 'k-.')
plt.plot([-4, 4], [0, 0], 'k-.')
# print(eVecs[0, 0], eVecs[1, 0])
# print(eVecs)
plt.plot([0, eVecs[0, 0] * 6], [0, eVecs[1, 0] * 6], 'b:')
plt.plot([0, eVecs[0, 1] * 6], [0, eVecs[1, 1] * 6], 'b:')
plt.plot([0, eVecs[0, 0] * -6], [0, eVecs[1, 0] * -6], 'b:')
plt.plot([0, eVecs[0, 1] * -6], [0, eVecs[1, 1] * -6], 'b:')
plt.show()returneVecs, topk_evecs
#转换数据
def transform_data(eVecs, Mcx):
print("------------------转换数据---------------------")
tran_data= Mcx *eVecs
plt.figure(num='转换数据', figsize=(6, 6))
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.xlim((-2, 2))
plt.ylim((-2, 2))
new_ticks= np.arange(-2, 2, 0.5)
plt.xticks(new_ticks)
plt.yticks(new_ticks)
plt.scatter(tran_data[:,0].tolist(), tran_data[:, 1].tolist(), c='r', marker='+')#哪一维对应x,哪一维对应y
plt.plot([0, 0], [-4, 4], 'k-.')
plt.plot([-4, 4], [0, 0], 'k-.')
# print(eVecs[0, 0], eVecs[1, 0])
# print(eVecs)
plt.show()return#压缩数据
def compress_data(Mcx, topkevecs, eVecs):
print("------------------压缩数据---------------------")
comdata= Mcx *topkevecs
c1= np.zeros((10, 1), dtype=int)
comdata1=np.column_stack((c1, comdata))
comdata2= comdata1 *eVecs
plt.figure(num='压缩数据', figsize=(6, 6))
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.xlim((-4, 4))
plt.ylim((-4, 4))
new_ticks= np.arange(-4, 4, 0.5)
plt.xticks(new_ticks)
plt.yticks(new_ticks)
plt.scatter(comdata2[:,0].tolist(), comdata2[:, 1].tolist(), c='r', marker='+') # 哪一维对应x,哪一维对应y
plt.plot([0, 0], [-4, 4], 'k-.')
plt.plot([-4, 4], [0, 0], 'k-.')
plt.plot([0, eVecs[0, 0] * 6], [0, eVecs[1, 0] * 6], 'b:')
plt.plot([0, eVecs[0, 1] * 6], [0, eVecs[1, 1] * 6], 'b:')
plt.plot([0, eVecs[0, 0] * -6], [0, eVecs[1, 0] * -6], 'b:')
plt.plot([0, eVecs[0, 1] * -6], [0, eVecs[1, 1] * -6], 'b:')
# print(eVecs[0, 0], eVecs[1, 0])
# print(eVecs)
plt.show()returnplot_oridata(cx)
Covx, Mcx=get_covMat(cx)
eVecs, topk_evecs= get_eign(Covx, 1)
transform_data(eVecs, Mcx)
compress_data(Mcx, topk_evecs, eVecs)
print('end')