简体   繁体   English

如何将特征矩阵另存为csv文件

[英]how to save feature matrix as csv file

I have several features of Image and after image pre-processing I have plenty of data which I need to use frequently in future works. 我具有Image的几个功能,经过图像预处理后,我有很多数据需要在以后的工作中经常使用。 So to save time I want to save the data of image features in csv format. 因此,为了节省时间,我想以csv格式保存图像特征的数据。 The following image features are the row attributes: Intensity, Skewness, Kurtosis, Std_deviation Max5, Min5. 以下图像特征是行属性:强度,偏度,峰度,Std_deviation Max5,Min5。

Here every image feature is a numpy array of size (34560,1). 这里每个图像特征都是一个大小为(34560,1)的numpy数组。

How to make a csv file which consists of all these image features. 如何制作包含所有这些图像功能的csv文件。

You can use structured array if you want to include attribute name to numpy array. 如果要将属性名称包括在numpy数组中,则可以使用结构化数组。 But that will make things a bit more complicated to use. 但这会使使用起来有些复杂。 I would rather save the numpy array with same types and save the attributes name somewhere else. 我宁愿用相同的类型保存numpy数组,然后将属性名称保存在其他位置。 That is more straight forward and easier to use. 这更直接,更易于使用。

Example: For the sake of simplicity, let's say that you have three col arrays of length 4: a, b, c 示例:为简单起见,假设您有三个长度为4的col数组:a,b,c

a -> array([[1],
           [2],
           [3],
           [4]])

a.shape -> (4,1)

b and c array have same array shape. b和c数组具有相同的数组形状。

For the sake of faster access to the data, it would be better to make that as a row array so that it is stored continuously on the disk and memory when loaded. 为了更快地访问数据,最好将其设置为行数组,以便在加载时将其连续存储在磁盘和内存中。

a = a.ravel(); b = b.ravel(); c = c.ravel()
a - > array([1, 2, 3, 4])
a.shape -> (4,)

Then, you stack them into a large array and save it to csv file. 然后,将它们堆叠到一个大数组中,然后将其保存到csv文件中。

x = np.vstack((a,b,c))
array([[1, 2, 3, 4],
       [5, 6, 7, 8],
       [9, 10, 11, 12]])
x.shape -> (3,4)

Then, just save this stacked array to csv file. 然后,只需将此堆叠数组保存到csv文件中即可。

np.savetxt('out.csv', x, delimiter=',')

This can be done in one line: 可以一行完成:

np.savetxt('out.csv', np.vstack(a.ravel(), b.ravel(), c.ravel()), delimiter='x')

For example if yo got your out put into a variable "result" then you can save that result in csv format using the below commands 例如,如果您将结果放入变量“结果”中,则可以使用以下命令将该结果保存为csv格式

import pandas as pd
result = "......................"(your expression)
result1 = pd.DataFrame({'col1':result[:,0],'col2':result[:,1]})
result1.to_csv('Myresult.csv', header = False)

in the place of " Myresult " you can mention your desire path of output. 在“ Myresult ”的地方,您可以提及您期望的输出路径。 In my case it would be like " C:\\Users\\dinesh.n\\Desktop\\Myresult.csv ". 在我的情况下,它将类似于“ C:\\ Users \\ dinesh.n \\ Desktop \\ Myresult.csv ”。 I hope it clears your doubt if not please excuse me and correct me. 希望它能消除您的疑问,请原谅并纠正我。 Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM