PCA降维和TSNE降维的对比


# -*- coding: utf-8 -*-
"""
Created on Mon Nov 15 21:48:20 2021
@author: guangjie2333
"""
import keras
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.decomposition import PCA
from keras.preprocessing import image
from sklearn import svm
from sklearn import metrics
from sklearn.model_selection import GridSearchCV
from sklearn.manifold import TSNE


np.random.seed(1)

#宏定义
switch = 0

'数据解析'
BreastMnistData = np.load('breastmnist.npz')
print(BreastMnistData.files)
train_images = BreastMnistData['train_images']
val_images = BreastMnistData['val_images']
test_images = BreastMnistData['test_images']
train_labels = BreastMnistData['train_labels']
val_labels = BreastMnistData['val_labels']
test_labels = BreastMnistData['test_labels']



X = []
'数据展示'
print(train_images.shape)
print(train_images.shape[0])

'训练集PCA降维'
train_feature = []
for idx, image in enumerate(train_images):
    img_feature = image.flatten()
    train_feature.append(img_feature)


train_feature = np.array(train_feature)
print('trian_feature.shape:', train_feature.shape)

if switch == 0 :
    pca = PCA(n_components=3) # 降成3维    
    train_pca = pca.fit_transform(train_feature)
    print(train_pca)
    print('trian_pca:shape:', train_pca.shape)
    x, y, z = train_pca[:,0], train_pca[:,1], train_pca[:,2]
else:
    tsne = TSNE(n_components=3) # 降成3维    
    train_tsen = tsne.fit_transform(train_feature)
    print(train_tsen)
    print('train_tsen:shape:', train_tsen.shape)
    x, y, z = train_tsen[:,0], train_tsen[:,1], train_tsen[:,2]

'''
# 2D绘图
plt.figure(figsize=(12, 6))
plt.scatter(train_pca[:,0], train_pca[:,1], c=train_labels)
plt.colorbar()
plt.title('Use of PCA')
'''

if switch == 0 :
    # 3D绘图\n",
    fig = plt.figure(figsize=(12,6))
    ax = fig.add_subplot(111,projection='3d')  # 创建一个三维的绘图工程"
    
    ax.scatter(x, y, z, c=train_labels)
    ax.set_zlabel('Z')  # 坐标轴"
    ax.set_ylabel('Y')
    ax.set_xlabel('X')
    plt.title("method of PCA")
else:
    fig = plt.figure(figsize=(12,6))
    ax = fig.add_subplot(111,projection='3d')  # 创建一个三维的绘图工程"
    
    ax.scatter(x, y, z, c=train_labels)
    ax.set_zlabel('Z')  # 坐标轴"
    ax.set_ylabel('Y')
    ax.set_xlabel('X')
    plt.title("method of TSNE")




'验证集PCA降维'
val_feature = []
for idx, image in enumerate(val_images):
    img_feature = image.flatten()
    val_feature.append(img_feature)


val_feature = np.array(val_feature)
print('val_feature.shape:', val_feature.shape)

if switch == 0 :
    pca = PCA(n_components=3) # 降成3维    
    val_pca = pca.fit_transform(val_feature)
    print(val_pca)
    print('val_pca:shape:', val_pca.shape)
else:
    tsne = TSNE(n_components=3) # 降成3维    
    val_tsne = tsne.fit_transform(val_feature)
    print(val_tsne)
    print('val_tsne:shape:', val_tsne.shape)


'测试集PCA降维'
test_feature = []
for idx, image in enumerate(test_images):
    img_feature = image.flatten()
    test_feature.append(img_feature)


test_feature = np.array(test_feature)
print('val_feature.shape:', test_feature.shape)

if switch == 0 :
    pca = PCA(n_components=3) # 降成3维    
    test_pca = pca.fit_transform(test_feature)
    print(test_pca)
    print('val_pca:shape:', test_pca.shape)
else:
    tsne = TSNE(n_components=3) # 降成3维    
    test_tsne = tsne.fit_transform(val_feature)
    print(test_tsne)
    print('test_tsne:shape:', test_tsne.shape)



#基于SVM验证
'''
parameters={'kernel':['linear','rbf','sigmoid','poly'],'C':np.linspace(0.1,20,5),'gamma':np.linspace(0.1,20,5)}
svc = svm.SVC()
clf = GridSearchCV(svc,parameters,cv=5,scoring='accuracy')
'''

clf = svm.SVC(kernel = 'poly', C = 1)
if switch == 0 :
   clf.fit(train_pca,train_labels)
   y_val_predict = clf.predict(val_pca)
else:
   clf.fit(train_tsen,train_labels)
   y_val_predict = clf.predict(val_tsne)
    
valResult = metrics.accuracy_score(val_labels,y_val_predict)
        
#计算精度
print("Accuracy",valResult);

版权声明:本文为CSDN博主「guangjie2333」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/guangjie2333/article/details/121518213

guangjie2333

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

【3D 目标检测】M3DETR

本文可以借鉴的另一篇博客: 【3D 目标检测】PVRCNN详解(个人阅读心得并总结其他人的结论得出的文章)_JY.Wang_China的博客-CSDN博客 M3DETR:Multi representation, Multi scale,