【发布时间】:2017-04-17 11:58:47
【问题描述】:
我正在处理一个 csv 文件,该文件在一列中包含图像(矢量化)。这里是the csv file ~240MB。
我正在尝试将 Image 字符串转换为整数列表,重塑为矩阵,翻转,并将其重塑回列表,然后最终转换回长字符串。但事情并没有如我所愿。以下是我的代码:
import pandas as pd
import numpy as np
df = pd.read_csv('training.csv')
img = df['Image'][0] # take the first row as example
img_int = np.fromstring(img, sep=' ') # img_int.shape --> (9216,), good.
img_matrix = img_int.reshape(96,96)
img_matrix_flipped = np.fliplr(img_matrix) # img_matrix_flipped.shape --> (96,96), good
img_matrix_flipped_vector = img_matrix_flipped.reshape(1, 9216) # img_matrix_flipped_vector.shape --> (1, 9216), good
img_matrix_flipped_vector_str = str(img_matrix_flipped_vector) # len(img_matrix_flipped_vector_str) --> 44, NOT GOOD!!!
我很困惑为什么 len(img_matrix_flipped_vector_str) 是 44。字符串不应该包含所有 9216 整数吗?请帮忙!
【问题讨论】:
-
据我所知,您的代码没有任何问题。最好在数组上使用
tostring()方法,而不是获取所有数组符号和换行符。
标签: python string pandas numpy multidimensional-array