Multiplication in Python with arrays of different length - python

I have a five 100x100 arrays, A, and I want to multiply each matrix by a value from an array of length five, B. I wish to multiply the first matrix in A by the first value in B and the second matrix by the second value in B, etc. Am I able to do this?

Actually the answer has been provided by gboffi in his comment. Yet I want to elaborate that answer, giving a concrete example with code:
import numpy as np
#example data, all arrays of ones 100x100
A1 = A2 = A3 =A4 = A5 = np.ones((100, 100))
#example array containing the factor for each matrix
B = np.array([1, 2, 3, 4, 5])
#create an array containing all matrices
A = np.array([A1, A2, A3, A4, A5])
A*B[:,None,None]
The result then looks like this:
array([[[ 1., 1., 1., ..., 1., 1., 1.],
[ 1., 1., 1., ..., 1., 1., 1.],
[ 1., 1., 1., ..., 1., 1., 1.],
...,
[ 1., 1., 1., ..., 1., 1., 1.],
[ 1., 1., 1., ..., 1., 1., 1.],
[ 1., 1., 1., ..., 1., 1., 1.]],
[[ 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., 2., 2., ..., 2., 2., 2.],
[ 2., 2., 2., ..., 2., 2., 2.]],
[[ 3., 3., 3., ..., 3., 3., 3.],
[ 3., 3., 3., ..., 3., 3., 3.],
[ 3., 3., 3., ..., 3., 3., 3.],
...,
[ 3., 3., 3., ..., 3., 3., 3.],
[ 3., 3., 3., ..., 3., 3., 3.],
[ 3., 3., 3., ..., 3., 3., 3.]],
[[ 4., 4., 4., ..., 4., 4., 4.],
[ 4., 4., 4., ..., 4., 4., 4.],
[ 4., 4., 4., ..., 4., 4., 4.],
...,
[ 4., 4., 4., ..., 4., 4., 4.],
[ 4., 4., 4., ..., 4., 4., 4.],
[ 4., 4., 4., ..., 4., 4., 4.]],
[[ 5., 5., 5., ..., 5., 5., 5.],
[ 5., 5., 5., ..., 5., 5., 5.],
[ 5., 5., 5., ..., 5., 5., 5.],
...,
[ 5., 5., 5., ..., 5., 5., 5.],
[ 5., 5., 5., ..., 5., 5., 5.],
[ 5., 5., 5., ..., 5., 5., 5.]]])

Related

combining 2 numpy arrays

I have 2 numpy arrays:
one of shape (753,8,1) denoting 8 sequential actions of a customer
and other of shape (753,10) denoting 10 features of a training sample.
How can I combine these two such that:
all 10 features are appended to each of the 8 sequential actions of a training sample , that is, the combined final array should have shape of (753,8,11).
Maybe something like this:
import numpy as np
# create dummy arrays
a = np.zeros((753, 8, 1))
b = np.arange(753*10).reshape(753, 10)
# make a new axis for b and repeat the values along axis 1
c = np.repeat(b[:, np.newaxis, :], 8, axis=1)
c.shape
>>> (753, 8, 10)
# now the first two axes of a and c have the same shape
# append the values in c to a along the last axis
result = np.append(a, c, axis=2)
result.shape
>>> (753, 8, 11)
result[0]
>>> array([[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]])
# values from b (0-9) have been appended to a (0)

Numpy reshape/dimension change

I want to change an array such as
array([[ 7., 3., 14., 1., 9., 17.],
[ 7., 3., 14., 1., 9., 17.],
[ 7., 3., 14., 1., 9., 17.],
[ 7., 3., 14., 1., 9., 17.]])
To
array([[[7., 3.],
[7., 3.],
[7., 3.],
[7., 3.]],
[[14., 1.],
[14., 1.],
[14., 1.],
[14., 1.]],
[[9., 17.],
[9., 17.],
[9., 17.],
[9., 17.]]])
I thought I would manage with reshape, but none of the order statements work in this case. Currently, I am doing it this way
np.vstack([mat[:, i-2:i] for i in range(2, mat.shape[1]+1, 2)]).reshape(3,-1,2)
And I was wondering if there is a better way to do it
reshape and then swapaxes:
import numpy as np
a = np.array(
[[ 7., 3., 14., 1., 9., 17.],
[ 7., 3., 14., 1., 9., 17.],
[ 7., 3., 14., 1., 9., 17.],
[ 7., 3., 14., 1., 9., 17.]])
a.reshape((a.shape[0], -1, 2)).swapaxes(0, 1)

Python - Concatenate or stack more than two arrays with different shape

