我正在尝试使用SVM分类器对某些图像进行分类。我正在使用预训练的Inception V3模型提取图像的特征。我想将提取的功能保存到csv或excel文件中,然后为它们分配标签,以便它们可与SVM分类器一起使用。这些功能当前位于numpy数组中。我想将此numpy数组转储到excel或csv文件。当使用numpy的“ savetxt”功能时,会显示错误
期望一维或二维数组,而不是数组为%d维
这意味着由于尺寸过大,无法将要素阵列转储到csv文件中。有人可以告诉我一种将这种高维数组转储到excel或csv文件中的方法吗?
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import preprocess_input
from keras.applications.inception_v3 import decode_predictions
import os
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import scipy.io
from keras.preprocessing import image
import numpy as np
import pandas as pd
model = InceptionV3(weights='imagenet', include_top=False)
model.summary()
#for file in os.listdir('D:/MS/Literature for thesis/Mediaeval 2018/multimedia task/tools/twitter_downloader/evidence_0/'):
img_path='900095331450273792.png'
img = image.load_img(img_path, target_size=(229, 229))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
inception_feature = model.predict(img_data)
df = pd.DataFrame (inception_feature)
filepath = 'my_excel_file.xlsx'
df.to_excel(filepath, index=False)
答案 0 :(得分:0)
如果只想将高维数组保存在文件中,则最好将其保存为泡菜文件
import numpy as np
import pickle
m_array = np.arange(24).reshape(4,2,3)
# save as pickle file
with open('filename.pickle', 'wb') as handle:
pickle.dump(m_array , handle, protocol=pickle.HIGHEST_PROTOCOL)
# read pickle file
with open('filename.pickle', 'rb') as handle:
b = pickle.load(handle)
但是,如果要将其另存为CSV文件,请使用pandas库。
您可以找到更多详细信息here