fill numpy array with sequence of numbers - python

I have a numpy array with the shape (6, 3, 4) that I'd like to fill with an ascending sequence of numbers so that the resulting array is this:
array([[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[2, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[3, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[4, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[5, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]], dtype=uint8)
I do not want to use a loop, if possible.
I've tried the following with no luck:
import numpy as np
new_row = np.zeros([6, 3, 4], dtype=np.uint8)
new_row[:,0:1,0:1] = range(6)
which results in ValueError: could not broadcast input array from shape (6) into shape (6,1,1)
Any help appreciated

Related

PIL Python: A rounded rectangle image converted to numpy array is all zeros

a_rect = Image.new('RGBA', (400, 100))
draw = ImageDraw.Draw(a_rect)
draw.rounded_rectangle((0, 0, 400, 100),
outline=None,
radius=75,
fill='blue'
)
a_rect
When i convert the above image to array using np.asarray(a_rect) I get following:
array([[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
...,
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
...,
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
...,
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
...,
...
[0, 0, 0, 0],
...,
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]], dtype=uint8)
This is not the expected behaviour, I am unable to manipulate this array or apply animation to it due to it being all zeros.

Strange behavior of skimage.morphology.skeletonize3d

It is strange the if using skimage.morphology.skeletonize_3don structure as below. It will remove all elements. Such structure is a equilateral triangle in 3d space.
array = np.array([
[[0, 1, 0],
[0, 0, 1],
[0, 0, 0]],
[[0, 0, 0],
[0, 1, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]]).astype('uint8')
morphology.skeletonize_3d(array)
Output:
array([[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]], dtype=uint8)
It results as an empty array. This is strange. Could anyone explain it? How to avoid it?

Change zeroes in 2D array one by one, and put the resulting arrays into another array in Python [duplicate]

This question already has answers here:
How do I compute all possibilities for an array of numbers/bits (in python, or any language for that matter)
(5 answers)
Closed 2 years ago.
I am trying to achieve the following. I have a 2D array, which is of a 4x4 dimension. I want to get all possibilities, where I can insert a single 1 instead of a zero, and return an array, which contains all of these possibilities
So if we take:
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
would result in:
[[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
...
There would be a total of 14 entries in the resulting array, since there were 14 zeroes in the input array.
The problem is, that the code I have currently should work, as far as I understand, but I can't seem to get where it goes wrong.
def getPossibilities(arr):
p = []
for i in range(4):
for j in range(4):
if arr[i][j] == 0:
p.append(arr)
p[-1][i][j]=1
return p
for i in getPossibilities([[0,1,0,0],[0,0,1,0],[0,0,0,0],[0,0,0,0]]):
print(i)
This results in 14 arrays of solid ones.
I included the way I check the results, in case there is an error there. I also tried with first copying the arr array into a temporary one, then make the changes, but to no avail.
What goes wrong here? I cannot seem to find an answer. Also, is there a more elegant and faster way of doing this? It would be really beneficial for my usecase.
Thank you very much in advance!
This is somewhat tricky but since you have a list of lists, the copy won't work and you will be changing the array every time, what you need is deepcopy:
import copy
def getPossibilities(arr):
p = []
for i in range(4):
for j in range(4):
if arr[i][j] == 0:
tmp = copy.deepcopy(arr)
tmp[i][j]=1
p.append(tmp)
return p
for i in getPossibilities([[0,1,0,0],[0,0,1,0],[0,0,0,0],[0,0,0,0]]):
print(i)
[[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [1, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 1, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 1, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]]

How to set values in a 2d numpy array?

I am working with a 2-d numpy array which looks like this:
array([[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
...,
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
...,
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
...,
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]])
So I have numpy inside numpy which has a list of four values (pixels in RGBA to be specific). I want to set all values to 0. What is the most pythonic way to do it?
Thanks in advance!
m[:] = 0
would set all values of your array m to zero.
If you need an array of zeros with the same shape and datatype of m, use:
np.zeros_like(m)
import numpy as np
a = np.random.randn(10, 10)
b = np.zeros_like(a)
b will be an ndarray of exactly the same shape as the original, filled with 0.

Python array creation with shape

a = np.diag(np.array([2,3,4,5,6]),k=-1)
For the above code, I want to know how to change it for shaping the 6*6 matrix into 6*5 matrix with the first line is filled with 0 and the following lines with 2,3,4,5,6 to be diagonal? Thank you very much
I don't understand what you want to know.
In your code if k>0
then the resultant matrix will have k extra columns,if k=2 then,
output will be :
array([[0, 0, 2, 0, 0, 0, 0],
[0, 0, 0, 3, 0, 0, 0],
[0, 0, 0, 0, 4, 0, 0],
[0, 0, 0, 0, 0, 5, 0],
[0, 0, 0, 0, 0, 0, 6],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
And if k<0 then it will have the k extra rows , for example if k=-1
then:
array([[0, 0, 0, 0, 0, 0],
[2, 0, 0, 0, 0, 0],
[0, 3, 0, 0, 0, 0],
[0, 0, 4, 0, 0, 0],
[0, 0, 0, 5, 0, 0],
[0, 0, 0, 0, 6, 0]])
and if k=0 then :
array([[2, 0, 0, 0, 0],
[0, 3, 0, 0, 0],
[0, 0, 4, 0, 0],
[0, 0, 0, 5, 0],
[0, 0, 0, 0, 6]])
I think you want to create a matrix of 5*5 and then want too add a row. Then you can do it using this
a=a.tolist()
Now a is 2d list and you can insert the row wherever you want.
Do this for your result.
a.insert(0,[0,0,0,0,0])

Categories

Resources