I would like to get an array of size 11x11 with different subarrays, for example the array M composed by the following arrays (shape in parenthesis):
CC(3x3) CA(3x4) CB(3x4)
AC(4x3) AA(4x4) AB(4x4)
BC(4x3) BA(4x4) BB(4x4)
I could use concatenate but it is not optimal. I also tried the stack function, but arrays must have the same shape. Do you have any ideas to do it?
Thanks a lot!
You want np.block(). It creates an array out of 'blocks', like what you have. For e.g.
>>> CC = 1*np.ones((3, 3))
>>> CA = 2*np.ones((3, 4))
>>> CB = 3*np.ones((3, 4))
>>> AC = 4*np.ones((4, 3))
>>> AA = 5*np.ones((4, 4))
>>> AB = 6*np.ones((4, 4))
>>> BC = 7*np.ones((4, 3))
>>> BA = 8*np.ones((4, 4))
>>> BB = 9*np.ones((4, 4))
>>> M = np.block([[CC, CA, CB],
[AC, AA, AB],
[BC, BA, BB]])
>>> M
array([[ 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.],
[ 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.],
[ 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.],
[ 4., 4., 4., 5., 5., 5., 5., 6., 6., 6., 6.],
[ 4., 4., 4., 5., 5., 5., 5., 6., 6., 6., 6.],
[ 4., 4., 4., 5., 5., 5., 5., 6., 6., 6., 6.],
[ 4., 4., 4., 5., 5., 5., 5., 6., 6., 6., 6.],
[ 7., 7., 7., 8., 8., 8., 8., 9., 9., 9., 9.],
[ 7., 7., 7., 8., 8., 8., 8., 9., 9., 9., 9.],
[ 7., 7., 7., 8., 8., 8., 8., 9., 9., 9., 9.],
[ 7., 7., 7., 8., 8., 8., 8., 9., 9., 9., 9.]])

How to make a loop for going through the input variables in python?

I want to modify this code by making maybe some sort of loop that could go through the variable names so I should not write down that 'if data1 is not None' part? I was also wondering if there is a way that I could make some sort of dynamic code that the number of inputs into the function could change somehow, for example, let's say I want to input 100 different data sets, I can't write down all of them in the part of input for function, what should I do for that?
Also, how could I put title for both of the plots? because when I use the plt.title(), it only shows the last title.
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(4)
randomSet = np.random.randint(0, 2, (10, 20))
np.random.seed(3)
randomSet3 = np.random.randint(0, 2, (10, 20))
np.random.seed(2)
randomSet2 = np.random.randint(0, 2, (10, 20))
np.random.seed(1)
randomSet1 = np.random.randint(0, 2, (10, 20))
def showResult(data, data1 = None, data2 = None, data3 = None, data4 = None, data5 = None, nscan = 1):
#index = 0
total = np.zeros(data.shape[0]*data.shape[1])
dataList = [data.reshape(data.shape[0]*data.shape[1])]
if data1 is not None:
dataList.append(data1.reshape(data1.shape[0]*data1.shape[1]))
if data2 is not None:
dataList.append(data2.reshape(data2.shape[0]*data2.shape[1]))
if data3 is not None:
dataList.append(data3.reshape(data3.shape[0]*data3.shape[1]))
if data4 is not None:
dataList.append(data4.reshape(data4.shape[0]*data4.shape[1]))
if data5 is not None:
dataList.append(data5.reshape(data5.shape[0]*data5.shape[1]))
#total = copy.copy(data)
for i in range(nscan):
total += dataList[i]
fig = plt.figure(figsize = (8, 10))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.imshow(total.reshape(data.shape[0], data.shape[1]), cmap= 'gray', interpolation= 'nearest')
#plt.title('Image')
ax2.hist(total)
#plt.title('Histogram')
plt.show()
return total
showResult(randomSet, randomSet1, randomSet, randomSet3, randomSet, randomSet2, nscan= 6)
Output should be:
array([ 1., 2., 5., 4., 4., 2., 4., 3., 2., 5., 0., 3., 5.,
6., 2., 5., 5., 5., 0., 0., 0., 2., 2., 1., 2., 0.,
4., 0., 5., 4., 4., 4., 1., 6., 2., 1., 3., 1., 4.,
1., 2., 4., 1., 3., 5., 3., 1., 5., 2., 4., 4., 1.,
1., 3., 1., 6., 3., 5., 5., 1., 3., 5., 4., 1., 4.,
3., 5., 5., 4., 5., 2., 1., 4., 1., 2., 1., 6., 3.,
2., 4., 5., 1., 1., 2., 5., 3., 2., 5., 3., 2., 3.,
3., 4., 1., 4., 2., 5., 2., 4., 5., 5., 5., 1., 4.,
5., 0., 4., 1., 5., 1., 5., 2., 2., 2., 1., 3., 1.,
1., 3., 1., 3., 3., 5., 5., 5., 2., 2., 1., 4., 5.,
2., 5., 2., 3., 2., 0., 0., 5., 5., 5., 2., 2., 1.,
1., 4., 4., 4., 2., 5., 2., 4., 5., 4., 2., 2., 1.,
4., 4., 2., 4., 4., 1., 4., 3., 5., 0., 1., 2., 3.,
0., 5., 3., 2., 2., 2., 4., 4., 2., 4., 0., 5., 5.,
2., 3., 0., 1., 1., 5., 3., 1., 3., 5., 1., 2., 3.,
5., 5., 2., 2., 5.])
Output plots
You don't need to hardcore each dataset individually. You can simply call np.random.randint(low, high, (x, y, n)), with n being the number of scans/trials. Summing them along the last axis means you'll get an array with shape (x, y). This can be done trivially with np.sum().
The way to add a title in a subplot can be found here. Overall,
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
sets = 6
data = np.random.randint(0, 2, (10, 20, sets))
def plot_data(data):
total = np.sum(data, axis=-1)
fig = plt.figure(figsize=(8, 10))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.imshow(total, cmap= 'gray', interpolation= 'nearest')
ax1.set_title('Image')
# best way to flatten a numpy array
ax2.hist(total.flatten())
ax2.set_title('Histogram')
plt.show()
plot_data(data)

