A newbie question and possible duplicate: How can one compose a matrix in numpy using arrays or 1d matrices? In matlab, I would use following syntax for the matrix consisting of three arrays treated as rows:
A=[1; 1; 1];
B=[2; 2; 2];
C=[3; 3; 3];
D=[A B C]
The result is:
D =
1 2 3
1 2 3
1 2 3
Thank you
You should do
import numpy as np
A = np.array([1, 1, 1])
B = np.array([2, 2, 2])
C = np.array([3, 3, 3])
D = np.vstack((A, B, C))
See NumPy for MATLAB users (official link seems to be down)
Related
Suppose I have the following numpy array.
A = [1,2,3,4,5,6]
Question Is there a quick way to multiply or add every nth element in A to yield the following arrays?
B = [3*1, 2*4, 3*5, 4*6]
C = [3+1, 2+4, 3+5, 4+6]
I can accomplish this by rolling A and then adding or multiplying it to the original A.
T = np.roll(A,-2)
B = (A*T)[0:4]
C = (A*T)[0:4]
Just wondering if there is a more pythonic/efficient way to accomplish this? I have also looked at np.add.reduceat and np.multiply.reduceat but they do not seem to allow for skipping values.
You can do it in the following way:
A = np.array([1, 2, 3, 4, 5, 6])
k = 2
B = A[:-k]
C = A[k:]
print(B * C)
print(B + C)
Output
[ 3 8 15 24]
[ 4 6 8 10]
Cheers.
I'm trying to make a clean connection between the dimensions in a numpy array and the dimensions of a matrix via classical linear algebra. Suppose the following:
In [1] import numpy as np
In [2] rand = np.random.RandomState(42)
In [3] a = rand.rand(3,2)
In [4] a
Out[4]:
array([[0.61185289, 0.13949386],
[0.29214465, 0.36636184],
[0.45606998, 0.78517596]])
In [5]: a[np.newaxis,:,:]
Out[5]:
array([[[0.61185289, 0.13949386],
[0.29214465, 0.36636184],
[0.45606998, 0.78517596]]])
In [6]: a[:,np.newaxis,:]
Out[6]:
array([[[0.61185289, 0.13949386]],
[[0.29214465, 0.36636184]],
[[0.45606998, 0.78517596]]])
In [7]: a[:,:,np.newaxis]
Out[7]:
array([[[0.61185289],
[0.13949386]],
[[0.29214465],
[0.36636184]],
[[0.45606998],
[0.78517596]]])
My questions are as follows:
Is is correct to say that the dimensions of a are 3 X 2? In other words, a 3 X 2 matrix?
Is it correct to say that the dimensions of a[np.newaxis,:,:] are 1 X 3 X 2? In other words, a matrix containing a 3 X 2 matrix?
Is it correct to say that the dimensions of a[:,np.newaxis,:] are 3 X 1 X 2? In other words a matrix containing 3 1 X 2 matrices?
Is it correct to say that the dimensions of a[:,:,np.newaxis] are 3 X 2 X1? In other words a matrix containing 3 matrices each of which contain 2 1 X 1 matrices?
yes
yes
yes
three 2x1 matrices each of which contains one vector of size 1
Just find out using .shape:
import numpy as np
rand = np.random.RandomState(42)
# 1.
a = rand.rand(3, 2)
print(a.shape, a, sep='\n', end='\n\n')
# 2.
b = a[np.newaxis, :, :]
print(b.shape, b, sep='\n', end='\n\n')
# 3.
c = a[:, np.newaxis, :]
print(c.shape, c, sep='\n', end='\n\n')
# 4.a
d = a[:, :, np.newaxis]
print(d.shape, d, sep='\n', end='\n\n')
# 4.b
print(d[0].shape, d[0], sep='\n', end='\n\n')
print(d[0, 0].shape, d[0, 0])
output:
(3, 2)
[[0.37454012 0.95071431]
[0.73199394 0.59865848]
[0.15601864 0.15599452]]
(1, 3, 2)
[[[0.37454012 0.95071431]
[0.73199394 0.59865848]
[0.15601864 0.15599452]]]
(3, 1, 2)
[[[0.37454012 0.95071431]]
[[0.73199394 0.59865848]]
[[0.15601864 0.15599452]]]
(3, 2, 1)
[[[0.37454012]
[0.95071431]]
[[0.73199394]
[0.59865848]]
[[0.15601864]
[0.15599452]]]
(2, 1)
[[0.37454012]
[0.95071431]]
(1,) [0.37454012]
I have created this array(or I think its a list) that consist of many arrays that are different size and that is the reason I put dtype = object .
m = [data[a:b] for a, b in zip(z[0:-1:2], z[1:-1:2])]
array = np.array(m, dtype=object)
I need to pad each array with zero so that they have the same size (lets say size=smax) and become a "proper" array. My definitions are a little off and I am sorry in advance
You can do this using np.pad on each row. For example:
import numpy as np
data = np.arange(10)
z = [0, 2, 1, 4, 6, 10, 8, 9]
m = [data[a:b] for a, b in zip(z[0:-1:2], z[1:-1:2])]
max_length = max(len(row) for row in m)
result = np.array([np.pad(row, (0, max_length-len(row))) for row in m])
print(result)
# [[0 1 0 0]
# [1 2 3 0]
# [6 7 8 9]]
As title, I have Matlab
op = [1 3 5]
[op ;op+1]
opf = sort([op ;op+1])
opf = [1 2 3 4 5 6]
and reading the doc, I've discovered that ; could signify end of row. However I don't know if that's the case since it's in square brackets and in Matlab more often than not there are way too many ways of doing most of the things. Not that it's something bad to be honest, but it's at least a bit confusing for students like me.
To replicate in Python I did
opf = np.sort([op,op+1])
but opf shape is wrong. I get in fact [[0 2 4] [1 3 5]] which is (2,3). opf instead should be [1 2 3 4 5 6] and its shape accordingly (6,1)
This is possible in Python using regular lists:
import numpy as np
op = np.array([1, 3, 5])
opf = [[i, i+1] for i in op]
opf = [i for j in opf for i in j]
print(np.asarray(opf))
Returning:
[1 2 3 4 5 6]
If your op array is unordered, you could do:
import numpy as np
op = np.array([1, 5, 3])
opf = [[i, i+1] for i in sorted(op)]
opf = [i for j in opf for i in j]
print(np.sort(np.asarray(opf)))
Again returning:
[1 2 3 4 5 6]
When working with numpy arrays, you cannot concatenate like you did on matlab.
The way to do it, is via np.concatenate(). Also, np.concatenate() takes in a tuple as its argument, so you must use double parenthesis (one to indicate the function, and the other for the tuple of the arrays you want to concatenate).
Your complete example would look something like this
import numpy as np
a = [1, 3, 5]
op = np.array(a)
result = np.sort(np.concatenate((op, op + 1)))
print(result)
Try:
np.reshape(6,1)
If you are using numpy you can reshape it yourself
This question already has answers here:
How to get element-wise matrix multiplication (Hadamard product) in numpy?
(5 answers)
Closed 4 years ago.
I Have this code:
import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
And I would like to get from a and b this matrix:
c = np.array([4,10,18])
I mean
c = np.array([a0*b0, a1*b1, a2*b2])
without a for loop. How can I do this?
numpy arrays support vectorized operators, so a * b will return your required array.
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a*b)
# [ 4 10 18]