英文:
Can not reshape 2 dimensional array to 3 dimensional array for RNN in python
问题
我无法将数组重塑为三维数组。以下是示例数据集:
import pandas as pd
import numpy as np
df = {
"a": [0.06, 0.07, 0.45, 0.98, 0.97],
"b": [12, 45, 65, 56, 34],
"c": [2, 5, 5, 5, 3],
"d": [23, 55, 25, 15, 34],
"e": [0.0005, 0.55555, 0.383825, 0.4747477415, 0.348344334],
"f": [0.0236, 0.3407, 0.4545, 0.9658, 0.4597],
"g": [70, 90, 123, 154, 99],
}
# 加载到数据帧(DataFrame):
df = pd.DataFrame(df)
print(df)
df.shape
X = df[['a', 'b', 'c', 'd', 'e', 'f']].to_numpy()
y = df['g'].to_numpy()
X
这是我从一个stackoverflow帖子中找到的可能解决方案:
# 将X数据重塑为3D
X = X.reshape(5, 7, -1)
但对我没有起作用。我理解矩阵有其规则,但是否可能将它转换为三维数组?如果你知道的话,能帮我吗?非常感谢!
我想要得到类似于(5,7,3)的数组,最后一维的大小是3。
英文:
I can not reshape the array into 3-dimensional one. Here is the example dataset:
import pandas as pd
import numpy as np
df = {
"a": [0.06 , 0.07, 0.45, 0.98, 0.97 ],
"b": [12,45,65, 56, 34],
"c": [2,5,5, 5, 3],
"d": [23,55,25, 15, 34],
"e": [0.0005,0.55555,0.383825, 0.4747477415, 0.348344334],
"f": [0.0236 , 0.3407, 0.4545, 0.9658, 0.4597 ],
"g": [70 , 90, 123, 154, 99 ],
}
#load into df:
df = pd.DataFrame(df)
print(df)
df.shape
X = df[['a', 'b', 'c','d','e','f']].to_numpy()
y = df['g'].to_numpy()
X
This is what I found as a possible solution from one of the stackoverflow posts
# Reshaping the X data to be 3D
X = X.reshape(5, 7, -1)
But it did not work for me. I understand that matrix has its rules, however, is it possible to convert it into three dimensional array? Could you help me if you know? Thank you very much!
I want to get something like (5,7,3). The array that end with the 3.
答案1
得分: 0
我认为你需要执行以下操作:
X = df[['a', 'b', 'c', 'd', 'e', 'f']].to_numpy()
y = df['g'].to_numpy()
X的形状是(5, 6)
df[['a', 'b', 'c', 'd', 'e', 'f']].to_numpy().reshape(5, 6, -1)
由于你已经将 'g'
移到了 y
中,所以可以这样重塑X:
X = X.reshape(5, 6, -1)
其他变体包括:
X = X.reshape(2, 5, 3)
X = X.reshape(5, 2, 3)
X = X.reshape(10, 1, 3)
X = X.reshape(1, 10, 3)
实际上,这些数字的乘积应该等于形状的乘积。因此,在这种情况下,(5, 6)
的乘积是 30
。
现在,你想要一个末尾为 3
的三维数组,所以我们需要从前两个维度获取 10
。
英文:
I think you have to do:
X = df[['a', 'b', 'c','d','e','f']].to_numpy()
y = df['g'].to_numpy()
X.shape is (5, 6)
df[['a', 'b', 'c','d','e','f']].to_numpy().reshape(5,6,-1)
Since you have moved 'g'
out to y
X = X.reshape(5, 6, -1)
Other variants are:
X=X.reshape(2, 5, 3)
X=X.reshape(5, 2, 3)
X=X.reshape(10, 1, 3)
X=X.reshape(1, 10, 3)
How we get them, actually the product of these numbers should be the product of shape. So, in this case product of (5,6)
is 30
.
Now, you want a 3D array with 3
at the end, so we need 10 from the first 2 dimensions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论