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

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.

Related

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

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

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)

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

set dimension of svd algorithm in python

svd formular: A ≈ UΣV*
I use numpy.linalg.svd to run svd algorithm.
And I want to set dimension of matrix.
For example: A=3*5 dimension, after running numpy.linalg.svd, U=3*3 dimension, Σ=3*1 dimension, V*=5*5 dimension.
I need to set specific dimension like U=3*64 dimension, V*=64*5 dimension. But it seems there is no optional dimension parameter can be set in numpy.linalg.svd.
If A is a 3 x 5 matrix then it has rank at most 3. Therefore the SVD of A contains at most 3 singular values. Note that in your example above, the singular values are stored as a vector instead of a diagonal matrix. Trivially this means that you can pad your matrices with zeroes at the bottom. Since the full S matrix contains of 3 values on the diagonal followed by the rest 0's (in your case it would be 64x64 with 3 nonzero values), the bottom rows of V and the right rows of U don't interact at all and can be set to anything you want.
Keep in mind that this isn't the SVD of A anymore, but instead the condensed SVD of the matrix augmented with a lot of 0's.

Randomly sample rows (first dimension) of an array with unknown dimension

When the dimension is known, the task is trivial. Take a 2D array:
a = np.random.randint(10, a=(5,2))
a[np.random.choice(a.shape[0]), :]
However, in my function, the dimension of the array is arbitrary. How to handle this situation?
use the size of the 1st dimension to determine the random range:
a[np.random.randint(0,a.shape[0],10)]
or if you prefer, include an Ellipsis
a[np.random.randint(0,a.shape[0],10),...]
1 indexing array selects from rows by default.

Categories

Resources