I have 3 arrays and one list:
array1.shape = (1000,5,5,7)
array2.shape = (1000,)
array3.shape = (1000,)
len(list1) = (1000)
I want to save all of these to a numpy file. When I used to run in Python 2.7, the command I used to use was:
np.save(filename,[array1, array2, array3, list1])
And everything worked great, including loading all of the data with np.load. However, when I try this command in Python 3.6 I get an error:
could not broadcast input array from shape (1000,5,5,7) into shape (1000)
How am I able to save the 3 arrays as well as the list into a single numpy array in Python 3.6?
Related
I have a 2D numpy array which I converted to R matrix and now I need to convert it further to named list:
rpy2.robjects.numpy2ri.activate()
nr,nc = counts.shape
r_mtx = robjects.r.matrix(counts, nrow=nr, ncol=nc)
So, I got the matrix r_mtx, but I am not sure how to make a named list out of it similar to how we do it in R:
named_list <- list(counts=mtx)
I need it to feed into SingleCellExperiment object to do dataset normalization:
https://bioconductor.org/packages/devel/bioc/vignettes/scran/inst/doc/scran.html
I tried using rpy2.rlike.container both TaggedList and OrdDict but can't figure out how to apply them to my case.
Ultimately I solved it (avoiding convertion of numpy array to r matrix), straight making the named list from the numpy array:
named_list = robjects.r.list(counts=counts)
Where counts is a 2D numpy array
This question already has answers here:
Sliding windows from 2D array that slides along axis=0 or rows to give a 3D array
(2 answers)
Closed 4 years ago.
I have a 64x64 numpy array and I have a 5x64 window. I want to slide this window over the main numpy array with a step size of 1 and save the values that lie in that window in a column in an empty numpy array.
Thanks
Exactly as you said in the comment, use the array index and incrementally iterate. Create a list (a in my case) to hold your segmented windows (window). In the end, use np.hstack to concatenate them.
import numpy as np
yourArray = np.random.randn(64,64) # just an example
winSize = 5
a = [] # a python list to hold the windows
for i in range(0, yourArray.shape[0]-winSize+1):
window = yourArray[i:i+winSize,:].reshape((-1,1)) # each individual window
a.append(window)
result = np.hstack(a)
I have an array of complex numbers in Matlab and I want to import that data in Python. I have tried all methods including Scipy module and h5py etc. Can anyone tell me any other possible way?
My Matlab version is 2017b. and python version is 2.7.
In MATLAB, save your data with the '-v7' option:
myMat = complex(rand(4), rand(4));
save('myfile', 'myMat', '-v7')
In Python, load the .mat file with scipy.io.loadmat. The result is a Python dict:
>>> d = scipy.io.loadmat('myfile.mat')
>>> m = d['myMat']
>>> m[0,0]
'(0.421761282626275+0.27692298496088996j)'
and so on.
a=[1,2,3];
context_var = autograd.Variable(torch.LongTensor(a))
This is giving an error
RuntimeError: tried to construct a tensor from a int sequence, but found an item of type numpy.int32 at index
I am not able to figure out how to get over this.
Your code works perfectly fine in the recent version of pytorch. But for older versions, you can convert the numpy array to list using .tolist() method as follows to get rid of the error.
a=[1,2,3];
context_var = autograd.Variable(torch.LongTensor(a.tolist()))
Works fine for me:
a=[1,2,3]
print(torch.autograd.Variable(torch.LongTensor(a)))
b = np.array(a)
print(torch.autograd.Variable(torch.LongTensor(b)))
outputs:
Variable containing:
1
2
3
[torch.LongTensor of size 3]
Variable containing:
1
2
3
[torch.LongTensor of size 3]
I'm using Python 3.6.2, torch 0.2.0.post3, and numpy 1.13.3.
I am following this tutorial: graph tutorial on my own data file. but I am getting stuck on the last bit. the code seems to throw:
(most recent call last)<ipython-input-193-7227d35394c0> in <module>()
1 for node in links: #loops through each link and changes each dictionary to a tuple so networkx can read in the information
2 edges = node.items()
----> 3 G.add_edge(*edges[0]) #takes the tuple from the list and unpacks the tuples
4
5 nx.draw(G)
TypeError: 'dict_items' object does not support indexing
is there a fix? I'm sort of stuck.
This is probably python-2 only compatible code.
In python 2, dict.items() returns a list of tuples (key, value) as opposed to dict.iteritems()
In python 3, dict.iteritems() has been removed, and dict.items() returns an iterator.
So you have to explicitly convert it to list to get access to the elements by index:
edges = list(node.items())