精华内容
下载资源
问答
  • PCA降维和TSNE降维的对比

    千次阅读 2021-11-24 16:12:20
    # -*- 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 ...
    
    # -*- 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);
    
    
    展开全文
  •   本文使用PCA、ICA、TSNE等降维模型进行了对比实验,分别对原始数据进行降维并可视化展示,然后分别用PCA、ICA、TSNE降维后的数据训练一个简单MLP神经网络用于评估降维的数据质量。最后用三个模型的预测效果进行...

      本文使用PCA、ICA、TSNE等降维模型进行了对比实验,分别对原始数据进行降维并可视化展示,然后分别用PCA、ICA、TSNE降维后的数据训练一个简单MLP神经网络用于评估降维的数据质量。最后用三个模型的预测效果进行了对比和结果的展示。
      本文使用的数据集在本人上传的资源中,链接为mock_kaggle.csv

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import tensorflow as tf
    from sklearn.decomposition import FastICA,PCA
    from sklearn.manifold import TSNE
    from sklearn import preprocessing
    

    取数据

    data=pd.read_csv('mock_kaggle.csv',encoding ='gbk',parse_dates=['datetime'])
    
    data=data.iloc[:,1:]
    
    data
    
    特价股票价格
    0049721.29
    17049021.29
    25948431.29
    39347501.29
    49646541.29
    ............
    9329831792.39
    93310830712.39
    93412840952.39
    93527038252.39
    93618336422.39

    937 rows × 3 columns

    标准化

    data_normal = preprocessing.scale(data)
    train_label=data_normal[:900,1]
    test_label=data_normal[901:,1]
    

    PCA降维

    pca = PCA(n_components=2)
    pca_data = pca.fit_transform(data_normal)     #等价于pca.fit(X) pca.transform(X)
    #invX = pca.inverse_transform(X)  #将降维后的数据转换成原始数据
    print(pca.explained_variance_ratio_)#输出降维后的各特征贡献度
    
    [0.38909061 0.3429785 ]
    
    plt.figure(figsize=(12,8))
    plt.title('PCA Components')
    plt.scatter(pca_data[:,0], pca_data[:,1])
    

    在这里插入图片描述

    训练简单神经网络

    pca_train_data=pca_data[:900]
    pca_test_data=pca_data[901:]
    pca_model=tf.keras.Sequential([tf.keras.layers.Dense(10,input_shape=(2,),activation='relu'),
                               tf.keras.layers.Dense(10,activation='relu'),
                               tf.keras.layers.Dense(1)])
    pca_model.summary()#查看模型基本信息
    pca_model.compile(optimizer='adam',loss='mse',metrics=['mape']) #设置优化器和损失函数
    pca_history=pca_model.fit(pca_train_data,train_label,epochs=100) #每个数据训练1000次
    
    Model: "sequential"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense (Dense)                (None, 10)                30        
    _________________________________________________________________
    dense_1 (Dense)              (None, 10)                110       
    _________________________________________________________________
    dense_2 (Dense)              (None, 1)                 11        
    =================================================================
    Total params: 151
    Trainable params: 151
    Non-trainable params: 0
    _________________________________________________________________
    Train on 900 samples
    Epoch 1/100
    900/900 [==============================] - 2s 2ms/sample - loss: 0.7850 - mape: 174.4778
    Epoch 2/100
    900/900 [==============================] - 0s 231us/sample - loss: 0.6291 - mape: 203.4696
    Epoch 3/100
    900/900 [==============================] - 0s 184us/sample - loss: 0.5221 - mape: 235.8750
    Epoch 4/100
    900/900 [==============================] - 0s 182us/sample - loss: 0.4389 - mape: 260.7603
    Epoch 5/100
    900/900 [==============================] - 0s 172us/sample - loss: 0.3774 - mape: 280.1786
    Epoch 6/100
    900/900 [==============================] - 0s 184us/sample - loss: 0.3348 - mape: 283.5222
    Epoch 7/100
    900/900 [==============================] - 0s 310us/sample - loss: 0.3014 - mape: 286.3506
    Epoch 8/100
    900/900 [==============================] - 0s 230us/sample - loss: 0.2762 - mape: 291.6097
    Epoch 9/100
    900/900 [==============================] - 0s 234us/sample - loss: 0.2539 - mape: 279.0452
    Epoch 10/100
    900/900 [==============================] - 0s 199us/sample - loss: 0.2364 - mape: 267.8730
    Epoch 11/100
    900/900 [==============================] - 0s 187us/sample - loss: 0.2219 - mape: 254.5653
    Epoch 12/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.2095 - mape: 241.9442
    Epoch 13/100
    900/900 [==============================] - 0s 266us/sample - loss: 0.1987 - mape: 234.5619
    Epoch 14/100
    900/900 [==============================] - 0s 184us/sample - loss: 0.1884 - mape: 225.1507
    Epoch 15/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.1811 - mape: 221.8909
    Epoch 16/100
    900/900 [==============================] - 0s 214us/sample - loss: 0.1749 - mape: 205.1877
    Epoch 17/100
    900/900 [==============================] - 0s 199us/sample - loss: 0.1702 - mape: 204.4365
    Epoch 18/100
    900/900 [==============================] - 0s 189us/sample - loss: 0.1649 - mape: 198.4753
    Epoch 19/100
    900/900 [==============================] - 0s 186us/sample - loss: 0.1621 - mape: 205.5184
    Epoch 20/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1590 - mape: 195.3820
    Epoch 21/100
    900/900 [==============================] - 0s 196us/sample - loss: 0.1569 - mape: 197.1003
    Epoch 22/100
    900/900 [==============================] - 0s 199us/sample - loss: 0.1547 - mape: 193.8486
    Epoch 23/100
    900/900 [==============================] - 0s 181us/sample - loss: 0.1543 - mape: 192.1595
    Epoch 24/100
    900/900 [==============================] - 0s 185us/sample - loss: 0.1519 - mape: 192.7433
    Epoch 25/100
    900/900 [==============================] - 0s 169us/sample - loss: 0.1519 - mape: 190.2121
    Epoch 26/100
    900/900 [==============================] - 0s 171us/sample - loss: 0.1508 - mape: 196.8424
    Epoch 27/100
    900/900 [==============================] - 0s 193us/sample - loss: 0.1510 - mape: 187.2915
    Epoch 28/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1489 - mape: 187.9957
    Epoch 29/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1494 - mape: 191.7432
    Epoch 30/100
    900/900 [==============================] - 0s 182us/sample - loss: 0.1485 - mape: 196.7090
    Epoch 31/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.1477 - mape: 189.1148
    Epoch 32/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.1468 - mape: 193.2476
    Epoch 33/100
    900/900 [==============================] - 0s 176us/sample - loss: 0.1473 - mape: 191.6133
    Epoch 34/100
    900/900 [==============================] - 0s 173us/sample - loss: 0.1464 - mape: 186.4275
    Epoch 35/100
    900/900 [==============================] - 0s 233us/sample - loss: 0.1454 - mape: 190.5075
    Epoch 36/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.1450 - mape: 189.6160
    Epoch 37/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1450 - mape: 190.8441
    Epoch 38/100
    900/900 [==============================] - 0s 176us/sample - loss: 0.1449 - mape: 191.0887
    Epoch 39/100
    900/900 [==============================] - 0s 192us/sample - loss: 0.1446 - mape: 195.8095
    Epoch 40/100
    900/900 [==============================] - 0s 203us/sample - loss: 0.1446 - mape: 187.4518
    Epoch 41/100
    900/900 [==============================] - 0s 188us/sample - loss: 0.1441 - mape: 191.2747
    Epoch 42/100
    900/900 [==============================] - 0s 243us/sample - loss: 0.1441 - mape: 191.9978
    Epoch 43/100
    900/900 [==============================] - 0s 217us/sample - loss: 0.1439 - mape: 185.9973
    Epoch 44/100
    900/900 [==============================] - 0s 198us/sample - loss: 0.1446 - mape: 186.2656
    Epoch 45/100
    900/900 [==============================] - 0s 198us/sample - loss: 0.1439 - mape: 179.1912
    Epoch 46/100
    900/900 [==============================] - 0s 217us/sample - loss: 0.1443 - mape: 189.1231
    Epoch 47/100
    900/900 [==============================] - 0s 221us/sample - loss: 0.1446 - mape: 190.8205
    Epoch 48/100
    900/900 [==============================] - 0s 201us/sample - loss: 0.1426 - mape: 197.7570
    Epoch 49/100
    900/900 [==============================] - 0s 185us/sample - loss: 0.1425 - mape: 191.7054
    Epoch 50/100
    900/900 [==============================] - 0s 235us/sample - loss: 0.1424 - mape: 195.1246
    Epoch 51/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.1417 - mape: 188.4568
    Epoch 52/100
    900/900 [==============================] - 0s 247us/sample - loss: 0.1422 - mape: 190.8196
    Epoch 53/100
    900/900 [==============================] - 0s 175us/sample - loss: 0.1419 - mape: 195.7927
    Epoch 54/100
    900/900 [==============================] - 0s 192us/sample - loss: 0.1421 - mape: 192.0233
    Epoch 55/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.1416 - mape: 189.6909
    Epoch 56/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.1410 - mape: 189.0969
    Epoch 57/100
    900/900 [==============================] - 0s 187us/sample - loss: 0.1415 - mape: 182.2992
    Epoch 58/100
    900/900 [==============================] - 0s 187us/sample - loss: 0.1412 - mape: 189.5773
    Epoch 59/100
    900/900 [==============================] - 0s 175us/sample - loss: 0.1417 - mape: 193.3024
    Epoch 60/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.1411 - mape: 188.5672
    Epoch 61/100
    900/900 [==============================] - 0s 248us/sample - loss: 0.1422 - mape: 187.6868
    Epoch 62/100
    900/900 [==============================] - 0s 182us/sample - loss: 0.1424 - mape: 197.4033
    Epoch 63/100
    900/900 [==============================] - 0s 188us/sample - loss: 0.1425 - mape: 189.5185
    Epoch 64/100
    900/900 [==============================] - 0s 216us/sample - loss: 0.1411 - mape: 192.8488
    Epoch 65/100
    900/900 [==============================] - 0s 260us/sample - loss: 0.1413 - mape: 185.5174
    Epoch 66/100
    900/900 [==============================] - 0s 194us/sample - loss: 0.1405 - mape: 189.3971
    Epoch 67/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1407 - mape: 186.0119
    Epoch 68/100
    900/900 [==============================] - 0s 182us/sample - loss: 0.1430 - mape: 187.2527
    Epoch 69/100
    900/900 [==============================] - 0s 233us/sample - loss: 0.1410 - mape: 183.7318
    Epoch 70/100
    900/900 [==============================] - 0s 225us/sample - loss: 0.1409 - mape: 193.7119
    Epoch 71/100
    900/900 [==============================] - 0s 211us/sample - loss: 0.1402 - mape: 186.8175
    Epoch 72/100
    900/900 [==============================] - 0s 204us/sample - loss: 0.1400 - mape: 191.3500
    Epoch 73/100
    900/900 [==============================] - 0s 178us/sample - loss: 0.1396 - mape: 184.2501
    Epoch 74/100
    900/900 [==============================] - 0s 178us/sample - loss: 0.1411 - mape: 191.1308
    Epoch 75/100
    900/900 [==============================] - 0s 175us/sample - loss: 0.1399 - mape: 185.8354
    Epoch 76/100
    900/900 [==============================] - 0s 168us/sample - loss: 0.1397 - mape: 191.8246
    Epoch 77/100
    900/900 [==============================] - 0s 181us/sample - loss: 0.1395 - mape: 190.7547
    Epoch 78/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1397 - mape: 188.8087
    Epoch 79/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1399 - mape: 186.0689
    Epoch 80/100
    900/900 [==============================] - 0s 171us/sample - loss: 0.1397 - mape: 189.4801
    Epoch 81/100
    900/900 [==============================] - 0s 163us/sample - loss: 0.1396 - mape: 181.5564
    Epoch 82/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1398 - mape: 187.0418
    Epoch 83/100
    900/900 [==============================] - 0s 178us/sample - loss: 0.1393 - mape: 188.8249
    Epoch 84/100
    900/900 [==============================] - 0s 171us/sample - loss: 0.1419 - mape: 186.9317
    Epoch 85/100
    900/900 [==============================] - 0s 176us/sample - loss: 0.1400 - mape: 194.4677
    Epoch 86/100
    900/900 [==============================] - 0s 167us/sample - loss: 0.1401 - mape: 181.7322
    Epoch 87/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.1401 - mape: 190.9029
    Epoch 88/100
    900/900 [==============================] - 0s 172us/sample - loss: 0.1405 - mape: 192.8174
    Epoch 89/100
    900/900 [==============================] - 0s 172us/sample - loss: 0.1400 - mape: 186.8127
    Epoch 90/100
    900/900 [==============================] - 0s 176us/sample - loss: 0.1398 - mape: 188.7201
    Epoch 91/100
    900/900 [==============================] - 0s 171us/sample - loss: 0.1407 - mape: 185.1754
    Epoch 92/100
    900/900 [==============================] - 0s 168us/sample - loss: 0.1397 - mape: 192.7381
    Epoch 93/100
    900/900 [==============================] - 0s 221us/sample - loss: 0.1393 - mape: 186.2105
    Epoch 94/100
    900/900 [==============================] - 0s 199us/sample - loss: 0.1391 - mape: 181.0793
    Epoch 95/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.1389 - mape: 190.5739
    Epoch 96/100
    900/900 [==============================] - 0s 182us/sample - loss: 0.1382 - mape: 192.4536
    Epoch 97/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.1386 - mape: 182.1012
    Epoch 98/100
    900/900 [==============================] - 0s 186us/sample - loss: 0.1386 - mape: 190.5573
    Epoch 99/100
    900/900 [==============================] - 0s 196us/sample - loss: 0.1379 - mape: 186.4868
    Epoch 100/100
    900/900 [==============================] - 0s 271us/sample - loss: 0.1384 - mape: 189.7631
    

    ICA降维

    # ICA降维
    ica = FastICA(n_components=2)
    ica_data = ica.fit_transform(data_normal)  # 重构信号
    # A_ = ica.mixing_  # 获得估计混合后的矩阵
    
    plt.figure(figsize=(12,8))
    plt.title('ICA Components')
    plt.scatter(ica_data[:,0], ica_data[:,1])
    

    在这里插入图片描述

    训练简单神经网络

    ica_train_data=ica_data[:900]
    ica_test_data=ica_data[901:]
    ica_model=tf.keras.Sequential([tf.keras.layers.Dense(10,input_shape=(2,),activation='relu'),
                               tf.keras.layers.Dense(10,activation='relu'),
                               tf.keras.layers.Dense(1)])
    ica_model.summary()#查看模型基本信息
    ica_model.compile(optimizer='adam',loss='mse',metrics=['mape']) #设置优化器和损失函数
    ica_history=ica_model.fit(ica_train_data,train_label,epochs=100) #每个数据训练1000次
    
    Model: "sequential_1"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_3 (Dense)              (None, 10)                30        
    _________________________________________________________________
    dense_4 (Dense)              (None, 10)                110       
    _________________________________________________________________
    dense_5 (Dense)              (None, 1)                 11        
    =================================================================
    Total params: 151
    Trainable params: 151
    Non-trainable params: 0
    _________________________________________________________________
    Train on 900 samples
    Epoch 1/100
    900/900 [==============================] - 1s 1ms/sample - loss: 0.9873 - mape: 103.0597
    Epoch 2/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.9456 - mape: 110.8891
    Epoch 3/100
    900/900 [==============================] - 0s 170us/sample - loss: 0.9062 - mape: 117.8290
    Epoch 4/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.8612 - mape: 120.4753
    Epoch 5/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.8110 - mape: 122.2516
    Epoch 6/100
    900/900 [==============================] - 0s 185us/sample - loss: 0.7573 - mape: 134.9871
    Epoch 7/100
    900/900 [==============================] - 0s 176us/sample - loss: 0.6952 - mape: 135.3464
    Epoch 8/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.6329 - mape: 152.0235
    Epoch 9/100
    900/900 [==============================] - 0s 186us/sample - loss: 0.5675 - mape: 162.2172
    Epoch 10/100
    900/900 [==============================] - 0s 198us/sample - loss: 0.5150 - mape: 168.6576
    Epoch 11/100
    900/900 [==============================] - 0s 205us/sample - loss: 0.4536 - mape: 201.6551
    Epoch 12/100
    900/900 [==============================] - 0s 196us/sample - loss: 0.4089 - mape: 206.0919
    Epoch 13/100
    900/900 [==============================] - 0s 193us/sample - loss: 0.3707 - mape: 230.9236
    Epoch 14/100
    900/900 [==============================] - 0s 186us/sample - loss: 0.3410 - mape: 248.6063
    Epoch 15/100
    900/900 [==============================] - 0s 192us/sample - loss: 0.3208 - mape: 258.0475
    Epoch 16/100
    900/900 [==============================] - 0s 187us/sample - loss: 0.3083 - mape: 276.5189
    Epoch 17/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.2968 - mape: 287.3281
    Epoch 18/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.2902 - mape: 304.3135
    Epoch 19/100
    900/900 [==============================] - 0s 189us/sample - loss: 0.2842 - mape: 292.3876
    Epoch 20/100
    900/900 [==============================] - 0s 185us/sample - loss: 0.2802 - mape: 294.3054
    Epoch 21/100
    900/900 [==============================] - 0s 198us/sample - loss: 0.2762 - mape: 295.0771
    Epoch 22/100
    900/900 [==============================] - 0s 188us/sample - loss: 0.2723 - mape: 301.5387
    Epoch 23/100
    900/900 [==============================] - 0s 186us/sample - loss: 0.2673 - mape: 296.3002
    Epoch 24/100
    900/900 [==============================] - 0s 185us/sample - loss: 0.2655 - mape: 286.4019
    Epoch 25/100
    900/900 [==============================] - 0s 198us/sample - loss: 0.2650 - mape: 295.7499
    Epoch 26/100
    900/900 [==============================] - 0s 187us/sample - loss: 0.2597 - mape: 296.9456
    Epoch 27/100
    900/900 [==============================] - 0s 199us/sample - loss: 0.2601 - mape: 279.2125
    Epoch 28/100
    900/900 [==============================] - 0s 198us/sample - loss: 0.2533 - mape: 288.8799
    Epoch 29/100
    900/900 [==============================] - 0s 201us/sample - loss: 0.2469 - mape: 287.6161
    Epoch 30/100
    900/900 [==============================] - 0s 179us/sample - loss: 0.2436 - mape: 271.4766
    Epoch 31/100
    900/900 [==============================] - 0s 187us/sample - loss: 0.2377 - mape: 272.0242
    Epoch 32/100
    900/900 [==============================] - 0s 182us/sample - loss: 0.2349 - mape: 269.1444
    Epoch 33/100
    900/900 [==============================] - 0s 181us/sample - loss: 0.2317 - mape: 270.4389
    Epoch 34/100
    900/900 [==============================] - 0s 184us/sample - loss: 0.2291 - mape: 259.8926
    Epoch 35/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.2267 - mape: 255.4664
    Epoch 36/100
    900/900 [==============================] - 0s 189us/sample - loss: 0.2239 - mape: 261.1479
    Epoch 37/100
    900/900 [==============================] - 0s 209us/sample - loss: 0.2250 - mape: 266.7558
    Epoch 38/100
    900/900 [==============================] - 0s 217us/sample - loss: 0.2205 - mape: 264.2413
    Epoch 39/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.2184 - mape: 262.9426
    Epoch 40/100
    900/900 [==============================] - 0s 223us/sample - loss: 0.2140 - mape: 258.2297
    Epoch 41/100
    900/900 [==============================] - 0s 288us/sample - loss: 0.2117 - mape: 252.5215
    Epoch 42/100
    900/900 [==============================] - 0s 221us/sample - loss: 0.2106 - mape: 250.6110
    Epoch 43/100
    900/900 [==============================] - 0s 248us/sample - loss: 0.2076 - mape: 253.7611
    Epoch 44/100
    900/900 [==============================] - 0s 233us/sample - loss: 0.2073 - mape: 256.7436
    Epoch 45/100
    900/900 [==============================] - 0s 205us/sample - loss: 0.2053 - mape: 243.5077
    Epoch 46/100
    900/900 [==============================] - 0s 267us/sample - loss: 0.2019 - mape: 243.2053
    Epoch 47/100
    900/900 [==============================] - 0s 215us/sample - loss: 0.2019 - mape: 248.5658
    Epoch 48/100
    900/900 [==============================] - 0s 238us/sample - loss: 0.1990 - mape: 239.8024
    Epoch 49/100
    900/900 [==============================] - 0s 238us/sample - loss: 0.1977 - mape: 240.7237
    Epoch 50/100
    900/900 [==============================] - 0s 201us/sample - loss: 0.1963 - mape: 228.0875
    Epoch 51/100
    900/900 [==============================] - 0s 186us/sample - loss: 0.1986 - mape: 246.7753
    Epoch 52/100
    900/900 [==============================] - 0s 212us/sample - loss: 0.1927 - mape: 237.7096
    Epoch 53/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.1915 - mape: 237.9841
    Epoch 54/100
    900/900 [==============================] - 0s 194us/sample - loss: 0.1912 - mape: 227.1314
    Epoch 55/100
    900/900 [==============================] - 0s 173us/sample - loss: 0.1925 - mape: 232.2966
    Epoch 56/100
    900/900 [==============================] - 0s 295us/sample - loss: 0.1898 - mape: 236.6484
    Epoch 57/100
    900/900 [==============================] - 0s 197us/sample - loss: 0.1880 - mape: 236.1455
    Epoch 58/100
    900/900 [==============================] - 0s 272us/sample - loss: 0.1888 - mape: 237.5421
    Epoch 59/100
    900/900 [==============================] - 0s 222us/sample - loss: 0.1874 - mape: 240.4873
    Epoch 60/100
    900/900 [==============================] - 0s 257us/sample - loss: 0.1879 - mape: 226.1057
    Epoch 61/100
    900/900 [==============================] - 0s 253us/sample - loss: 0.1854 - mape: 227.4727
    Epoch 62/100
    900/900 [==============================] - 0s 194us/sample - loss: 0.1874 - mape: 225.6724
    Epoch 63/100
    900/900 [==============================] - 0s 175us/sample - loss: 0.1834 - mape: 227.1784
    Epoch 64/100
    900/900 [==============================] - 0s 221us/sample - loss: 0.1835 - mape: 230.1133
    Epoch 65/100
    900/900 [==============================] - 0s 262us/sample - loss: 0.1825 - mape: 222.8086
    Epoch 66/100
    900/900 [==============================] - 0s 236us/sample - loss: 0.1814 - mape: 217.2498
    Epoch 67/100
    900/900 [==============================] - 0s 223us/sample - loss: 0.1833 - mape: 226.5325
    Epoch 68/100
    900/900 [==============================] - 0s 240us/sample - loss: 0.1808 - mape: 215.9711
    Epoch 69/100
    900/900 [==============================] - 0s 228us/sample - loss: 0.1805 - mape: 222.7063
    Epoch 70/100
    900/900 [==============================] - 0s 197us/sample - loss: 0.1803 - mape: 220.8909
    Epoch 71/100
    900/900 [==============================] - 0s 201us/sample - loss: 0.1791 - mape: 218.5792
    Epoch 72/100
    900/900 [==============================] - 0s 281us/sample - loss: 0.1784 - mape: 215.1091
    Epoch 73/100
    900/900 [==============================] - 0s 248us/sample - loss: 0.1789 - mape: 213.6558
    Epoch 74/100
    900/900 [==============================] - 0s 213us/sample - loss: 0.1782 - mape: 217.4238
    Epoch 75/100
    900/900 [==============================] - 0s 266us/sample - loss: 0.1827 - mape: 243.4462
    Epoch 76/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.1766 - mape: 217.2935
    Epoch 77/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.1764 - mape: 216.2845
    Epoch 78/100
    900/900 [==============================] - 0s 168us/sample - loss: 0.1773 - mape: 213.9372
    Epoch 79/100
    900/900 [==============================] - 0s 223us/sample - loss: 0.1755 - mape: 218.9747
    Epoch 80/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.1774 - mape: 218.7054
    Epoch 81/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.1769 - mape: 212.9746
    Epoch 82/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.1759 - mape: 216.8962
    Epoch 83/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1749 - mape: 222.6659
    Epoch 84/100
    900/900 [==============================] - 0s 175us/sample - loss: 0.1752 - mape: 205.6387
    Epoch 85/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1743 - mape: 211.9823
    Epoch 86/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1737 - mape: 219.6483
    Epoch 87/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.1732 - mape: 217.6315
    Epoch 88/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.1741 - mape: 217.9983
    Epoch 89/100
    900/900 [==============================] - 0s 166us/sample - loss: 0.1745 - mape: 206.2586
    Epoch 90/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.1731 - mape: 222.3474
    Epoch 91/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.1720 - mape: 212.1967
    Epoch 92/100
    900/900 [==============================] - 0s 166us/sample - loss: 0.1744 - mape: 215.4123
    Epoch 93/100
    900/900 [==============================] - 0s 175us/sample - loss: 0.1752 - mape: 221.3754
    Epoch 94/100
    900/900 [==============================] - 0s 209us/sample - loss: 0.1727 - mape: 222.7617
    Epoch 95/100
    900/900 [==============================] - 0s 306us/sample - loss: 0.1719 - mape: 209.2482
    Epoch 96/100
    900/900 [==============================] - 0s 291us/sample - loss: 0.1716 - mape: 202.8925
    Epoch 97/100
    900/900 [==============================] - 0s 268us/sample - loss: 0.1711 - mape: 220.4949
    Epoch 98/100
    900/900 [==============================] - 0s 212us/sample - loss: 0.1705 - mape: 212.8038
    Epoch 99/100
    900/900 [==============================] - 0s 265us/sample - loss: 0.1736 - mape: 225.6118
    Epoch 100/100
    900/900 [==============================] - 0s 185us/sample - loss: 0.1723 - mape: 221.9312
    

    TSNE降维

    tsne = TSNE(method='exact',init='pca',n_components=2, random_state=0, perplexity=5)
    #learning rate:学习率,表示梯度下降的快慢,默认为200,建议取值在10到1000之间
    #n_iter:迭代次数,默认为1000,自定义设置时应保证大于250
    #perplexity:混乱度,表示t-SNE优化过程中考虑邻近点的多少,默认为30,建议取值在5到50之间
    #method:两种优化方法:barnets_hut和exact。第一种耗时O(NlogN),第二种耗时O(N^2)但是误差小
    #metric:表示向量间距离度量的方式,默认是欧氏距离。如果是precomputed,则输入X是计算好的距离矩阵。也可以是自定义的距离度量函数。
    #init:初始化,默认为random。取值为random为随机初始化,取值为pca为利用PCA进行初始化(常用),取值为numpy数组时必须shape=(n_samples, n_components)
    tsne_data = tsne.fit_transform(data_normal)
    
    tsne_data.shape,data_normal.shape
    
    ((937, 2), (937, 3))
    
    plt.figure(figsize=(12,8))
    plt.title('TSNE Components')
    plt.scatter(tsne_data[:,0], tsne_data[:,1])
    

    在这里插入图片描述

    训练简单神经网络

    tsne_train_data=tsne_data[:900]
    tsne_test_data=tsne_data[901:]
    tsne_model=tf.keras.Sequential([tf.keras.layers.Dense(10,input_shape=(2,),activation='relu'),
                               tf.keras.layers.Dense(10,activation='relu'),
                               tf.keras.layers.Dense(1)])
    tsne_model.summary()#查看模型基本信息
    tsne_model.compile(optimizer='adam',loss='mse',metrics=['mape']) #设置优化器和损失函数
    tsne_history=tsne_model.fit(tsne_train_data,train_label,epochs=100) #每个数据训练1000次
    
    Model: "sequential_2"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_6 (Dense)              (None, 10)                30        
    _________________________________________________________________
    dense_7 (Dense)              (None, 10)                110       
    _________________________________________________________________
    dense_8 (Dense)              (None, 1)                 11        
    =================================================================
    Total params: 151
    Trainable params: 151
    Non-trainable params: 0
    _________________________________________________________________
    Train on 900 samples
    Epoch 1/100
    900/900 [==============================] - 1s 2ms/sample - loss: 89.3856 - mape: 6934.5850
    Epoch 2/100
    900/900 [==============================] - 0s 212us/sample - loss: 21.7487 - mape: 3360.4509
    Epoch 3/100
    900/900 [==============================] - 0s 272us/sample - loss: 7.2081 - mape: 2058.5820
    Epoch 4/100
    900/900 [==============================] - 0s 267us/sample - loss: 3.9222 - mape: 1663.4132
    Epoch 5/100
    900/900 [==============================] - 0s 230us/sample - loss: 2.7726 - mape: 1321.7251
    Epoch 6/100
    900/900 [==============================] - 0s 248us/sample - loss: 2.2014 - mape: 1145.6675
    Epoch 7/100
    900/900 [==============================] - 0s 201us/sample - loss: 1.8586 - mape: 1012.2946
    Epoch 8/100
    900/900 [==============================] - 0s 213us/sample - loss: 1.6085 - mape: 903.4532
    Epoch 9/100
    900/900 [==============================] - 0s 303us/sample - loss: 1.3883 - mape: 829.0187
    Epoch 10/100
    900/900 [==============================] - 0s 208us/sample - loss: 1.2411 - mape: 798.7681
    Epoch 11/100
    900/900 [==============================] - 0s 195us/sample - loss: 1.1060 - mape: 750.3655
    Epoch 12/100
    900/900 [==============================] - 0s 271us/sample - loss: 1.0106 - mape: 696.5897
    Epoch 13/100
    900/900 [==============================] - 0s 234us/sample - loss: 0.9510 - mape: 650.1858
    Epoch 14/100
    900/900 [==============================] - 0s 183us/sample - loss: 0.8703 - mape: 652.8735
    Epoch 15/100
    900/900 [==============================] - 0s 227us/sample - loss: 0.8276 - mape: 627.7747
    Epoch 16/100
    900/900 [==============================] - 0s 205us/sample - loss: 0.7819 - mape: 591.9622
    Epoch 17/100
    900/900 [==============================] - 0s 196us/sample - loss: 0.7341 - mape: 603.2118 - loss: 0.7363 - mape: 605.45
    Epoch 18/100
    900/900 [==============================] - 0s 201us/sample - loss: 0.6928 - mape: 566.8422
    Epoch 19/100
    900/900 [==============================] - 0s 194us/sample - loss: 0.6713 - mape: 585.1208
    Epoch 20/100
    900/900 [==============================] - 0s 205us/sample - loss: 0.6247 - mape: 549.8159
    Epoch 21/100
    900/900 [==============================] - 0s 185us/sample - loss: 0.5897 - mape: 532.0855
    Epoch 22/100
    900/900 [==============================] - 0s 192us/sample - loss: 0.5658 - mape: 527.9331
    Epoch 23/100
    900/900 [==============================] - 0s 187us/sample - loss: 0.5446 - mape: 488.4539
    Epoch 24/100
    900/900 [==============================] - 0s 175us/sample - loss: 0.5169 - mape: 468.9429
    Epoch 25/100
    900/900 [==============================] - 0s 207us/sample - loss: 0.5201 - mape: 467.5760
    Epoch 26/100
    900/900 [==============================] - 0s 304us/sample - loss: 0.4877 - mape: 452.7673
    Epoch 27/100
    900/900 [==============================] - 0s 197us/sample - loss: 0.4782 - mape: 440.4234
    Epoch 28/100
    900/900 [==============================] - 0s 319us/sample - loss: 0.4719 - mape: 439.3805
    Epoch 29/100
    900/900 [==============================] - 0s 213us/sample - loss: 0.4486 - mape: 405.8721
    Epoch 30/100
    900/900 [==============================] - ETA: 0s - loss: 0.4335 - mape: 432.51 - 0s 327us/sample - loss: 0.4355 - mape: 420.7079
    Epoch 31/100
    900/900 [==============================] - 0s 202us/sample - loss: 0.4259 - mape: 408.2603
    Epoch 32/100
    900/900 [==============================] - 0s 212us/sample - loss: 0.4334 - mape: 410.5372
    Epoch 33/100
    900/900 [==============================] - 0s 213us/sample - loss: 0.4127 - mape: 385.4236
    Epoch 34/100
    900/900 [==============================] - 0s 269us/sample - loss: 0.3966 - mape: 402.8446
    Epoch 35/100
    900/900 [==============================] - 0s 199us/sample - loss: 0.3908 - mape: 410.7667
    Epoch 36/100
    900/900 [==============================] - 0s 204us/sample - loss: 0.3764 - mape: 368.6591
    Epoch 37/100
    900/900 [==============================] - 0s 191us/sample - loss: 0.3710 - mape: 371.9299
    Epoch 38/100
    900/900 [==============================] - 0s 279us/sample - loss: 0.3618 - mape: 362.1169
    Epoch 39/100
    900/900 [==============================] - 0s 229us/sample - loss: 0.3583 - mape: 383.2825
    Epoch 40/100
    900/900 [==============================] - 0s 194us/sample - loss: 0.3447 - mape: 360.1974
    Epoch 41/100
    900/900 [==============================] - 0s 186us/sample - loss: 0.3384 - mape: 354.0529
    Epoch 42/100
    900/900 [==============================] - 0s 205us/sample - loss: 0.3303 - mape: 353.8742
    Epoch 43/100
    900/900 [==============================] - 0s 246us/sample - loss: 0.3518 - mape: 376.3791
    Epoch 44/100
    900/900 [==============================] - 0s 311us/sample - loss: 0.3161 - mape: 353.5660
    Epoch 45/100
    900/900 [==============================] - 0s 239us/sample - loss: 0.3059 - mape: 345.9633
    Epoch 46/100
    900/900 [==============================] - 0s 219us/sample - loss: 0.3012 - mape: 335.1208
    Epoch 47/100
    900/900 [==============================] - 0s 257us/sample - loss: 0.2983 - mape: 340.1209
    Epoch 48/100
    900/900 [==============================] - 0s 195us/sample - loss: 0.2884 - mape: 318.1566
    Epoch 49/100
    900/900 [==============================] - 0s 235us/sample - loss: 0.2840 - mape: 331.5550
    Epoch 50/100
    900/900 [==============================] - 0s 194us/sample - loss: 0.2825 - mape: 344.0093
    Epoch 51/100
    900/900 [==============================] - 0s 239us/sample - loss: 0.2776 - mape: 320.8491
    Epoch 52/100
    900/900 [==============================] - 0s 225us/sample - loss: 0.2791 - mape: 312.0119
    Epoch 53/100
    900/900 [==============================] - 0s 209us/sample - loss: 0.2723 - mape: 325.9967
    Epoch 54/100
    900/900 [==============================] - 0s 283us/sample - loss: 0.2650 - mape: 299.1724
    Epoch 55/100
    900/900 [==============================] - 0s 249us/sample - loss: 0.2611 - mape: 287.6593
    Epoch 56/100
    900/900 [==============================] - 0s 260us/sample - loss: 0.2663 - mape: 304.5265
    Epoch 57/100
    900/900 [==============================] - 0s 224us/sample - loss: 0.2570 - mape: 288.9542 - loss: 0.2604 - mape: 365.
    Epoch 58/100
    900/900 [==============================] - 0s 239us/sample - loss: 0.2542 - mape: 300.7240
    Epoch 59/100
    900/900 [==============================] - 0s 219us/sample - loss: 0.2481 - mape: 287.3276
    Epoch 60/100
    900/900 [==============================] - 0s 219us/sample - loss: 0.2510 - mape: 305.7006
    Epoch 61/100
    900/900 [==============================] - 0s 226us/sample - loss: 0.2462 - mape: 315.4265
    Epoch 62/100
    900/900 [==============================] - 0s 275us/sample - loss: 0.2400 - mape: 315.1567
    Epoch 63/100
    900/900 [==============================] - 0s 235us/sample - loss: 0.2456 - mape: 271.3877
    Epoch 64/100
    900/900 [==============================] - 0s 217us/sample - loss: 0.2428 - mape: 310.5604
    Epoch 65/100
    900/900 [==============================] - 0s 222us/sample - loss: 0.2348 - mape: 304.9576
    Epoch 66/100
    900/900 [==============================] - 0s 186us/sample - loss: 0.2274 - mape: 277.8430
    Epoch 67/100
    900/900 [==============================] - 0s 201us/sample - loss: 0.2266 - mape: 273.0795
    Epoch 68/100
    900/900 [==============================] - 0s 187us/sample - loss: 0.2298 - mape: 289.7397
    Epoch 69/100
    900/900 [==============================] - 0s 215us/sample - loss: 0.2250 - mape: 276.7233
    Epoch 70/100
    900/900 [==============================] - 0s 195us/sample - loss: 0.2203 - mape: 269.2948
    Epoch 71/100
    900/900 [==============================] - 0s 229us/sample - loss: 0.2177 - mape: 256.3293
    Epoch 72/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.2180 - mape: 263.3174
    Epoch 73/100
    900/900 [==============================] - 0s 173us/sample - loss: 0.2400 - mape: 295.4164
    Epoch 74/100
    900/900 [==============================] - 0s 177us/sample - loss: 0.2255 - mape: 279.1955
    Epoch 75/100
    900/900 [==============================] - 0s 191us/sample - loss: 0.2176 - mape: 274.6373
    Epoch 76/100
    900/900 [==============================] - 0s 172us/sample - loss: 0.2157 - mape: 292.2338
    Epoch 77/100
    900/900 [==============================] - 0s 171us/sample - loss: 0.2119 - mape: 264.9478
    Epoch 78/100
    900/900 [==============================] - 0s 176us/sample - loss: 0.2127 - mape: 269.1001
    Epoch 79/100
    900/900 [==============================] - 0s 195us/sample - loss: 0.2127 - mape: 281.3460
    Epoch 80/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.2055 - mape: 276.5002
    Epoch 81/100
    900/900 [==============================] - 0s 238us/sample - loss: 0.2062 - mape: 279.3807
    Epoch 82/100
    900/900 [==============================] - 0s 166us/sample - loss: 0.2129 - mape: 290.9121
    Epoch 83/100
    900/900 [==============================] - 0s 162us/sample - loss: 0.2110 - mape: 290.7337
    Epoch 84/100
    900/900 [==============================] - 0s 176us/sample - loss: 0.2045 - mape: 272.8240
    Epoch 85/100
    900/900 [==============================] - 0s 175us/sample - loss: 0.2034 - mape: 296.3637
    Epoch 86/100
    900/900 [==============================] - 0s 176us/sample - loss: 0.1994 - mape: 261.6485
    Epoch 87/100
    900/900 [==============================] - 0s 180us/sample - loss: 0.1970 - mape: 289.5106
    Epoch 88/100
    900/900 [==============================] - 0s 182us/sample - loss: 0.1983 - mape: 287.4521
    Epoch 89/100
    900/900 [==============================] - 0s 178us/sample - loss: 0.2053 - mape: 290.1107
    Epoch 90/100
    900/900 [==============================] - 0s 174us/sample - loss: 0.1981 - mape: 307.7013
    Epoch 91/100
    900/900 [==============================] - 0s 232us/sample - loss: 0.2053 - mape: 299.0280
    Epoch 92/100
    900/900 [==============================] - 0s 171us/sample - loss: 0.2096 - mape: 316.3831
    Epoch 93/100
    900/900 [==============================] - 0s 182us/sample - loss: 0.2034 - mape: 303.2004
    Epoch 94/100
    900/900 [==============================] - 0s 188us/sample - loss: 0.2196 - mape: 262.1139
    Epoch 95/100
    900/900 [==============================] - 0s 193us/sample - loss: 0.1936 - mape: 325.1552
    Epoch 96/100
    900/900 [==============================] - 0s 197us/sample - loss: 0.1936 - mape: 303.9213
    Epoch 97/100
    900/900 [==============================] - 0s 181us/sample - loss: 0.1921 - mape: 288.6425
    Epoch 98/100
    900/900 [==============================] - 0s 168us/sample - loss: 0.1920 - mape: 313.0998
    Epoch 99/100
    900/900 [==============================] - 0s 173us/sample - loss: 0.1954 - mape: 305.5129
    Epoch 100/100
    900/900 [==============================] - 0s 172us/sample - loss: 0.1889 - mape: 314.0016
    

    对比评估

    #模型评估
    pca_model.evaluate(pca_test_data,test_label)
    ica_model.evaluate(ica_test_data,test_label)
    tsne_model.evaluate(tsne_test_data,test_label)
    
    36/1 [========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 0s 8ms/sample - loss: 0.6366 - mape: 146.9983
    36/1 [========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 0s 4ms/sample - loss: 0.6447 - mape: 155.4730
    36/1 [========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 0s 4ms/sample - loss: 0.2164 - mape: 292.4103
    
    
    
    
    
    [0.3441612786716885, 292.4103]
    
    plt.figure(figsize=(10,5))
    pca_history.history.keys() #查看history中存储了哪些参数
    plt.subplot(1,3,1)
    plt.title("PCA")
    plt.plot(pca_history.epoch,pca_history.history.get('loss')) #画出随着epoch增大loss的变化图
    ica_history.history.keys() #查看history中存储了哪些参数
    plt.subplot(1,3,2)
    plt.title("ICA")
    plt.plot(ica_history.epoch,ica_history.history.get('loss')) #画出随着epoch增大loss的变化图
    plt.subplot(1,3,3)
    plt.title("TSNE")
    plt.plot(tsne_history.epoch,tsne_history.history.get('loss')) #画出随着epoch增大loss的变化图
    plt.show()
    

    在这里插入图片描述

    展开全文
  • sklearn中的降维PCATSNE

    千次阅读 2018-06-22 10:24:25
    同为降维工具,二者的主要区别在于,所在的包不同(也即机制原理不同) from sklearn.decomposition import PCAfrom sklearn.manifold import TSNE因为原理不同,导致,tsne 保留下的属性信息,更具代表性,也即...

    同为降维工具,二者的主要区别在于,

    • 所在的包不同(也即机制和原理不同) 
      • from sklearn.decomposition import PCA
      • from sklearn.manifold import TSNE
    • 因为原理不同,导致,tsne 保留下的属性信息,更具代表性,也即最能体现样本间的差异;
    • TSNE 运行极慢,PCA 则相对较快;

    因此更为一般的处理,尤其在展示(可视化)高维数据时,常常先用 PCA 进行降维,再使用 tsne:

    data_pca = PCA(n_components=50).fit_transform(data)
    data_pca_tsne = TSNE(n_components=2).fit_transform(data_pca)
    
    转载https://blog.csdn.net/lanchunhui/article/details/64923702

    python--sklearn,聚类结果可视化工具TSNE

    详见

    https://www.cnblogs.com/cygalaxy/p/6934197.html

    展开全文
  • #!user/bin/env python # -- coding:utf-8 - import numpy as np from sklearn.datasets import load_...from sklearn.decomposition import PCA from sklearn.decomposition import FactorAnalysis from...

    PCA、TSNE、LDA、FA是非常常用的几个降维方法,通过一个简单的可视化对比来比较几个降维方法的好坏~

    
    #!user/bin/env python
    # -*- coding:utf-8 -*-
    
    import numpy as np
    from sklearn.datasets import load_digits
    from sklearn.decomposition import PCA
    from sklearn.decomposition import FactorAnalysis
    from sklearn.manifold import TSNE
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
    from matplotlib import pyplot as plt
    
    
    
    dx = load_digits()
    xx = dx.data
    yy = dx.target
    
    pca  = PCA(n_components=2)
    xxp = pca.fit_transform(xx)
    
    tsne = TSNE(n_components=2,init='pca')
    xxt = tsne.fit_transform(xx)
    
    lda = LinearDiscriminantAnalysis(n_components=2)
    xxl = lda.fit_transform(xx,yy)
    
    fa = FactorAnalysis(n_components=2)
    xxf = fa.fit_transform(xx)
    
    """pca、tsne、fa都是典型的无监督降维方法,lda是监督降维方法"""
    def bzh(data):
        x_max = np.max(data,axis=0)
        x_min = np.min(data,axis=0)
        xinx = (data-x_min)/(x_max-x_min)
        return xinx
    
    
    xxp = bzh(xxp)
    xxt = bzh(xxt)
    xxl = bzh(xxl)
    xxf = bzh(xxf)
    
    
    figure = plt.figure()
    ax1 = figure.add_subplot(2, 2, 1)
    ax2 = figure.add_subplot(2, 2, 2)
    ax3 = figure.add_subplot(2, 2, 3)
    ax4 = figure.add_subplot(2, 2, 4)
    for i in range(400):
        ax1.text(xxp[i,0],xxp[i,1],str(yy[i]),color=plt.cm.Set1(yy[i]/10.),
                fontdict={'weight': 'bold', 'size': 9})
        ax2.text(xxt[i,0],xxt[i,1],str(yy[i]),color=plt.cm.Set1(yy[i]/10.),
                fontdict={'weight': 'bold', 'size': 9})
        ax3.text(xxl[i,0],xxl[i,1],str(yy[i]),color=plt.cm.Set1(yy[i]/10.),
                fontdict={'weight': 'bold', 'size': 9})
        ax4.text(xxf[i, 0], xxf[i, 1], str(yy[i]), color=plt.cm.Set1(yy[i] / 10.),
                 fontdict={'weight': 'bold', 'size': 9})
    
    
    ax1.set_title("pca2d jw")
    ax2.set_title("tsne2d jw")
    ax3.set_title("lda2d jw")
    ax4.set_title("fa2d jw")
    figure.suptitle('jiangwei')
    plt.show()

    图示:
    (https://img-blog.csdn.net/20180729200327799?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyODI4NDA0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

    通过将8*8数据降维到2维数据,实现可视化。
    TSNE的降维效果还是非常赞的~

    展开全文
  • Pca,Kpca,TSNE降维非线性数据的效果展示与理论解释前言一:几类降维技术的介绍二:主要介绍Kpca的实现步骤三:实验结果四:总结 前言 本文主要介绍运用机器学习中常见的降维技术对数据提取主成分后并观察降维效果...
  • t-SNE vs PCA背景概述实战总结 由于原理较枯燥以及博主水平有限,故本文直接开始实战,需要补原理的读者还请谅解。 背景概述 假设你有一个包含数百个特征的数据集,却对该数据所属领域几乎没有什么了解,并且你需要...
  • 文章目录一、相似样本的降维聚类1、载入所需的包2、构建两个相似样本数据集3、绘制热图4、绘制PCA5、绘制TSNE二、差异样本的降维聚类1、构建第三个具有差异的数据集2、绘制热图3、绘制PCA4、绘制TSNE全部代码 ...
  • sklearn 特征降维利器 —— PCA TSNE
  • 说到这里,大家可能还是对PCA的作用感到有一些模糊,那么大家可以这么想,你有一堆细胞,但是你需要将这些细胞进行分群,但是你又不知道这些细胞如果进行分群的话是按照什么依据进行分群,所以你就需要先使用PCA降维...
  • 从SNE到t-SNE再到LargeVis http://bindog.github.io/blog/2016/06/04/from-sne-to-tsne-to-largevis/ t-SNE和PCA介绍 https://www.jianshu.com/p/8c20b975a174
  • titles = ['原始数据','PCA','因子分析','tsne','lda'] fa = FactorAnalysis(n_components=2) tsne = TSNE(n_components=2) from sklearn.discriminant_analysis import LinearDiscriminantAnalysis lda = ...
  • from sklearn.manifold import TSNE high_d = pd.read_csv('hd_coordinate.csv', header=None).to_numpy().tolist() low_d = TSNE(random_state=0).fit_transform(high_d) # 将X降到2维 pd.DataFrame(low_d).to_...
  • 线性降维算法我想最重要的就是PCA和LDA了,后面还会介绍一些非线性的方法。 LLE 局部线性嵌入 Locally linear embedding(LLE)[1] 是一种非线性降维算法,它能够使降维后的数据较好地保持原有 流形结构 。LLE...
  • 异常点检测 前文讲到tsne在高维度数据有着绝对...t-SNE和PCA降维后都有异常点,我们试着来分析分析,这些异常点如何检测以及通过这些点来判断降维的效果。 常见的检测异常点有4种异常点检测方法,One-Class SVM(一分类
  • 常见的降纬方法1.PCA降纬方法2.tSNE降纬分析3.UMAP降纬分析 1.PCA降纬方法 2.tSNE降纬分析 3.UMAP降纬分析
  • import numpy as np import pickle ...from sklearn.manifold import TSNE from sklearn.decomposition import PCA import PIL from PIL import Image from sklearn.neighbors import NearestNeighbors import.
  • sklearn中tsne可视化

    2020-12-21 23:31:33
    SNE embedding') tsne = TSNE(n_components=2, init='pca', random_state=0) t0 = time() result = tsne.fit_transform(data) fig = plot_embedding(result, label, 't-SNE embedding of the digits (time %.2fs)' %...
  • TSNE   资料链接: http://bindog.github.io/blog/2018/07/31/t-sne-tips/   GitHub代码: https://github.com/heucoder/dimensionality_reduction_alo_codes/tree/master/codes/T-SNE   AutoEncoder ...
  • 小老师让我在之前分析的情绪数据结果的基础之上,对原始数据进行分析,首先让我用PCA降维一下看看数据分布,然后换了TSNE降维。 真的是小白一枚呜呜呜~每次一个小任务我都要想好久,还会有很多bug 这次任务,我...
  • Matlab使用PCA降维

    千次阅读 2014-03-11 16:46:17
    原文地址:使用PCA(Principal Component Analysis)降维的语句">Matlab 使用PCA(Principal Component Analysis)降维的语句作者:abracadabra   在统计学中,主成分分析(principal components analysis (PCA)...
  • 并从精度上与PCA等其它降维算法进行比较分析,结果表明t-SNE算法更优越,本文最后给出了R、Python实现的示例以及常见问题。t-SNE算法用于自然语音处理、图像处理等领域很有研究前景。 更多深度文章,请关注:...
  • PCA、LDA、MDS、LLE、TSNE等降维算法的python实现
  • Pca_TSNE

    2020-02-20 11:05:27
    #-*-coding: utf-8-* from sklearn.datasets.samples_generator import make_...from sklearn.decomposition import PCA X, y = make_blobs(n_samples=10000, n_features=3, centers=[[3, 3, 3], [0, 0, 0], [1,...
  • 项目说明 针对高维向量进行K-means聚类,PCATSNE降维。
  • 以商圈为例,进行层次聚类,k-means聚类,T-SNE,PCA降维 ...层次聚类3.k-means聚类4.TSNE降维5.PCA降维5.1解释的比例5.2绘制图像呈现 1.读取数据 import pandas as pd df = pd.read_excel("business_circle.
  • 特征提取数据降维PCA、LDA、MDS、LLE、TSNE等降维算法的python实现

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,696
精华内容 678
关键字:

tsne和pca