我正在处理一个csv文件,该文件在一列中包含图像(矢量化)。这里是the csv file ~240MB。数组和字符串的长度在来回转换时不匹配
我想将图像字符串转换为一个整数列表,重塑成矩阵,翻转,并将其重新整形回列表,然后最终转换回长字符串。但事情并非如我所料。下面是我的代码:
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()'方法可能是一个更好的主意,不能获得所有的数组符号和换行符。 – Dschoni