精华内容
下载资源
问答
  • lstm分类预测
    千次阅读
    2021-11-03 17:28:02

    【人工智能项目】LSTM实现数据预测分类实验

    本次主要对csv文件中采集到的数据来区分树的品种实验,通过不同列的数据,送入lstm模型中,得到预测结果。
    在这里插入图片描述

    导包

    # 导包
    import numpy as np
    import pandas as pd
    import glob
    import os
    import tensorflow as tf
    from sklearn.model_selection import train_test_split
    from sklearn.utils import shuffle
    import matplotlib.pyplot as plt
    import seaborn as sns
    %matplotlib inline
    
    train_path = "./"
    
    print(os.listdir(train_path))
    
    ['.ipynb_checkpoints', 'code.ipynb', 'data.csv', 'lstm.h5', 'plant_totaldatat.xlsx']
    

    读取数据

    # 读取文件
    data = pd.read_csv("data.csv")
    data
    
    Filename172.538173.141173.744174.348174.951175.554176.157176.76177.363...1165.8461166.3731166.91167.4271167.9541168.4811169.0081169.5351170.061Label
    0芭蕉0001.ROH414.421417445.234558482.571625378.288757483.976776476.850617423.253845445.033813477.653564...487.088196513.986938532.956604545.502625504.853424568.687744547.811096584.947449564.7733761
    1芭蕉0002.ROH469.523712450.353333447.543030457.880981467.616699456.375458483.575287447.543030415.224365...560.357178511.477722473.337708613.151001513.384766495.418793618.771606618.570923495.6195071
    2芭蕉0003.ROH508.265930502.946411522.317505471.932556512.682129503.950104498.429840487.891144465.910461...597.694275552.126953540.885681661.327881553.030273540.183106650.889526659.521240550.2199711
    3芭蕉0004.ROH490.801819514.789917529.945557463.501617527.536682525.027466489.898499514.288025503.247528...567.784424576.416138573.906921625.596680548.915161621.280823632.221008652.595825599.1997681
    4芭蕉0005.ROH431.383697433.290680436.703217408.901154459.386505461.694977453.264008435.900269438.810974...535.867249499.232788503.849731569.691467518.704285512.381043577.921692573.605835520.8120121
    ..................................................................
    2422樟树10096.ROH376.682861396.656189391.637787371.363342434.796234390.634094378.489502410.406677394.147003...478.155395441.520904413.317383508.265930447.844116424.558624497.927978522.618652449.9518747
    2423樟树10097.ROH312.647797359.419495336.836578315.056641381.299835351.891876333.925903377.586182353.397400...434.495117420.242798342.758331476.047668397.258423369.054871446.639709460.390167384.9130867
    2424樟树10098.ROH383.809052372.166290419.941681371.363342412.112946411.912201399.566894382.905731405.287903...438.208740460.089081427.469330478.556885463.300873468.620392485.181183525.328613500.0357367
    2425樟树10099.ROH327.100861333.725159347.676392332.621124376.181030364.538300361.727966377.786926347.274902...417.934326411.410370377.184723433.190338413.919586395.752899432.989593445.636017425.5623177
    2426樟树10100.ROH380.697601424.859741441.119446388.526367448.446320433.089966416.428803431.383697450.654449...500.838684497.526520458.182068537.673889493.210663465.709717551.625122561.059753480.2631537

    2427 rows × 1757 columns

    # 查看前5行数据
    data.head()
    
    Filename172.538173.141173.744174.348174.951175.554176.157176.76177.363...1165.8461166.3731166.91167.4271167.9541168.4811169.0081169.5351170.061Label
    0芭蕉0001.ROH414.421417445.234558482.571625378.288757483.976776476.850617423.253845445.033813477.653564...487.088196513.986938532.956604545.502625504.853424568.687744547.811096584.947449564.7733761
    1芭蕉0002.ROH469.523712450.353333447.543030457.880981467.616699456.375458483.575287447.543030415.224365...560.357178511.477722473.337708613.151001513.384766495.418793618.771606618.570923495.6195071
    2芭蕉0003.ROH508.265930502.946411522.317505471.932556512.682129503.950104498.429840487.891144465.910461...597.694275552.126953540.885681661.327881553.030273540.183106650.889526659.521240550.2199711
    3芭蕉0004.ROH490.801819514.789917529.945557463.501617527.536682525.027466489.898499514.288025503.247528...567.784424576.416138573.906921625.596680548.915161621.280823632.221008652.595825599.1997681
    4芭蕉0005.ROH431.383697433.290680436.703217408.901154459.386505461.694977453.264008435.900269438.810974...535.867249499.232788503.849731569.691467518.704285512.381043577.921692573.605835520.8120121

    5 rows × 1757 columns

    数据分析

    data.index
    
    RangeIndex(start=0, stop=2427, step=1)
    
    print(data.info())
    
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 2427 entries, 0 to 2426
    Columns: 1757 entries, Filename to Label
    dtypes: float64(1755), int64(1), object(1)
    memory usage: 32.5+ MB
    None
    
    # 去除缺失数据
    data.dropna(axis=0, how='any', inplace=True)
    
    data
    
    Filename172.538173.141173.744174.348174.951175.554176.157176.76177.363...1165.8461166.3731166.91167.4271167.9541168.4811169.0081169.5351170.061Label
    0芭蕉0001.ROH414.421417445.234558482.571625378.288757483.976776476.850617423.253845445.033813477.653564...487.088196513.986938532.956604545.502625504.853424568.687744547.811096584.947449564.7733761
    1芭蕉0002.ROH469.523712450.353333447.543030457.880981467.616699456.375458483.575287447.543030415.224365...560.357178511.477722473.337708613.151001513.384766495.418793618.771606618.570923495.6195071
    2芭蕉0003.ROH508.265930502.946411522.317505471.932556512.682129503.950104498.429840487.891144465.910461...597.694275552.126953540.885681661.327881553.030273540.183106650.889526659.521240550.2199711
    3芭蕉0004.ROH490.801819514.789917529.945557463.501617527.536682525.027466489.898499514.288025503.247528...567.784424576.416138573.906921625.596680548.915161621.280823632.221008652.595825599.1997681
    4芭蕉0005.ROH431.383697433.290680436.703217408.901154459.386505461.694977453.264008435.900269438.810974...535.867249499.232788503.849731569.691467518.704285512.381043577.921692573.605835520.8120121
    ..................................................................
    2422樟树10096.ROH376.682861396.656189391.637787371.363342434.796234390.634094378.489502410.406677394.147003...478.155395441.520904413.317383508.265930447.844116424.558624497.927978522.618652449.9518747
    2423樟树10097.ROH312.647797359.419495336.836578315.056641381.299835351.891876333.925903377.586182353.397400...434.495117420.242798342.758331476.047668397.258423369.054871446.639709460.390167384.9130867
    2424樟树10098.ROH383.809052372.166290419.941681371.363342412.112946411.912201399.566894382.905731405.287903...438.208740460.089081427.469330478.556885463.300873468.620392485.181183525.328613500.0357367
    2425樟树10099.ROH327.100861333.725159347.676392332.621124376.181030364.538300361.727966377.786926347.274902...417.934326411.410370377.184723433.190338413.919586395.752899432.989593445.636017425.5623177
    2426樟树10100.ROH380.697601424.859741441.119446388.526367448.446320433.089966416.428803431.383697450.654449...500.838684497.526520458.182068537.673889493.210663465.709717551.625122561.059753480.2631537

    2427 rows × 1757 columns

    # 删除第一列数据
    data = data.drop(['Filename'], axis=1)
    
    data
    
    172.538173.141173.744174.348174.951175.554176.157176.76177.363177.966...1165.8461166.3731166.91167.4271167.9541168.4811169.0081169.5351170.061Label
    0414.421417445.234558482.571625378.288757483.976776476.850617423.253845445.033813477.653564595.285400...487.088196513.986938532.956604545.502625504.853424568.687744547.811096584.947449564.7733761
    1469.523712450.353333447.543030457.880981467.616699456.375458483.575287447.543030415.224365601.006409...560.357178511.477722473.337708613.151001513.384766495.418793618.771606618.570923495.6195071
    2508.265930502.946411522.317505471.932556512.682129503.950104498.429840487.891144465.910461655.907959...597.694275552.126953540.885681661.327881553.030273540.183106650.889526659.521240550.2199711
    3490.801819514.789917529.945557463.501617527.536682525.027466489.898499514.288025503.247528661.628967...567.784424576.416138573.906921625.596680548.915161621.280823632.221008652.595825599.1997681
    4431.383697433.290680436.703217408.901154459.386505461.694977453.264008435.900269438.810974592.675842...535.867249499.232788503.849731569.691467518.704285512.381043577.921692573.605835520.8120121
    ..................................................................
    2422376.682861396.656189391.637787371.363342434.796234390.634094378.489502410.406677394.147003510.775147...478.155395441.520904413.317383508.265930447.844116424.558624497.927978522.618652449.9518747
    2423312.647797359.419495336.836578315.056641381.299835351.891876333.925903377.586182353.397400424.960113...434.495117420.242798342.758331476.047668397.258423369.054871446.639709460.390167384.9130867
    2424383.809052372.166290419.941681371.363342412.112946411.912201399.566894382.905731405.287903531.149963...438.208740460.089081427.469330478.556885463.300873468.620392485.181183525.328613500.0357367
    2425327.100861333.725159347.676392332.621124376.181030364.538300361.727966377.786926347.274902443.227173...417.934326411.410370377.184723433.190338413.919586395.752899432.989593445.636017425.5623177
    2426380.697601424.859741441.119446388.526367448.446320433.089966416.428803431.383697450.654449533.458435...500.838684497.526520458.182068537.673889493.210663465.709717551.625122561.059753480.2631537

    2427 rows × 1756 columns

    # 样本分布
    # 以图方式表示
    sns.countplot(data["Label"])
    plt.xlabel("Label")
    plt.title("Number of  messages")
    
    Text(0.5, 1.0, 'Number of  messages')
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FIhX7uBd-1635931670390)(output_11_1.png)]

    # 重新排序
    df = data.sample(frac=1).reset_index(drop=True)
    
    df
    
    172.538173.141173.744174.348174.951175.554176.157176.76177.363177.966...1165.8461166.3731166.91167.4271167.9541168.4811169.0081169.5351170.061Label
    0429.978546448.345978447.342285430.380005473.839569442.323853457.178406452.862549429.175598582.739380...494.415100501.942718467.616699526.934509482.170136509.470367526.432617595.385803530.2466436
    1281.834656293.979248335.431427288.057526310.238953317.365112305.822723321.179108327.502319398.462830...329.911163375.578827351.691132373.270355349.984863390.132263369.255615398.061371381.1994637
    2440.316498426.164520453.665497430.480377450.052216461.594605456.174713440.517212444.130493607.429993...515.291748490.400360489.998871569.992554481.567932517.198731559.353516549.617737548.6140753
    3285.247192309.737091289.362305302.008728340.750977327.000488323.688324345.769379316.963623418.034698...338.442474373.169983362.530914384.511627340.750977376.983978375.578827407.997833391.2362981
    4458.081696475.345093447.743744437.606537476.950989457.278748469.523712437.305420436.803589594.683228...486.887451487.991516502.544952553.532104483.374573506.158203554.033997552.327698540.7853394
    ..................................................................
    2422441.822022464.706024461.996063427.368958481.266815490.299988465.207855473.939911446.037476610.240295...523.421570524.927124519.406860577.720947499.232788537.473144581.434570584.646362551.6251222
    2423290.767456264.270203298.395477291.670776303.915741331.416687325.394592316.762909328.204895404.384583...352.795166357.010651372.969238393.544769368.553040391.236298399.767639412.915894403.1801454
    2424303.915741286.652344305.722382293.176300329.810791303.815369317.967316328.907471294.179993419.239105...344.866058354.802551357.512512400.972046342.557587383.006103390.634094388.024506372.6681521
    2425428.473022434.394745487.991516408.098206464.003449489.396667456.174713431.684814458.583557590.066223...528.038513561.059753473.237335578.624268537.673889496.221741553.732849616.162048509.1692502
    2426454.468445441.621277450.754822424.357910459.286133460.289825469.222595459.787964443.729004592.274353...494.916931484.779724499.533875555.840576474.441772526.231873546.707092568.988892528.2392584

    2427 rows × 1756 columns

    # 空值检查
    df[df.isnull().values==True]
    
    172.538173.141173.744174.348174.951175.554176.157176.76177.363177.966...1165.8461166.3731166.91167.4271167.9541168.4811169.0081169.5351170.061Label

    0 rows × 1756 columns

    # 得到x和y
    x = df.iloc[:,:-1]
    y = df.iloc[:,-1]
    

    划分数据集

    # 划分数据集
    from sklearn.model_selection import train_test_split
    x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2)
    
    # 数据归一化处理
    from sklearn.preprocessing import StandardScaler
    
    scaler = StandardScaler()
    scaler.fit(x_train)
    
    X_train = scaler.transform(x_train)
    X_test = scaler.transform(x_test)
    
    print(X_train.shape)
    print(y_train.shape)
    print(X_test.shape)
    print(y_test.shape)
    
    (1941, 1755)
    (1941,)
    (486, 1755)
    (486,)
    
    # 对数据处理
    from keras.utils import np_utils
    
    X_train = X_train.reshape((-1,1,1755))
    Y_train = np_utils.to_categorical(y_train)
    X_test = X_test.reshape((-1,1,1755))
    Y_test = np_utils.to_categorical(y_test)
    
    print(X_train.shape)
    print(Y_train.shape)
    print(X_test.shape)
    print(Y_test.shape)
    
    (1941, 1, 1755)
    (1941, 8)
    (486, 1, 1755)
    (486, 8)
    

    模型

    from keras import Sequential
    from keras.layers import LSTM,Activation,Dense,Dropout,Input,Embedding,BatchNormalization,Add,concatenate,Flatten
    
    model = Sequential()
    
    model.add(LSTM(units=50,return_sequences=True,input_shape=(1,1755)))
    model.add(Dropout(0.2))
    
    model.add(LSTM(units=50,return_sequences=True))
    model.add(Dropout(0.2))
    
    model.add(LSTM(units=50,return_sequences=True))
    model.add(Dropout(0.2))
    
    # model.add(LSTM(units=50,return_sequences=True))
    # model.add(Dropout(0.2))
    
    model.add(LSTM(units=50))
    model.add(Dropout(0.2))
    
    # model.add(Dense(units=256))
    # model.add(Dropout(0.2))
    model.add(Dense(units=128))
    model.add(Dropout(0.2))
    model.add(Dense(units=64))
    model.add(Dropout(0.2))
    model.add(Dense(units=16))
    model.add(Dropout(0.2))
    
    model.add(Dense(units=8,activation="softmax"))
    
    # Implement Learning rate decay
    from keras.callbacks import EarlyStopping,ReduceLROnPlateau,ModelCheckpoint,LearningRateScheduler
    
    checkpoint = ModelCheckpoint("lstm.h5",
                                 monitor="val_loss",
                                 mode="min",
                                 save_best_only = True,
                                 verbose=1)
    
    earlystop = EarlyStopping(monitor = 'val_loss', 
                              min_delta = 0, 
                              patience = 5,
                              verbose = 1,
                              restore_best_weights = True)
    
    reduce_lr = ReduceLROnPlateau(monitor = 'val_loss',
                                  factor = 0.2,
                                  patience = 3,
                                  verbose = 1)
                                  #min_delta = 0.00001)
    
    callbacks = [earlystop, checkpoint, reduce_lr]
    
    model.compile(optimizer="adam", loss='categorical_crossentropy', metrics=['accuracy'])
    
    history_fit = model.fit(x=X_train, 
                            y=Y_train, 
                            batch_size=8, 
                            epochs=30, 
                            verbose=1, 
                            validation_data=(X_test, Y_test),
                            callbacks=callbacks)
    
    Train on 1941 samples, validate on 486 samples
    Epoch 1/30
    1941/1941 [==============================] - 6s 3ms/step - loss: 1.0300 - accuracy: 0.6188 - val_loss: 0.5473 - val_accuracy: 0.8313
    
    Epoch 00001: val_loss improved from inf to 0.54729, saving model to lstm.h5
    Epoch 2/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.6064 - accuracy: 0.7836 - val_loss: 0.3829 - val_accuracy: 0.8374
    
    Epoch 00002: val_loss improved from 0.54729 to 0.38287, saving model to lstm.h5
    Epoch 3/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.4797 - accuracy: 0.8089 - val_loss: 0.3595 - val_accuracy: 0.8272
    
    Epoch 00003: val_loss improved from 0.38287 to 0.35947, saving model to lstm.h5
    Epoch 4/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.4672 - accuracy: 0.8083 - val_loss: 0.2970 - val_accuracy: 0.8354
    
    Epoch 00004: val_loss improved from 0.35947 to 0.29702, saving model to lstm.h5
    Epoch 5/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3946 - accuracy: 0.8557 - val_loss: 0.2658 - val_accuracy: 0.9033
    
    Epoch 00005: val_loss improved from 0.29702 to 0.26579, saving model to lstm.h5
    Epoch 6/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3519 - accuracy: 0.8712 - val_loss: 0.2217 - val_accuracy: 0.8909
    
    Epoch 00006: val_loss improved from 0.26579 to 0.22171, saving model to lstm.h5
    Epoch 7/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3287 - accuracy: 0.8743 - val_loss: 0.2439 - val_accuracy: 0.8683
    
    Epoch 00007: val_loss did not improve from 0.22171
    Epoch 8/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3400 - accuracy: 0.8635 - val_loss: 0.2036 - val_accuracy: 0.9259
    
    Epoch 00008: val_loss improved from 0.22171 to 0.20360, saving model to lstm.h5
    Epoch 9/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3541 - accuracy: 0.8666 - val_loss: 0.2087 - val_accuracy: 0.9321
    
    Epoch 00009: val_loss did not improve from 0.20360
    Epoch 10/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3227 - accuracy: 0.8691 - val_loss: 0.2141 - val_accuracy: 0.9362
    
    Epoch 00010: val_loss did not improve from 0.20360
    Epoch 11/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2842 - accuracy: 0.8851 - val_loss: 0.1821 - val_accuracy: 0.9506
    
    Epoch 00011: val_loss improved from 0.20360 to 0.18205, saving model to lstm.h5
    Epoch 12/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3343 - accuracy: 0.8712 - val_loss: 0.2297 - val_accuracy: 0.8951
    
    Epoch 00012: val_loss did not improve from 0.18205
    Epoch 13/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3082 - accuracy: 0.8800 - val_loss: 0.2213 - val_accuracy: 0.9321
    
    Epoch 00013: val_loss did not improve from 0.18205
    Epoch 14/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2550 - accuracy: 0.9052 - val_loss: 0.1765 - val_accuracy: 0.9444
    
    Epoch 00014: val_loss improved from 0.18205 to 0.17651, saving model to lstm.h5
    Epoch 15/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.3290 - accuracy: 0.8856 - val_loss: 0.2044 - val_accuracy: 0.9383
    
    Epoch 00015: val_loss did not improve from 0.17651
    Epoch 16/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2812 - accuracy: 0.9031 - val_loss: 0.1578 - val_accuracy: 0.9465
    
    Epoch 00016: val_loss improved from 0.17651 to 0.15778, saving model to lstm.h5
    Epoch 17/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2332 - accuracy: 0.9145 - val_loss: 0.1287 - val_accuracy: 0.9547
    
    Epoch 00017: val_loss improved from 0.15778 to 0.12870, saving model to lstm.h5
    Epoch 18/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2597 - accuracy: 0.9114 - val_loss: 0.1607 - val_accuracy: 0.9280
    
    Epoch 00018: val_loss did not improve from 0.12870
    Epoch 19/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2570 - accuracy: 0.9052 - val_loss: 0.1230 - val_accuracy: 0.9671
    
    Epoch 00019: val_loss improved from 0.12870 to 0.12305, saving model to lstm.h5
    Epoch 20/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2401 - accuracy: 0.9129 - val_loss: 0.1639 - val_accuracy: 0.9588
    
    Epoch 00020: val_loss did not improve from 0.12305
    Epoch 21/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2233 - accuracy: 0.9155 - val_loss: 0.1172 - val_accuracy: 0.9671
    
    Epoch 00021: val_loss improved from 0.12305 to 0.11718, saving model to lstm.h5
    Epoch 22/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2524 - accuracy: 0.9088 - val_loss: 0.1627 - val_accuracy: 0.9588
    
    Epoch 00022: val_loss did not improve from 0.11718
    Epoch 23/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2185 - accuracy: 0.9176 - val_loss: 0.1313 - val_accuracy: 0.9342
    
    Epoch 00023: val_loss did not improve from 0.11718
    Epoch 24/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.2344 - accuracy: 0.9160 - val_loss: 0.1223 - val_accuracy: 0.9527
    
    Epoch 00024: val_loss did not improve from 0.11718
    
    Epoch 00024: ReduceLROnPlateau reducing learning rate to 0.00020000000949949026.
    Epoch 25/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.1890 - accuracy: 0.9274 - val_loss: 0.0862 - val_accuracy: 0.9691
    
    Epoch 00025: val_loss improved from 0.11718 to 0.08617, saving model to lstm.h5
    Epoch 26/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.1475 - accuracy: 0.9361 - val_loss: 0.0794 - val_accuracy: 0.9733
    
    Epoch 00026: val_loss improved from 0.08617 to 0.07940, saving model to lstm.h5
    Epoch 27/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.1507 - accuracy: 0.9392 - val_loss: 0.0673 - val_accuracy: 0.9774
    
    Epoch 00027: val_loss improved from 0.07940 to 0.06732, saving model to lstm.h5
    Epoch 28/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.1498 - accuracy: 0.9444 - val_loss: 0.0764 - val_accuracy: 0.9733
    
    Epoch 00028: val_loss did not improve from 0.06732
    Epoch 29/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.1513 - accuracy: 0.9423 - val_loss: 0.0733 - val_accuracy: 0.9774
    
    Epoch 00029: val_loss did not improve from 0.06732
    Epoch 30/30
    1941/1941 [==============================] - 4s 2ms/step - loss: 0.1338 - accuracy: 0.9418 - val_loss: 0.0815 - val_accuracy: 0.9753
    
    Epoch 00030: val_loss did not improve from 0.06732
    
    Epoch 00030: ReduceLROnPlateau reducing learning rate to 4.0000001899898055e-05.
    
    # 画曲线
    def plot_performance(history=None,figure_directory=None,ylim_pad=[0,0]):
        xlabel="Epoch"
        legends=["Training","Validation"]
        
        plt.figure(figsize=(20,5))
        
        y1=history.history["accuracy"]
        y2=history.history["val_accuracy"]
        
        min_y=min(min(y1),min(y2))-ylim_pad[0]
        max_y=max(max(y1),max(y2))+ylim_pad[0]
        
        plt.subplot(121)
        
        plt.plot(y1)
        plt.plot(y2)
        
        plt.title("Model Accuracy\n",fontsize=17)
        plt.xlabel(xlabel,fontsize=15)
        plt.ylabel("Accuracy",fontsize=15)
        plt.ylim(min_y,max_y)
        plt.legend(legends,loc="upper left")
        plt.grid()
        
        y1=history.history["loss"]
        y2=history.history["val_loss"]
        
        min_y=min(min(y1),min(y2))-ylim_pad[1]
        max_y=max(max(y1),max(y2))+ylim_pad[1]
        
        plt.subplot(122)
        
        plt.plot(y1)
        plt.plot(y2)
        
        plt.title("Model Loss:\n",fontsize=17)
        plt.xlabel(xlabel,fontsize=15)
        plt.ylabel("Loss",fontsize=15)
        plt.ylim(min_y,max_y)
        plt.legend(legends,loc="upper left")
        plt.grid()
        plt.show()
    
    # 可视化
    plot_performance(history=history_fit)
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4kudzXlk-1635931670393)(output_26_0.png)]

    # 预测
    predict_y = model.predict_classes(X_test)
    
    predict_y
    
    array([1, 6, 4, 3, 4, 1, 4, 6, 6, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 5, 4, 5,
           7, 1, 4, 5, 3, 4, 1, 6, 4, 4, 5, 4, 1, 1, 7, 4, 1, 4, 6, 4, 4, 5,
           4, 7, 7, 6, 1, 1, 5, 6, 2, 1, 4, 4, 1, 4, 4, 4, 6, 5, 2, 6, 3, 1,
           2, 4, 2, 4, 1, 1, 1, 1, 1, 1, 6, 4, 1, 3, 5, 2, 4, 6, 3, 4, 4, 3,
           6, 5, 7, 1, 1, 2, 7, 4, 1, 6, 6, 2, 6, 1, 3, 4, 1, 1, 1, 4, 2, 1,
           3, 6, 2, 4, 4, 4, 3, 4, 1, 1, 6, 7, 6, 7, 2, 5, 1, 3, 4, 1, 3, 3,
           5, 4, 4, 7, 6, 2, 6, 4, 6, 6, 3, 5, 3, 5, 6, 3, 4, 1, 3, 6, 1, 4,
           6, 4, 6, 2, 2, 1, 7, 4, 6, 3, 6, 6, 5, 4, 4, 4, 4, 2, 4, 6, 1, 3,
           1, 6, 6, 4, 1, 1, 4, 1, 4, 4, 2, 3, 1, 6, 4, 4, 3, 6, 5, 3, 4, 6,
           1, 1, 3, 5, 4, 1, 6, 3, 4, 3, 1, 2, 1, 4, 6, 5, 3, 5, 4, 4, 4, 4,
           7, 3, 1, 4, 2, 4, 6, 7, 4, 1, 4, 3, 1, 4, 1, 5, 2, 5, 3, 4, 1, 2,
           4, 5, 1, 4, 4, 6, 3, 1, 4, 4, 5, 5, 6, 4, 3, 3, 1, 4, 5, 1, 1, 2,
           3, 1, 1, 6, 7, 6, 4, 6, 1, 3, 4, 1, 4, 2, 7, 4, 5, 1, 4, 2, 1, 7,
           3, 6, 4, 4, 1, 7, 1, 5, 4, 4, 1, 4, 4, 1, 1, 4, 1, 1, 3, 6, 3, 3,
           6, 5, 4, 3, 1, 2, 6, 6, 6, 4, 2, 2, 3, 1, 5, 1, 4, 1, 7, 3, 1, 1,
           3, 5, 6, 2, 4, 1, 1, 6, 1, 6, 6, 6, 7, 1, 5, 4, 2, 7, 1, 6, 3, 1,
           4, 5, 2, 1, 4, 5, 6, 3, 1, 5, 1, 6, 3, 1, 3, 6, 6, 5, 1, 6, 4, 1,
           7, 3, 4, 3, 7, 3, 6, 1, 5, 3, 4, 2, 4, 5, 4, 1, 1, 4, 6, 3, 6, 5,
           4, 6, 1, 6, 3, 1, 4, 4, 3, 1, 5, 6, 6, 3, 5, 3, 5, 2, 1, 3, 2, 4,
           1, 4, 1, 3, 7, 6, 3, 4, 4, 1, 4, 2, 1, 4, 4, 2, 1, 3, 1, 3, 4, 7,
           4, 4, 1, 1, 1, 1, 4, 4, 1, 4, 5, 6, 5, 3, 3, 1, 4, 3, 2, 2, 6, 4,
           4, 3, 2, 2, 1, 6, 3, 1, 3, 1, 6, 7, 4, 4, 4, 1, 1, 4, 3, 1, 4, 5,
           4, 3], dtype=int64)
    
    from sklearn.metrics import accuracy_score,f1_score,confusion_matrix,classification_report
    
    print(classification_report(y_test,predict_y))
    
                  precision    recall  f1-score   support
    
               1       1.00      1.00      1.00       117
               2       0.97      1.00      0.99        36
               3       0.99      0.88      0.93        75
               4       0.92      0.99      0.95       117
               5       1.00      1.00      1.00        43
               6       1.00      0.99      0.99        73
               7       1.00      0.96      0.98        25
    
        accuracy                           0.98       486
       macro avg       0.98      0.97      0.98       486
    weighted avg       0.98      0.98      0.98       486
    

    小结

    瓷们,点赞评论收藏走起来呀!!!在这里插入图片描述

    更多相关内容
  • lstm官方源码,实现影评的积极消极分类,并实现训练和预测
  • LSTM-预测概率

    千次阅读 2020-08-19 10:56:42
    LSTM, Keras, Random walk model, 概率预测

    目录

    1.已知知识

    1.1LSTM

    1.2.随机行走模型

    2 问题描述

    3 代码

    3.1.数据准备

    3.2.结果


    1.已知知识

    1.1LSTM

    指长短期记忆人工神经网络。长短期记忆网络(LSTM,Long Short-Term Memory)是一种时间循环神经网络,是为了解决一般的RNN(循环神经网络)存在的长期依赖问题而专门设计出来的。

    RNN:Recurrent Neural Network 循环神经网络的计算过程如下:

    c^{N<t>}=tanh(W_c[c^{<t-1>},x^{<t>}]+b_c),

    \Gamma _u=\sigma (w_u[c^{<t>},x^{<T>}]+b_u),

    c^{<t>}=\Gamma _u*c^{N<t>}+{\color{Red} (1-\Gamma _u)}*c^{<t>},

    a^{<t>}=c^{<t>},

    在LSTM当中将{\color{Red} (1-\Gamma _u)}换成了\Gamma _f,而且,对a^{<t>}的更新也换成了\Gamma _o*tanh(c^{<t>})

    墙裂推荐吴恩达的课程,图片来自他讲LSTM,非常清楚,LSTM的计算过程如下:

    计算三个门的结果除了a^{<t-1>},x^{<t>}外还可以再加一个上次细胞状态c^{<t-1>} ,这个操作叫窥视连孔连接,peophole connection。

    1.2.随机行走模型

    用random walk model 得出概率的走向,下面这段代码可以玩一下。

    __author__ = 'Administrator'
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    fig=plt.figure()
    
    #time span
    T=500
    #drift factor飘移率
    mu=0.00005
    #volatility波动率
    sigma=0.04
    #t=0初试价
    S0=np.random.random()
    #length of steps
    dt=1
    N=round(T/dt)
    t=np.linspace(0,T,N)
    
    W=np.random.standard_normal(size=N)
    print("W ",W.shape)
    #W.shape=(500,)
    #几何布朗运动过程
    W=np.cumsum(W)*np.sqrt(dt)
    X=(mu-0.5*sigma**2)*t+sigma*W
    S=S0*np.exp(X)
    
    fd=pd.DataFrame({'pro':S})
    fd.to_csv('pic/random_walk.csv',sep=',',index=False)
    plt.plot(t,S,lw=2)
    plt.show()
    

    2 问题描述

    预测某个app在未来某个时间再次被打开的概率,其概率曲线用随机行走模型 random walk model 得出,数据大小都为0-1之间的小数,如果得出的图的取值不在【0,1】,多画几次~,因为每个动作都随机,很有可能会超过【0,1】这个范围。假设共500个分钟,先用70%的数据进行训练,打乱数据集后,再选取30%的数据进行测试,这样可以提高泛化能力。

    配置:用cpu跑的,相当的慢了,使用keras

    3 代码

    import pandas as pd
    import numpy as np
    import keras
    import matplotlib.pyplot as plt
    #from sklearn.preprocessing import MinMaxScaler
    from keras.models import Sequential
    from keras.layers import LSTM, Dense, Activation
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    test_num=500
    train_times=1000
    #random walk model to generate the probability tendency of task
    def random_walk_model():
        fig=plt.figure()
        #time span
        T=500
        #drift factor飘移率
        mu=0.00005
        #volatility波动率
        sigma=0.04
        #t=0初试价
        S0=np.random.random()
        #length of steps
        dt=1
        N=round(T/dt)
        #generate 500 steps and collect it into t
        t=np.linspace(0,T,N)
    
        #W is standard normal list
        W=np.random.standard_normal(size=N)
        print("W ",W)
        #W.shape=(500,)
        #几何布朗运动过程,产生概率轨迹
        W=np.cumsum(W)*np.sqrt(dt)
        X=(mu-0.5*sigma**2)*t+sigma*W
        S=S0*np.exp(X)
        plt.plot(t,S,lw=2)
        plt.show()
        #save the probability tendency of picture
        fd=pd.DataFrame({'pro':S})
        fd.to_csv('pic/random_walk_model.csv',sep=',',index=False)
        plt.savefig('pic/random_data.png')
        return S
    def random_test(sequence_length=5,split=0.7):
    
        #get the stored data by using pandas
        test_data = pd.read_csv('pic/random_walk_model.csv', sep=',',usecols=[0])
        #print("test_data:",test_data)
    
        #generate new test data for 2d
        test_data = np.array(test_data).astype('float64')
        #print('test_data:',test_data.shape)
        #test_data: (500, 1)
    
        #70% are used to be trained, the rest is used to be tested
        split_boundary = int(test_data.shape[0] * split)
        #print('split_boundary:',split_boundary)
        #split_boundary:350
    
        pro_test=np.linspace(split_boundary,test_data.shape[0],test_data.shape[0]-split_boundary)
        pro_x=np.linspace(1,split_boundary,split_boundary)
        plt.plot(pro_x,test_data[:split_boundary])
        plt.plot(pro_test,test_data[split_boundary:],'red')
        plt.legend(['train_data','test_data'])
        plt.xlabel('times')
        plt.ylabel('probability')
        plt.show()
        #print("test_data: ",test_data,test_data.shape),test_data.shape=(600,1),array to list format
    
        #generate 3d format of data and collect it
        data = []
    
        for i in range(len(test_data) - sequence_length - 1):
            data.append(test_data[i: i + sequence_length + 1])
        #print(len(data[0][0]),len(data[1]),len(data))
        #1 6 494
        reshaped_data = np.array(data).astype('float64')
        #print("reshaped_data:",reshaped_data.shape)
       #reshaped_data: (494, 6, 1)
    
        #random the order of test_data to improve the robustness
        np.random.shuffle(reshaped_data)
        #from n to n*5 are the training data collected in x, the n*6th is the true value collected in y
        x = reshaped_data[:, :-1]
        y = reshaped_data[:, -1]
    
        #print("x ",x.shape,"\ny ",y.shape)
        #x  (494, 5, 1) y  (494, 1)
    
        #train data
        train_x = x[: split_boundary]
        train_y = y[: split_boundary]
        #test data
        test_x = x[split_boundary:]
        test_y=y[split_boundary:]
        #print("train_y:",train_x.shape,"train_y:",train_y.shape,"test_x ",test_x.shape,"test_y",test_y.shape)
        #train_y: (350, 5, 1) train_y: (350, 1) test_x  (144, 5, 1) test_y (144, 1)
        return train_x, train_y, test_x, test_y
    
    def build_model():
        # input_dim是输入的train_x的最后一个维度,相当于输入的神经只有1个——特征只有1个,train_x的维度为(n_samples, time_steps, input_dim)
        #如果return_sequences=True:返回形如(samples,timesteps,output_dim)的3D张量否则,返回形如(samples,output_dim)的2D张量
        #unit并不是输出的维度,而是门结构(forget门、update门、output门)使用的隐藏单元个数
        model = Sequential()
        #use rmsprop for optimizer
        rmsprop=keras.optimizers.RMSprop(lr=0.001, rho=0.9,epsilon=1e-08,decay=0.0)
        #build one LSTM layer
        model.add(LSTM(input_dim=1, units=1, return_sequences=False,use_bias=True,activation='tanh'))
        #model.add(LSTM(100, return_sequences=False,use_bias=True,activation='tanh'))
    
        #comiple this model
        model.compile(loss='mse', optimizer=rmsprop)#rmsprop
        return model
    
    def train_model(train_x, train_y, test_x, test_y):
        #call function to build model
        model = build_model()
    
        try:
            #store this model to use its loss parameter
            history=model.fit(train_x, train_y, batch_size=20, epochs=train_times,verbose=2)
            #store the loss
            lossof_history=history.history['loss']
    
            predict = model.predict(test_x)
            predict = np.reshape(predict, (predict.size, ))
            #evaluate this model by returning a loss
            loss=model.evaluate(test_x,test_y)
            print("loss is ",loss)
        #if there is a KeyboardInterrupt error, do the following
        except KeyboardInterrupt:
            print("error of predict ",predict)
            print("error of test_y: ",test_y)
    
        try:
            #x1 is the xlabel to print the test value, there are 500 data,30% is for testing
            x1=np.linspace(1,test_y.shape[0],test_y.shape[0])
            #x1 is the xlabel to print the loss value, there are 500 data,70% is for training
            x2=np.linspace(1,train_times,train_times)
            fig = plt.figure(1)
            #print the predicted value and true value
            plt.title("test with rmsprop lr=0.01_")
            plt.plot(x1,predict,'ro-')
            plt.plot(x1,test_y,'go-')
            plt.legend(['predict', 'true'])
            plt.xlabel('times')
            plt.ylabel('propability')
            plt.savefig('pic/train_with_rmsprop_lr=0.01.png')
            #print the loss
            fig2=plt.figure(2)
            plt.title("loss lr=0.01")
            plt.plot(x2,lossof_history)
            plt.savefig('pic/train_with_rmsprop_lr=0.01_LOSS_.png')
            plt.show()
        #if the len(x1) is not equal to predict.shape[0] / test_y.shape[0] / len(x2) is not equal to lossof_history.shape[0],there will be an Exception
        except Exception as e:
            print("error: ",e)
    
    if __name__ == '__main__':
        #random_walk_model() function is only used by once, because data are stored as pic/random_data.csv
        #random_walk_model()
    
        #prepare the right data format for LSTM
        train_x, train_y, test_x, test_y=random_test()
        #standard the format for LSTM input
        test_x = np.reshape(test_x, (test_x.shape[0], test_x.shape[1], 1))
        #print("main: train_x.shape ",train_x.shape)
        #main: train_x.shape  (350, 5, 1)
        train_model(train_x, train_y, test_x, test_y)
    

    细节

    3.1.数据准备

    def random_test(sequence_length=6,split=0.7):

    在这个函数当中,将获取的数据处理为需要的三维的张量格式,在训练的时候

    history=model.fit(train_x, train_y, batch_size=20, epochs=train_times,verbose=2)

    train_x.shape=  (415, 6, 1)#600条数据,70%的训练数据,30%测试数据

    最后使用的训练数据需要是这样的三维张量。

    Question:600条的数据,70%用于训练,那么数据应该是420条,但是为什么是415?

    Answer:在预处理数据之后,建立一层的LSTM:

    model.add(LSTM(input_dim=1, units=50, return_sequences=False))

    后台显示input_dim warning,找了下原因,要使用input_shape作为参数,input_shape为3维,参数为(Batch_size, Time_step, Input_Sizes)。

    batch_size设置:batch_size为

    在model.add(LSTM(input_shape(,,,), units=, return_sequences=))语句中定义在该语句中不定义batch_size,input_shape=(5,),这只定义了time_step,或者input_shape=(None,5,5),第一个参数定义为None
    无法使用model.train_on_batch(),且在test时也需要有batch_size的数据可以调用train_on_batch()

    time_step为时间序列的长度/语句的最大长度

    input_sizes:每个时间点输入x的维度/语句的embedding的向量维度,本例题做概率预测,再次回忆LSTM的图,x(t)输入是一个概率值,那么input_sizes=1,即特征值只有一个

    那这与数据415条的关系在哪呢?

    注意默认的参数 sequence_length=6

    这是数据的一部分:

    '''
    0.7586717205211277
    0.6628550358816061
    0.9184003785782959
    0.09365662435769384
    0.9791582266747239
    0.8700739252039772
    0.7924134549615585
    0.3983410609045436
    0.38988445126231197
    0.8167186985712294
    0.879351951255656
    0.9468282424096985
    0.7060727836006101
    0.7650727081508003
    0.3633755461129521
    0.3489589275449808
    '''
    for i in range(len(test_data) - sequence_length - 1):
            data.append(test_data[i: i + sequence_length + 1])

    在上面的语句作用就是

    当i=0时,data.append(test[0:6]),一共循环594次,那么data的数据为

    [[[0.75867172]
      [0.66285504]
      [0.91840038]
      [0.09365662]
      [0.97915823]
      [0.87007393]]
    
     [[0.66285504]
      [0.91840038]
      [0.09365662]
      [0.97915823]
      [0.87007393]
      [0.79241345]]
    
     [[0.91840038]
      [0.09365662]
      [0.97915823]
      [0.87007393]
      [0.79241345]
      [0.39834106]]
    
     ...
    ]

    即 其步长为1,生成的data.shape=(594, 6, 1)= (seq_len, batch, input_size)

    其中第一个参数表示数据个数,

    第二个参数表示,观察dada的数据,步长为1(自己设定的),个数为6,进行划分,batch就是那个6

    第三个参数:回忆上面讲的input_shape的input_size——>每个时间点输入x的维度/语句的embedding的向量维度,本例题做概率预测,再次回忆LSTM的图,x(t)输入是一个概率值,那么input_sizes=1,即特征值只有一个

    3.2.结果

    用于测试的30%的数据预测结果与真实结果如下

    展开全文
  • 分类预测 | MATLAB实现CNN-LSTM(卷积长短期记忆神经网络)多特征分类预测

    分类预测 | MATLAB实现CNN-LSTM(卷积长短期记忆神经网络)多特征分类预测

    预测效果

    1
    2
    3
    4
    5

    基本介绍

    本次运行测试环境MATLAB2020b
    总体而言,CNN用作特征(融合)提取,然后将输出的feature映射为序列向量输入到LSTM当中。

    模型描述

    • 长短期记忆神经网络(LSTM)是一种特殊循环神经网络(RNN),在RNN的基础上引入了门控单元系统,采用输入门、遗忘门和输出门对信息进行选择性控制,适当遗忘历史信息并依据新信息更新细胞状态。
      1
      2
    • CNN模型对输入数据进行逐层卷积和池化操作,主要由输入层、卷积层、池化层、全连接层和输出层组成。
    • 与LSTM模型的构建类似,也需要将数据生成为堆叠数据类型,同时还在输入矩阵中添加了各变量的变动率作为额外的输入向量。
    • 在数据集选取方面,本模型数据集选取思路与LSTM模型基本保持一致。但由于变动率特征的存在,原始时间序列的第一个样本将缺少变动率的特征。考虑到异常数据在全体中所占比例较小,故选择直接将原始时间序列的第一个样本剔除,从而保证特征的完备性。
    • 考虑到CNN模型在预测系统中起到的作用主要是捕捉交叉特征,故引入膨胀卷积这一特殊的卷积模式,并使用残差学习和瓶颈方法来规避深化卷积神经网络带来的梯度消失问题,将原始特征直接作为全连接层的一部分特征加入到模型之中,以防止过拟合和退化问题。
    • 可以将货币价格图像与同一数据的价格时间序列数据同时训练,从而使两个模型互补。针对LSTM-CNN结构的构建方法, 提出了联合RMSE损失的LSTM-CNN模型。其构建过程为先分别建立CNN模型与LSTM模型,利用损失函数计算各模型的损失值,再按照一定比例联合两个模型的损失值使总RMSE损失尽可能小且预测精度较高。本文采取类似的构建方法,同样将CNN-LSTM模型的构建过程分三步进行。
    • 第一阶段采用CNN模型架构,采用与上文单一CNN模型相同的架构;第二步建立LSTM模型,同样采用与上文LSTM模型相同的架构;而在第三步,本文采取按串联合两个模型预测准确度的方法。
    • 利用既有数据的白噪检验P值作为CNN-LSTM模型预测的置信水平,并通过公式计算混合模型的预测数值。

    程序设计

    • 完整源码私信博主
    %% CNNLSTM训练选项
    % 批处理样本
    miniBatchSize =10;
    % 最大迭代次数
    MaxEpochs = 100;
    % 学习率
    learningrate = 0.005;
    % 一些参数调整
    if gpuDeviceCount>0
        mydevice = 'gpu';
    else
        mydevice = 'cpu';
    end
        options = trainingOptions( 'adam', ...
            'MaxEpochs',100, ...
            'MiniBatchSize',miniBatchSize,...
            'GradientThreshold',1, ...
            'InitialLearnRate',learningrate, ...
            'LearnRateSchedule','piecewise', ...
            'LearnRateDropPeriod',20, ...
            'LearnRateDropFactor',0.8, ...
            'L2Regularization',1e-3,...
            'Verbose',false, ...
            'ExecutionEnvironment',mydevice,...
            'Plots','training-progress');
    

    学习总结

    通过将两个模型进行加权得到CNN-LSTM多特征分类预测模型,其预测效果均优于两个单一模型,有效融合了LSTM提取时序特征的优点和CNN提取深层特征的优点。

    参考资料

    [1] 郭思涵. 基于改进循环神经网络的比特币价格预测及交易策略研究[D]: [硕士学位论文]. 上海: 上海师范大学, 2021.
    [2] 张宁, 方靖雯, 赵雨宣. 基于LSTM混合模型的比特币价格预测[J]. 计算机科学, 2021, 48(S2): 39-45.
    [3] 曹超凡, 罗泽南, 谢佳鑫, 李路. MDT-CNN-LSTM模型的股价预测研究[J]. 计算机工程与应用, 2022, 58(5): 280-286.
    [4] 杨丽, 吴雨茜, 王俊丽, 刘义理. 循环神经网络研究综述[J]. 计算机应用, 2018, 38(S2): 1-6+26.
    [5] 徐剑波. 基于数据挖掘技术的股票技术指标分析预测系统的设计与实现[D]: [硕士学位论文]. 成都: 电子科技大学, 2012.
    [6] 石赛男. 股票技术分析中MACD指标的有效性检验[D]: [硕士学位论文]. 成都: 西南财经大学, 2011.
    [7] 周飞燕, 金林鹏, 董军. 卷积神经网络研究综述[J]. 计算机学报, 2017, 40(6): 1229-1251.
    [8] 李彦冬, 郝宗波, 雷航. 卷积神经网络研究综述[J]. 计算机应用, 2016, 36(9): 2508-2515+2565.
    [9] 严春满, 王铖. 卷积神经网络模型发展及应用[J]. 计算机科学与探索, 2021, 15(1): 27-46.
    [10] 卢宏涛, 张秦川. 深度卷积神经网络在计算机视觉中的应用研究综述[J]. 数据采集与处理, 2016, 31(1): 1-17.
    [11] 方义秋, 卢壮, 葛君伟. 联合RMSE损失LSTM-CNN模型的股价预测[J]. 计算机工程与应用, 2022, 58(9): 294-302.

    展开全文
  • 分类预测 | MATLAB实现LSTM(长短期记忆神经网络)分类预测

    分类预测 | MATLAB实现LSTM(长短期记忆神经网络)多特征预测

    分类效果

    1
    2
    3
    4

    5

    基本介绍

    长短期记忆网络——通常被称为 LS

    展开全文
  • LSTM多变量预测

    2019-09-24 07:36:35
    LSTM多变量预测,本程序利用TensorFlow构建一个简易LSTM模型,内含对多个变量因子,以及本程序的运行环境。
  • LSTM预测分类模板代码

    2022-04-20 22:45:16
    LSTM预测分类模板代码
  • 爬取天气预报,使用xgb和lstm进行预测,并使用window定时任务设置每小时执行
  • LSTM文本预测分析python

    千次阅读 热门讨论 2022-03-21 10:40:22
    1.介绍LSTM原理 2.预测案例
  • 近年来受到了学者的广泛关注,它己被成功地应用到了车间调度、参数优化、图像分类等领域中。1.2 算法原理灰狼隶属于群居生活的犬科动物,且处于食物链的顶层。灰狼严格遵守着一个社会支配等级关系。...
  • 为了大家能够顺利以及最少的精力通过毕设,学长分享优质毕业设计项目,今天要分享的新项目是基于时间序列预测LSTM的天气预测系统学长这里给一个题目综合评分(每项满分5分)难度系数:4分工作量:4分。...
  • LSTM股票预测

    2022-05-01 23:33:05
    LSTM股票预测
  • 通过多种机器学习股票价格预测,包括随机森林(Random Forest),决策树(SVM),线性回归(LinearRegression),长短期记忆(LSTM)。 利用toshare获取600519.sh 2000-2020年数据,除了随机森林外基本都是以前19年数据做训练集...
  • ## 一、布谷鸟算法优化LSTM预测 **1 布谷鸟搜索算法** 布谷鸟算法是一种新型的群智能搜索算法,布谷鸟算法具有参数数目少、鲁棒性强、通用性好和全局寻优能力突出等多方面综合优势。布谷鸟算法以寻得全局最优鸟窝为...
  • 使用LSTM网络做预报(Forecast)在一次小小的比赛中需要做趋势预测,当时找了很多种方法,最后也对LSTM的使用做出一定的研究,现在大多数能找到的都是Predict,对于Forecast的做法虽然找到了原理,但由于各种原因自己...
  • 包括:1、svm和lstm用于文本分类 2、keras上LSTM长短期记忆网络金融时序预测python代码 3、LSTM数据集+python源码 4、PSO-LSTM 5、RNN-LSTM卷积神经网络Matlab实现 6、股票预测 LSTM 时间序列rnn 代码程序数据集 7、...
  • 分类预测 | MATLAB实现LSTM(长短期记忆神经网络)分类预测(完整源码和数据) 多特征输入多标签输出,运行环境MATLAB2018b及以上。
  • 一、LSTM和BiLSTM神经网络结构及原理介绍 长短期记忆网络(Long short-term memory, LSTM)实质上可以理解为一种特殊的RNN,主要是为了解决RNN网络在长序列训练过程中发生梯度消失和梯度爆炸的问题。相比于RNN,LSTM主要...
  • 看了好多序列预测的都是seq2seq的翻译模式的预测,根本没有预测未来的数据 import numpy as np import matplotlib.pyplot as plt step = 3 #步长 size = 6 #输入大小 forward = 3 #向后预测几步 def load_data...
  • LSTM是什么,依旧百度百科:长短期记忆网络(LSTM,Long Short-Term Memory)是一种时间循环神经网络,是为了解决一般的RNN(循环神经网络)存在的长期依赖问题而专门设计出来的,所有的RNN都具有一种重复神经网络...
  • 一种基于灰狼算法优化LSTM的网络流量预测方法,属于网络流量预测领域,该方法包括以下步骤:对第一网络流量数据集进行极差标准化处理,得到第二网络流量数据集,并划分为训练集和测试集,并确定灰狼算法优化LSTM神经网络中...
  • LSTM 是循环神经网络中的一个特殊网络,它能够很好的处理序列信息并从中学习有效特征,它把以往的神经单元用一个记忆单元( memory cell) 来代替,解决了以往循环神经网络在梯度反向传播中遇到的爆炸和衰减问题....
  • 基于麻雀算法优化LSTM回归预测(matlab) 概述: 麻雀算法构思 lstm原理 麻雀优化lstm原理 代码及结果展示 第一部分 麻雀算法构思 众所周知,麻雀是常见的留鸟而且非常喜欢群居。这种生物是非常聪明的,有很强...
  • 利用LSTM实现预测时间序列(股票预测

    万次阅读 多人点赞 2021-06-17 15:51:28
    目录1、作者介绍2、tushare 简介3、LSTM简介3.1 循环神经网络 (Recurrent Neural Networks)3.2 LSTM网络3.2.1 LSTM的核心思想3.2.2 一步一步理解LSTM4 代码实现4.1 导入相关资源包4.2 定义模型结构4.3 制作数据集4.4...
  • 回归预测 | MATLAB实现BiLSTM(双向长短期记忆神经网络)多输入单输出(完整源码和数据) 多输入单输出,运行环境MATLAB2018b及以上。
  • 使用LSTM网络做函数区分(二分类

    万次阅读 2018-08-07 16:13:16
    但是尝试了半个多月了,结果一直十分不理想,因为是初学者,难免对自己是否真的正确使用Tensorflow搭建LSTM网络,因此决定使用之前同样的LSTM网络来对数学函数进行预测分类,如果能正确分类,无疑可以消除一些对自己...
  • 使用pytorch搭建lstm时间序列预测

    千次阅读 2022-07-20 15:47:13
    我的疑问,time_step = 8,意味着每8个数据预测下一个数据,为什么预测后的图画出来和原始数据的形状一直呢?3,data[:,0] data[1,:]的含义。下图是归一化后的原数据和预测数据。下图是没有归一化过的原数据。....
  • CNN LSTM 带有Resnet后端的CNN LSTM用于视频分类的实现入门先决条件PyTorch(需要0.4以上版本) FFmpeg,FFprobe 的Python 3尝试自己的数据集mkdir datamkdir data/video_data将您的视频数据集放入data / video_data...
  • 最后,构建 Softmax 分类器对机场延误分类预测。所提RR-LSTM网络模型既能有效提取机场延误数据的时间相关性,又能避免深层LSTM网络的梯度消失问题。实验结果表明,RR-LSTM网络模型预测准确率可达95.52%,取得了比...
  • 主要介绍了在Keras中CNN联合LSTM进行分类实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 24,338
精华内容 9,735
关键字:

lstm分类预测