如何获得1维到3维的整形?

时间:2019-05-30 09:01:26

标签: python numpy keras lstm

这是我的代码


def create_dataset(signal_data, look_back=1):
    dataX, dataY = [], []
    for i in range(len(signal_data) - look_back):
        dataX.append(signal_data[i:(i + look_back), 0])
        dataY.append(signal_data[i + look_back, 0])
    return np.array(dataX), np.array(dataY)

look_back = 20
...

train_size = int(len(data) * 0.80)

test_size = len(data) - train_size

train = data[0:train_size]

test = data[train_size:len(data)]

x_train, y_train = create_dataset(train, look_back)

x_test, y_test = create_dataset(test, look_back)

然后x_train的形状为(62796,20),y_train的形状为(62796,)

我将这些数据用于LSTM

所以,重塑x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))已经完成

(现在x_train.shape是(62796,20,1))

但是y_train的形状是(62796,)所以,我无法重塑1D-> 3D

我如何y_train重塑1D-> 3D

我想要y_train形状为(62796,20,1),因为要使用LSTM return_sequences=True参数

1 个答案:

答案 0 :(得分:1)

这是您要找的吗?

<?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>abc</groupId>
        <artifactId>abc</artifactId>
        <version>1.2.0-SNAPSHOT</version>
    </parent>
    <artifactId>dfg</artifactId>
    <version>1.2.0-SNAPSHOT</version>
    <name>ghj</name>
    <packaging>pom</packaging>
    <description>Test</description>
    <properties>
        <tycho.version>1.3.0</tycho.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.reficio</groupId>
                <artifactId>p2-maven-plugin</artifactId>
                <version>1.3.0</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <phase>package</phase>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <id>x.y.z</id>
                                    <transitive>false</transitive>
                                    <instructions>
                                        <Bundle-Name>${pom.name}</Bundle-Name>
                                        <Bundle-Vendor>${pom.organization.name}</Bundle-Vendor>
                                    </instructions>
                                </artifact>
                                <artifact>
                                    <id>c.y.z:1.1.7</id>
                                    <transitive>false</transitive>
                                    <instructions>
                                        <Bundle-Name>${pom.name}</Bundle-Name>
                                        <Bundle-Vendor>${pom.organization.name}</Bundle-Vendor>
                                    </instructions>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-p2-repository-plugin</artifactId>
                <version>${tycho.version}</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>archive-repository</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Attach zipped P2 repository to be installed and deployed
                in the Maven repository during the deploy phase. -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>target/${project.artifactId}-${project.version}.zip</file>
                                    <type>zip</type>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-p2-repository-plugin</artifactId>
                <version>${tycho-version}</version>
                <configuration>
                    <compress>false</compress>
                    <includeAllDependencies>true</includeAllDependencies>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <pluginRepositories>
        <pluginRepository>
            <id>reficio</id>
            <url>http://repo.reficio.org/maven/</url>
        </pluginRepository>
    </pluginRepositories>
</project>

编辑:在评论中简短讨论之后的最终解决方案:

y_train = np.ones(100)
print(y_train.shape) #prints (100,)
y_train = y_train.reshape(-1,1,1)
print(y_train.shape) # prints (100,1,1)