low*_*owz 5 python numpy machine-learning computer-vision
我想将 MNIST 数据集从形状 (70000, 784) 重塑为 (70000, 28, 28),尝试了以下代码,但出现 TypeError:
类型错误:只有整数标量数组可以转换为标量索引
df = pd.read_csv('images.csv', sep=',', header=None)
x_data = np.array(df)
x_data = x_data.reshape(x_data[0], 28, 28)
Run Code Online (Sandbox Code Playgroud)
这有效,但速度很慢
data = np.array(df)
x_data = []
for d in data:
x_data.append(d.reshape(28,28))
x_data = np.array(x_data)
Run Code Online (Sandbox Code Playgroud)
numpy.reshape() 应该如何处理并且没有循环?曼尼谢谢!
小智 2
我认为,第二个的问题是因为你使用 for 循环可能需要更多时间。所以我建议你可以尝试这个
import tensorflow as tf
#load the data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', validation_size=0)
#considering only first 2 data points
img = mnist.train.images[:2]
x = tf.reshape(img, shape=[-1, 28, 28, 1]) # -1 refers to standard feature which is equivalent to 28*28*1 here
Run Code Online (Sandbox Code Playgroud)
理想情况下,我得到的 x 形状为 (2, 28, 28, 1)。希望这可以帮助!!
| 归档时间: |
|
| 查看次数: |
9532 次 |
| 最近记录: |