I have a 100x100 numpy array that I want to add for it a third dimension which has length 3 which have [1,0,1].
I'm trying to do this without a for loop if possible.
Tried all sort of things like np.newaxis but it only expands the dimension with length 1 and then it can't be populated with a array of length 3.
Thank yiu
Depending on what you want you have a few options:
import numpy as np
arr = np.random.random((100, 100))
some_numbers = [1, 0, 1]
# A
new_arr = np.empty(arr.shape + (3,))
new_arr[..., :] = some_numbers
# array([[[1., 0., 1.],
# [1., 0., 1.],
# [1., 0., 1.],
# ...,
# A2
new_arr = np.empty(arr.shape + (len(some_numbers) + 1,))
new_arr[..., 0] = arr[..., np.newaxis]
new_arr[..., 1:] = some_numbers
# array([[[0.2853, 1., 0., 1.],
# [0.7324, 1., 0., 1.],
# [0.0706, 1., 0., 1.],
# ...,
# B
new_arr = np.empty(arr.shape + (3,))
new_arr[..., :] = arr[..., np.newaxis]
# C
new_arr = np.repeat(arr[..., np.newaxis], 3, axis=-1)
# array([[[0.2853, 0.2853, 0.2853],
# [0.7324, 0.7324, 0.7324],
# [0.0706, 0.0706, 0.0706],
# ...,
In case A you are overwriting all elements of arr with [1, 0, 1].
In case A2 you keep the original array at new_arr[:, :, 0] and fill the remaining planes new_arr[:, :, 1:] with some_numbers respectively.
In case B and case C you repeat the 100x100 array 3 times along the new third dimension.
As I understood, you want to generate a 3-D array:
the first "layer" filled with ones,
the second "layer" filled with zeroes,
the third "layer" filled again with ones,
all "layers" with dimension 100 * 100.
For readablity, I changed your assumptions:
the third "layer" filled with 2,
all "layers" with dimension 5 * 5.
Step 1: Create each 2-D array (layer in the target array):
arr1 = np.ones((5,5), dtype=np.int)
arr2 = np.zeros((5,5), dtype=np.int)
arr3 = np.full((5,5), 2)
Step 2: Create the target array:
res = np.stack((arr1, arr2, arr3), axis=2)
When you print res.shape, you will get:
(5, 5, 3)
(5 rows, 5 columns, 3 layers)
To see each "layer" separately, run res[:, :, n] where n is either
0, 1 or 2. E.g. for n == 2 (the last layer) I got:
array([[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2]])
Related
Suppose I have the following data:
mask = [[0, 1, 1, 0, 1]] # 2D mask
ip_array = [[4, 5, 2]
[3, 2, 1]
[1, 8, 6]] # 2D array
I want to insert columns of 0s into ip_array where ever there is 0 in the mask. So the output should be like:
[[0, 4, 5, 0, 2]
[0, 3, 2, 0, 1]
[0, 1, 8, 0, 6]]
I am new to numpy functions and I am looking for an efficient way to do this. Any help is appreciated!
Here's one way to do it in two steps:
(i) Create an array of zeros of the correct shape (the first dimension of ip_array and the second dimension of mask)
(ii) Use the mask across the second dimension (as a boolean mask) and assign the values of ip_array to the array of zeros.
out = np.zeros((ip_array.shape[0], mask.shape[1])).astype(int)
out[..., mask[0].astype(bool)] = ip_array
print(out)
Output:
[[0 4 5 0 2]
[0 3 2 0 1]
[0 1 8 0 6]]
Here is another approach using slicing with a cumsum mask and an extra 0 column in the input. The cumsum mask will have the indices of the ip_array + 1 and 0 whenever to add zeros. The concatenated array has an extra initial columns of zeros so indexing with 0 yields a column of zeros.
m = (mask.cumsum()*mask)[0]
# array([0, 1, 2, 0, 3])
np.c_[np.zeros(ip_array.shape[0]), ip_array][:,m].astype(int)
# array([[0, 4, 5, 0, 2],
# [0, 3, 2, 0, 1],
# [0, 1, 8, 0, 6]])
A solution with parameters and other way to do than green checked. So it is more understandable.
Juste the last line is important for the operation.
import numpy
import random
n1 = 5
n2 = 5
r = 0.7
random.seed(1)
a = numpy.array([[0 if random.random() > r else 1 for _ in range(n1)]])
n3 = numpy.count_nonzero(a)
b = numpy.array([[random.randint(1,9) for _ in range(n3)] for _ in range(n2)])
c = numpy.zeros((n2, n1))
c[:, numpy.where(a)[1]] = b[:]
Result:
a = array([[1, 0, 0, 1, 1]])
b = array([[8, 8, 7],
[4, 2, 8],
[1, 7, 7],
[1, 8, 5],
[4, 2, 6]])
c = array([[8., 0., 0., 8., 7.],
[4., 0., 0., 2., 8.],
[1., 0., 0., 7., 7.],
[1., 0., 0., 8., 5.],
[4., 0., 0., 2., 6.]])
Here your time processing depending on n-values:
Using this code:
import numpy
import random
import time
import matplotlib.pyplot as plt
n1 = 5
n2 = 5
r = 0.7
def main(n1, n2):
print()
print(f"{n1 = }")
print(f"{n2 = }")
random.seed(1)
a = numpy.array([[0 if random.random() > r else 1 for _ in range(n1)]])
n3 = numpy.count_nonzero(a)
b = numpy.array([[random.randint(1,9) for _ in range(n3)] for _ in range(n2)])
t0 = time.time()
c = numpy.zeros((n2, n1))
c[:, numpy.where(a)[1]] = b[:]
t = time.time() - t0
print(f"{t = }")
return t
t1 = [main(10**i, 10) for i in range(1, 8)]
t2 = [main(10, 10**i) for i in range(1, 8)]
plt.plot(t1, label="n1 time process evolution")
plt.plot(t2, label="n2 time process evolution")
plt.xlabel("n-values (log)")
plt.ylabel("Time processing (s)")
plt.title("Insert columns into a numpy array based on mask")
plt.legend()
plt.show()
mask = np.array([0, 1, 1, 0, 1])
#extract indices of zeros
mask_pos = (list(np.where(mask == 0)[0]))
ip_array =np.array([[4, 5, 2],
[3, 2, 1],
[1, 8, 6]])
#insert 0 at respextive mask position
for i in mask_pos:
ip_array = np.insert(ip_array,i,0,axis=1)
print(ip_array)
I'd like to duplicate a numpy array dimension, but in a way that the sum of the original and the duplicated dimension array are still the same. For instance consider a n x m shape array (a) which I'd like to convert to a n x n x m (b) array, so that a[i,j] == b[i,i,j]. Unfortunately np.repeat and np.resize are not suitable for this job. Is there another numpy function I could use or is this possible with some creative indexing?
>>> import numpy as np
>>> a = np.asarray([1, 2, 3])
>>> a
array([1, 2, 3])
>>> a.shape
(3,)
# This is not what I want...
>>> np.resize(a, (3, 3))
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
In the above example, I would like to get this result:
array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
From 1d to 2d array, you can use the np.diagflat method, which Create a two-dimensional array with the flattened input as a diagonal:
import numpy as np
a = np.asarray([1, 2, 3])
np.diagflat(a)
#array([[1, 0, 0],
# [0, 2, 0],
# [0, 0, 3]])
More generally, you can create a zeros array and assign values in place with advanced indexing:
a = np.asarray([[1, 2, 3], [4, 5, 6]])
result = np.zeros((a.shape[0],) + a.shape)
idx = np.arange(a.shape[0])
result[idx, idx, :] = a
result
#array([[[ 1., 2., 3.],
# [ 0., 0., 0.]],
# [[ 0., 0., 0.],
# [ 4., 5., 6.]]])
I'm trying to learn python. In it, I'm trying to dynamically generate a N x M matrix in python, where each cell contains the index value of that cell in python.
The matrix would look like:
[0,1,2,3,4
0,1,2,3,4
...]
I know that in java it would go something like:
a={}{}
for (i=0;i<N;i++)
for (j=0;j<M:j++)
a[i][j] = i
Where N is the width of the matrix and M is the height of the matrix
Except in python it seems like I can't iterate on a matrix on the basis of the cell placement, rather I need to iterate on the basis of the elements in the cell. From my experience something like
a = [][]
a = np.zeroes((N, M))
[ 0, 0, 0
0, 0, 0]
in the case where N = 3, and M = 2
and then the same style of a loop:
j = 0
for i in len(a):
a[i][j] = i
if i == len(a):
j = j+1
doesn't work because python can't iterate on the basis of the places of the elements. Perhaps I am wrong. Would this work? Is there a better way to make such a matrix and fill it with the indexed values?
Since you're already using NumPy, you could use numpy.arange and numpy.tile:
In [26]: N = 5
In [27]: M = 4
In [28]: np.tile(np.arange(N), (M, 1))
Out[28]:
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
Another option is to create a row using np.arange(5) and assign it to every row of zeros matrix.
In [22]: m = np.zeros((4,5))
In [23]: m[:,] = np.arange(5)
In [24]: m
Out[24]:
array([[ 0., 1., 2., 3., 4.],
[ 0., 1., 2., 3., 4.],
[ 0., 1., 2., 3., 4.],
[ 0., 1., 2., 3., 4.]])
Some example similar to your Java example, but with python syntax sugar.
>>> N=M=5
>>> for z in [[n for n in xrange(N)] for m in xrange(M)]:
... print z
...
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
Here is the code in which matrix contain index value of that cell:
n,m=map(int,raw_input().split())
a=n*[m*[0]]
j=0
for i in range (0,n):
for j in range(0,m):
a[i][j]=j
for i in range (0,n):
for j in range(0,m):
print a[i][j],
print
How to obtain new array (new) from original array (x) by calculating mean as follows:
new = [[mean(1,3), mean(1,3), mean(1,3), mean(1,3), mean(1,3)],[mean(2,4),mean(2,4),mean(2,4),mean(2,4),mean(2,4)]]
import numpy as np
arr1 = np.array([[1,1,1,1,1],[2,2,2,2,2]])
arr2 = np.array([[3,3,3,3,3],[4,4,4,4,4]])
my_array = np.array([arr1,arr2])
for x in my_array:
new = np.mean(x,axis=1)
print (new)
IMPORTANT:
The arr1, arr2, and my_array are not really available as inputs, what is available is only x. So, the real data to be manipulated are in the form of for loop given by x as shown above.
Given my_array as defined above
>>> my_array
array([[[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2]],
[[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]]])
You simply need to take the mean over the first axis as follows:
>>> my_array.mean(axis=0)
array([[ 2., 2., 2., 2., 2.],
[ 3., 3., 3., 3., 3.]])
If it must be iterative for subsequent x you could do the following:
sums = 0
counter = 0
for x in my_array:
sums += x
counter += 1
new = sums / counter
Or, if you can store the data:
data = []
for x in my_array:
data.append(x)
new = np.dstack(data).mean(axis=2)
I'm trying to append one numpy array to another numpy array, like this:
import numpy as np
meanings = 2
signals = 4
def new_agent(agent_type, context_size):
if agent_type == 'random':
comm_system = np.random.random_integers(0, 1, (meanings, signals))
if agent_type == 'blank':
comm_system = np.zeros((meanings, signals), int)
score_list = np.array([0., 0., 0., 0.])
np.append(comm_system, score_list)
np.append(comm_system, context_size)
return comm_system
if I now call:
random_agent = new_agent('random', 5)
I expect to get something like:
[[0 1 0 0]
[1 1 0 1]
[0. 0. 0. 0.]
5]
But instead I get only:
[[0 1 0 0]
[1 1 0 1]]
So the score_list and the context_size don't get appended. And the same holds for when I call new_agent() with 'blank'.
Thanks!
You can use hstack and vstack to concatenate arrays:
>>> from numpy import array, hstack, vstack
>>> a = array([1, 2, 3])
>>> b = array([4, 5, 6])
>>> hstack([a, b])
array([1, 2, 3, 4, 5, 6])
>>> vstack([a, b])
array([[1, 2, 3],
[4, 5, 6]])
numpy.append() returns a new array containing the data from its inputs together. It does not modify the inputs themselves, and there would be no way for it to do so. This is because arrays in NumPy are generally not resizable.
Try changing your code to capture the value returned from append(), which will be the array you want.
#John is correct about how to use the return value from numpy.append because it doesn't modify the original array. However, there's a problem with your expected output:
[[0 1 0 0]
[1 1 0 1]
[0. 0. 0. 0.]
5]
is not a possible numpy array because of two reasons: one is that some elements are integers and some are floats, but a numpy array's dtype must be uniform; the other is that each row is not the same length, but numpy arrays must have uniform (rectangular) shape.
I think what you might rather do is to just return all three things:
comm_system as an array of ints,
score_list as an array of floats,
and context_size as an int (not an array).
You can do that with a tuple:
def new_agent(agent_type, context_size):
if agent_type == 'random':
comm_system = np.random.random_integers(0, 1, (meanings, signals))
if agent_type == 'blank':
comm_system = np.zeros((meanings, signals), int)
score_list = np.zeros(signals) #This is different too! No need to type out the 0, 0, ...
# now just return all three:
return comm_system, score_list, context_size
Then you can "unpack" the tuple like so:
random_agent, scores, size = new_agent('random', 5)
Or just keep them all in one tuple:
random_agent_info = new_agent('random', 5)
And you'll have
In [331]: random_agent, scores, size = new_agent('random', 5)
In [332]: random_agent
Out[332]:
array([[0, 1, 1, 0],
[0, 1, 0, 1]])
In [333]: scores
Out[333]: array([ 0., 0., 0., 0.])
In [334]: size
Out[334]: 5
In [336]: random_agent_info
Out[336]:
(array([[1, 1, 0, 1],
[0, 1, 0, 0]]),
array([ 0., 0., 0., 0.]),
5)
In [337]: random_agent_info[0]
Out[337]:
array([[1, 1, 0, 1],
[0, 1, 0, 0]])
In [338]: random_agent_info[1]
Out[338]: array([ 0., 0., 0., 0.])
In [339]: random_agent_info[2]
Out[339]: 5
If you do want to have the comm_system and score_list to be one (3,2) array, you can do that with:
def new_agent(agent_type, context_size):
...
return np.vstack([comm_system, score_list]), context_size
Then you'll get one array and one int:
In [341]: random_agent, size = new_agent('random', 5)
In [342]: random_agent
Out[342]:
array([[ 1., 0., 1., 1.],
[ 1., 0., 1., 0.],
[ 0., 0., 0., 0.]])
In [343]: size
Out[343]: 5