使用索引在文本文件中保存numpy矩阵

时间:2016-12-27 22:44:08

标签: arrays python-2.7 numpy matrix save

我正在尝试将2D矩阵A保存到每个非零条目的文本文件中。我想以下面的格式保存它,

对于第(i,j)个条目,一行为

row[i]::column[j]::A[i,j]

其中,行和列是对应于行和列索引的1D numpy数组

我试过了,

np.savetxt(rel_file,(row,column,A),fmt='%d',delimiter="::",newline="\n");

但是,由于形状不匹配,我得到错误。我不想在循环中迭代每个行和列索引(我认为它太耗时,我有5000 * 5000矩阵)。

2 个答案:

答案 0 :(得分:0)

在我的第一个回答中,我显示了所有值;只有非零,它更容易创建列数组。 require(arrayhelpers);require(stringr);require(plyr);require(ncdf4) # store all files from ftp://rfdata:forceDATA@ftp.iiasa.ac.at/WFDEI/ in the following folder: setwd("C:/folder") temp = list.files(pattern="*.nc") #list all the file names param<-gsub("_\\S+","",temp,perl=T) #extract parameter from file name xcoord=seq(176,180,by=1) #The X-coordinates you are interested in ycoord=seq(428,433,by=1) #The Y-coordinates you are interested in list_var<-list() # make an empty list for (t in 1:length(temp)){ temp_year<-str_sub(temp[],-9,-6) #take string number last place minus 9 till last place minus 6 to extract the year from file name temp_month<-str_sub(temp[],-5,-4) #take string number last place minus 9 till last place minus 6 to extract the month from file name temp_netcdf<-nc_open(temp[t]) temp_day<-rep(seq(1:length(ncvar_get(temp_netcdf,"day"))),length(xcoord)*length(ycoord)) # make a string of day numbers the same length as amount of values dim.order<-sapply(temp_netcdf[["var"]][[param[t]]][["dim"]],function(x) x$name) # gives the name of each level of the array start <- c(lon = 428, lat = 176, tstep = 1) # indicates the starting value of each variable count <- c(lon = 6, lat = 5, tstep = length(ncvar_get(nc_open(temp[t]),"day"))) # indicates how many values of each variable have to be present starting from start tempstore<-ncvar_get(temp_netcdf, param[t], start = start[dim.order], count = count[dim.order]) # array with parameter values df_temp<-array2df (tempstore, levels = list(lon=ycoord, lat = xcoord, day = NA), label.x = "value") # convert array to dataframe Add_date<-sort(as.Date(paste(temp_year[t],"-",temp_month[t],"-",temp_day,sep=""),"%Y-%m-%d"),decreasing=FALSE) # make vector with the dates list_var[t]<-list(data.frame(Add_date,df_temp,parameter=param[t])) #add dates to data frame and store in a list of all output files nc_close(temp_netcdf) #close nc file to prevent data loss and prevent error when working with a lot of files } All_NetCDF_var_in1df<-do.call(rbind,list_var) #### If you want to take a look at the netcdf files first use: list2env( lapply(setNames(temp, make.names(gsub("*.nc$", "", temp))), nc_open), envir = .GlobalEnv) #import all parameters lists to global environment 可以解决问题

HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD type = GetFileType(hIn);
switch (type) {
case FILE_TYPE_CHAR: 
    // it's from a character device, almost certainly the console
case FILE_TYPE_DISK:
    // redirected from a file
case FILE_TYPE_PIPE:
    // piped from another program, a la "echo hello | myprog"
case FILE_TYPE_UNKNOWN:
    // this shouldn't be happening...
}

==========

对于所有值:

这是一个开始;它是迭代的,但正如我评论的那样,np.where也是如此。

In [1548]: I,J = np.nonzero(A)    # np.where 
In [1549]: Acol = np.column_stack((I,J,A[I,J]))

我们可以创建一个由({1}}的索引和展平值组成的(n,3)数组。但是np.savetxt仍会依次对其进行迭代:

In [1523]: A=np.arange(25).reshape(5,5)

In [1526]: list(np.ndenumerate(A))
Out[1526]: 
[((0, 0), 0),
 ((0, 1), 1),
 ((0, 2), 2),
 ((0, 3), 3),
 ((0, 4), 4),
 ((1, 0), 5),

In [1528]: with open('txt','w') as f:
      ...:     for (i,j),v in np.ndenumerate(A):
      ...:         f.write('%d::%d::%d\n'%(i,j,v))
      ...:         
In [45]: cat txt
0::0::0
0::1::1
0::2::2
0::3::3
0::4::4
1::0::5

以下是制作此3列数组的一种方法:

A

答案 1 :(得分:0)

这是一个简单的方法,可以手动将原始数组重新整形为由 row,col,value 组成的非常长的数组。只需过滤行非零,它应该工作。以下示例适用于此随机50,50数组。

a = np.random.randint(0,5, (50, 50))

rows = np.repeat(np.arange(50), 50)
cols = np.tile(np.arange(50), 50)

data = np.column_stack((rows, cols, a.flatten()))

data_filtered = data[data[:, 2] != 0]
np.savetxt('temp.txt',data_filtered,fmt='%d',delimiter="::",newline="\n");