Create an identity matrix with scalar in python - python

This creates an identity matrix.
np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
But what if I want to multiply with 3I like this?
array([[3., 0., 0.],
[0., 3., 0.],
[0., 0., 3.]])

Related

Making a 3D diagonal matrix in Python

I would like to create 3D diagonal matrices. I already succeded to create one with numpy routine numpy.fill_diagonal(numpy.zeros((N, N, N)), n), however it does not allow to choose the diagonal to fill.
In other words, I would like to find the 3D generalization of this numpy routine : https://numpy.org/doc/stable/reference/generated/numpy.diag.html. Thank you.
Well instead of using np.diag to fill a semi diagonal you can do it manually like this:
N = 4
arr = np.zeros((N, N))
i = np.arange(N-1)
arr[i,i+1] = 1
array([[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0., 0., 0., 0.]])
And it has the advantage to generalize to 3d arrays.
arr = np.zeros((N, N, N))
i = np.arange(N-1)
arr[i,i,i+1] = 1
array([[[0., 1., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 1.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])

weird behavior of numpy when it calculates a vector and matrix multiplication

I have the following weird behavior of numpy where numpy can't multiply a (n,n) matrix with (n,) matrix and convert the later to (1,n) matrix. I tried different examples and it worked fine. u and s were obtained from svd function as follows:
[u, s, vt] = np.linalg.svd(G)
svd_estimate = np.matmul(u * s, vt)
and G is a numpy matrix. I tried to squeeze(s) but also didn't work. What am I missing? numpy version is '1.19.2'
Look at what svd produces for a matrix versus array:
In [24]: np.linalg.svd(np.matrix(np.eye(3)))
Out[24]:
(matrix([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]),
array([1., 1., 1.]),
matrix([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]))
In [25]: np.linalg.svd(np.eye(3))
Out[25]:
(array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]),
array([1., 1., 1.]),
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]))
With the array values, as shown in the docs:
In [27]: u,s,vh=_25
In [28]: np.dot(u*s,vh)
Out[28]:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
With the matrix results we have use np.multiply
In [37]: u,s,vh=_24
In [38]: np.multiply(u,s)
Out[38]:
matrix([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
In [39]: np.multiply(u,s)*vh
Out[39]:
matrix([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

ValueError: Found input variables with inconsistent numbers of samples: [13, 26]

This is my predictions outcome
array([[1., 0., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 1., 0.],
[0., 0., 1.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.],
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.],
[0., 0., 1.],
[1., 0., 0.],
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.]], dtype=float32)
and this is the confusion_matrix() function
cm = confusion_matrix(test_labels, predictions[:,0])
My query is how this confusion_matrix() functions works and how to solve this issue?
As I am a novice it will be really helpful if anyone can give me a little explanation.
Thank you.
This is primarily due to the shape of the arrays not being similar. Please check the arrays with test_labels.shape etc. Then use the reshape method or split them properly so that the shapes match.

Reduce 3D volume mask by uniform margin in python

I'm working with 3D boolean arrays that mask a volume. My goal is to take a mask and reduce the area of the mask by some margin, m, in all dimensions.
Is there an easy way to do this using some common libraries (numpy, scipy, pandas, etc..)?
I found some code online that uses multiple for loops to expand a mask by one dimension. This works for the expansion case but I feel like there is a more compact way out there.
Here is a minimum example of what I am looking for in 2D.
Original
array([[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[1., 1., 1., 1., 1.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.]])
Uniform reduction by 1 pixel
array([[0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0.]])
But I want this to be in 3D space. Thank you for any input.
You might be looking for scipy.ndimage.binary_erosion(a):
a = np.array([
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[1., 1., 1., 1., 1.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.]
])
b = scipy.ndimage.binary_erosion(a) # returns an array of bool
Note that this will erode internal surfaces too

Is it possible to Copy Subregion of One 2D Array to Another Using numpy.ndarray

I have some 2d arrays using numpy, and I want to copy subregions from one into another. For example, if I start with:
dest = numpy.zeros((4, 4))
# array([[0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]])
src = numpy.ones((4, 4))
# array([[1., 1., 1., 1.],
# [1., 1., 1., 1.],
# [1., 1., 1., 1.],
# [1., 1., 1., 1.]])
I want to somehow say that the src should be copied into dest at (2,1), such that source would then look like:
array([[0., 0., 0., 0.],
[0., 0., 1., 1.],
[0., 0., 1., 1.],
[0., 0., 1., 1.]])
Or if (-3, 0), then:
array([[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.]])
I can do the good old fashioned double index loop to do this, but I was hoping numpy had some clever magic that did it. I looked at take, but couldn't see how to make that the tool for this job.
Both of these can be accomplished with numpy indexing. To understand how this works, the documentation is always your friend.
Your first case:
dest[1: ,2:] = src[1: ,2:]
array([[0., 0., 0., 0.],
[0., 0., 1., 1.],
[0., 0., 1., 1.],
[0., 0., 1., 1.]])
Your second case: (You indicated column -3 but your results indicate -4)
dest[:, -4] = src[:, -4]
array([[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.]])

Categories

Resources