In the answer to the question was mentioned:
tf_coo_matrix = tf.SparseTensorValue(
indices=np.array([coo_matrix.rows, coo_matrix.cols]).T,
values=coo_matrix.data,
dense_shape=coo_matrix.shape)
I'm trying to understand why one needs to transpose a scipy sparse matrix when using TensorFlow. Thanks in advance.
If you look at the documentation of tf.SparseTensor, indices is expected to be a 2-dimensional tensor with dimensions (N, ndims), where N is the number of non-zero values in the sparse tensor and ndims is its number of dimensions. For a sparse matrix (two dimensions), each row will contain the row and the column of the corresponding value in values.
In the snippet, coo_matrix.rows is an array of row indices of the sparse matrix, and coo_matrix.cols is an array of column indices. np.array([coo_matrix.rows, coo_matrix.cols]) will be an array with two rows and N columns, which is the opposite order of what a sparse tensor expects, so transposing it with .T you get the (N, 2) indices tensor. Not that you are not transposing the sparse matrix, the indices still remain the same, you are just giving them to tf.SparseTensorValue in the required order. You could get the same result by doing, for example, np.stack([coo_matrix.rows, coo_matrix.cols], axis=1).
Related
Suppose I have a numpy array A with shape (j,d,d) and I want to obtain an array with shape j, in which each entry corresponds to the determinant of each (d,d) array.
I tried using np.apply_along_axis(np.linalg.det(A), axis=0), but np.apply_along_axis only seems to work for 1D slices.
Is there an efficient way of doing that using only numpy?
np.linalg.det can already do this for an array of arbitrary shape as long as the last two dimensions are square. You can see the documentation here.
My goal is to to turn a row vector into a column vector and vice versa. The documentation for numpy.ndarray.transpose says:
For a 1-D array, this has no effect. (To change between column and row vectors, first cast the 1-D array into a matrix object.)
However, when I try this:
my_array = np.array([1,2,3])
my_array_T = np.transpose(np.matrix(myArray))
I do get the wanted result, albeit in matrix form (matrix([[66],[640],[44]])), but I also get this warning:
PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
my_array_T = np.transpose(np.matrix(my_array))
How can I properly transpose an ndarray then?
A 1D array is itself once transposed, contrary to Matlab where a 1D array doesn't exist and is at least 2D.
What you want is to reshape it:
my_array.reshape(-1, 1)
Or:
my_array.reshape(1, -1)
Depending on what kind of vector you want (column or row vector).
The -1 is a broadcast-like, using all possible elements, and the 1 creates the second required dimension.
If your array is my_array and you want to convert it to a column vector you can do:
my_array.reshape(-1, 1)
For a row vector you can use
my_array.reshape(1, -1)
Both of these can also be transposed and that would work as expected.
IIUC, use reshape
my_array.reshape(my_array.size, -1)
I have two scipy.sparse.csr_matrices with identical non-zero locations. I was assuming that their indptr and indices arrays would be identical. But it turns out only their indptr arrays are identical (and sorted). The indices array, which stores the columns for any row, are permutations for any row.
In the image above, let A = sum_predictions and B = Yt_preds[4] be two matrices. Both the matrices have identical non-zero locations. We see that their indptr arrays (number of nonzeros in each row) are identical. However the indices arrays (columns for any particular row) are permutations of each other.
How is the order of the indices array in scipy sparse matrix determined? How can I get a representation where two matrices with identical non-zero locations have the same indptr and indices arrays?
The solution is what CJR proposed: mat.sortindices()
I have the following problem in python: I have several numpy 2d-arrays where all have same number of columns ,but different number of rows.I want to index all these 2d-array in a unique numpy 3d-array where the first index keeps into account each 2d-array.
For example :
let's suppose I got two 2d-arrays like this :
[[1,2,3][4,5,6][7,8,9]] (3X3 array)
[[11,12,13][14,15,16]] (2X3 array)
I want to get a numpy 3d-array name,for example, c where : c[0] has shape (3,3), c[1] (2,3) and so on...So I expect a 3d-array whose shape is (number of 2d matrices,number of rows of one matrix,number of columns of one matrix)
How can I reshape since each 2d-arrays have different number of rows each other?
Thanks
I have a numpy ndarray object with the following shape:
(3, 256, 170, 256).
So, basically this represents an array of 3-dimensional vectors. The dimension of the vector is the first element as it enables one to write something like: array[0] for the relevant vector component.
Now, I am trying to use scipy pdist function, which computes the distance between the entries. So, I need to modify this array, so that it can be represented as a two dimensional matrix, where the number of rows is 256*170*256 and the number of columns is 3 and pdist should return me the matrix where each element is the squared distance between the corresponding 3 dimensional vectors (if I have interpreted the documentation correctly).
Can someone tell me how I can get a view into this numpy array, so that I can generate this matrix. I do not want to copy the data again (as these matrices can be quite large), so looking for some efficient solutions.