np.nanmax() over all except one axis - is this the best way?

For a numpy array of dimension n, I'd like to apply np.nanmax() to n-1 dimensions producing a 1 dimensional array of maxima, ignoring all values set to np.nan.
q = np.arange(5*4*3.).reshape(3,4,5) % (42+1)
q[q%5==0] = np.nan
producing:
array([[[ nan, 1., 2., 3., 4.],
[ nan, 6., 7., 8., 9.],
[ nan, 11., 12., 13., 14.],
[ nan, 16., 17., 18., 19.]],
[[ nan, 21., 22., 23., 24.],
[ nan, 26., 27., 28., 29.],
[ nan, 31., 32., 33., 34.],
[ nan, 36., 37., 38., 39.]],
[[ nan, 41., 42., nan, 1.],
[ 2., 3., 4., nan, 6.],
[ 7., 8., 9., nan, 11.],
[ 12., 13., 14., nan, 16.]]])
If I know ahead of time that I want to use the last axis as the remaining dimension, I can use the -1 feature in .reshape() and do this:
np.nanmax(q.reshape(-1, q.shape[-1]), axis=0)
which produces the result I want:
array([ 12., 41., 42., 38., 39.])
However, suppose I don't know ahead of time to which one of the axes that I don't want to apply the maximum? Suppose I started with n=4 dimensions, and wanted it to apply to all axes except the mth axis, which could be 0, 1, 2, or 3? Would have to actually use a conditional if-elif-else ?
Is there something that would work like a hypothetical exeptaxis=m?
The axis argument of nanmax can be a tuple of axes over which the maximum is computed. In your case, you want that tuple to contain all the axes except m. Here's one way you could do that:
In [62]: x
Out[62]:
array([[[[ 4., 3., nan, nan],
[ 0., 2., 2., nan],
[ 4., 5., nan, 3.],
[ 2., 0., 3., 1.]],
[[ 2., 0., 0., 1.],
[ nan, 3., 0., nan],
[ 0., 1., nan, 2.],
[ 5., 4., 0., 1.]],
[[ 4., 0., 2., 0.],
[ 4., 0., 4., 5.],
[ 3., 4., 1., 0.],
[ 5., 3., 4., 3.]]],
[[[ 2., nan, 6., 4.],
[ 3., 1., 2., nan],
[ 5., 4., 1., 0.],
[ 2., 6., 0., nan]],
[[ 4., 1., 4., 2.],
[ nan, 1., 5., 5.],
[ 2., 0., 1., 1.],
[ 6., 3., 6., 5.]],
[[ 1., 0., 0., 1.],
[ 1., nan, 2., nan],
[ 3., 4., 0., 5.],
[ 1., 6., 2., 3.]]]])
In [63]: m = 0
In [64]: np.nanmax(x, axis=tuple(i for i in range(x.ndim) if i != m))
Out[64]: array([ 5., 6.])

Categories

Resources