python - 从一行上的数据创建一个 numpy 数组
问题描述
我的数据文件的每一行都有一个样本。每行是 400 个浮点数。这是单行上的 20x20 图像。我必须编写一个带有维度的 numpy 数组(行数,20、20、1)。最后一个维度是值(文件中的浮点数)。
我试过类似的东西:
X1=[]
for x in range (1,nrow+1):
for a in range (1,21):
for b in range (1,21):
index = a*b-1
X1.append((x,a,b,X[x,index]))
X = np.array(X1)
但我知道这是错误的。
编辑:
也许这是解决方案:
X1=[]
for x in range (1,Nsamples+1):
for a in range (1,21):
for b in range (1,21):
index = a*b-1
X1.append((X[x,index]))
#X1.append((X[x,index], x))
X = np.array(X1)
X = X.reshape(Nsamples,20,20,1)
解决方案
尝试使用重塑
X = X.reshape(X.shape[0],20,20,1)
它将为您提供具有相同行数的重塑图像的 numpy 数组
推荐阅读
- firebase - Firebase 身份验证:具有相同电子邮件地址的多个帐户/个人资料
- go - 符合 UTF-8 文件的字符索引
- android - 如何开发可与旧版 Google Play 服务用于 AR 的 ARCore 应用程序?
- python - Pytorch 内存分析器为空返回高 CPU 内存
- javascript - Vue staging build 创建一个开发版本而不是生产版本
- c++ - 无法在 CMake 中设置 c++ 版本
- c++ - 试图为wallhack usw写一个DLL
- excel - VBA - 后期绑定 Outlook.Mailitem 以支持不同的 Excel 版本
- c# - 您是否应该为 Web 应用程序/API 提供一个 HTTP 客户端?
- c# - 如何获得 TimePicker 的值?