can I assign a 3d vector to a 2d matrix - python

if I have a 2D matrix, and I want to assign a vector [1,1,1] into each cell of my M matrix
vector = np.array([1,1,1])
M= np.zeros((4,4)).astype(np.object)
M[:]=vector.astype(object)
This will obviously give me the error that:
ValueError: could not broadcast input array from shape (2) into shape (3,3)
So is there any method I can store my 3d vector into each cell of my 4x4 M matrix?
Thanks!
I know that if I iterate the ndarray I can do it
for i in range(np.shape(M)[0]):
for j in range(np.shape(M)[1]):
M[i][j]=vector
just wandering whether there's a simple syntax for this

You need to declare what the entries of your matrix should contain with the argument dtype, namely vector.dtype.
This link might help: Numpy - create matrix with rows of vector

Related

What is a 1-D np.array of dimension m?

I have a task and the output should be a "1-D np.array of dimension m" and I don't understand how a 1-D array can have m Dimension, it has 1 per definition ?
The word dimension can mean multiple things, in this case it means the size/length of the singular dimension, i.e. you can say an array has dimensions 2x2.
Therefore, a 1D array of dimension m is equivalent to a list of length m.

Concatenate 3d and 2d array

I have 2 arrays
The first one has this shape
(4133,10000,12)
and the second one has this shape:
(4133,2)
I want to combine those two arrays so I get this shape
(4133,10000,12,2)
Shape of an array along a dimension is NOT the total number of elements. It is the number of elements PER corresponding dimension. Thus, you cannot concatenate arrays of shapes (4133,10000,12) and (4133,2) to have an array of shape (4133,10000,12,2). An easier example to think of is two matrices of shapes (m,n) and (m,k). You cannot concatenate them to have an array of shape (m,n,k).
I don't know what you mean by combine but you can reshape the arrays then let broadcasting kick in. For example:
x = np.empty((10,8,4))
y = np.empty((10,2))
combined = x.reshape((10,8,4,1))*y.reshape((10,1,1,2))
print(combined.shape)
# (10,8,4,2)

Finding eigen values of numpy array of arrays

So I have a 3 numpy arrays which has the following dimensions,
a.shape = (704, 528)
b.shape = (704, 528)
c.shape = (704, 528)
And I have a square matrix that looks like this,
mat = np.array([[a, b], [b, c]])
I need to find the eigen values of this. I'm aware that it's going to be a matrix of eigen values. But when I use numpy.linalg.eig(), it gives me an error: numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square.
I haven't found many resources as to how to do this, could someone guide me to any sources or give me a solution? Thank you!
Eigenvalues are only defined for square matrices.
Your matrix has 2*704 = 1408 rows and 2*528 = 1056 columns, hence you get an error as numpy.linalg.eig() is expecting a square matrix as input.
Depending on your goal of wanting to compute eigenvalues, you might want to consider SVD which is defined for non-square matrix as well. You might also want to examine if the matrix that you constructed is indeed the matrix that you intend to construct.

Followup on question "Use coo_matrix in TensorFlow"

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).

Python: Reshaping arrays and lists

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.

Categories

Resources