This question already has answers here:
Vectorized NumPy linspace for multiple start and stop values
(4 answers)
Closed 5 years ago.
I want to do linspace to an array. Just like following:
a = np.array([2, 4, 6])
b = vectorize(np.array)(0, a, 5)
I would like something back that looks like:
b = [[0, 0.5, 1, 1.5, 2]
[0, 1, 2, 3, 4]
[0, 1.5, 3, 4.5, 6]]
This is my code:
import numpy as np
a = np.arange(1001)
c = np.vectorize(np.linspace)(0, a, 101)
print(c)
It shows that: ValueError: setting an array element with a sequence.
Is there any method to do this in numpy without for loop?
Build your own:
def vlinspace(a, b, N, endpoint=True):
a, b = np.asanyarray(a), np.asanyarray(b)
return a[..., None] + (b-a)[..., None]/(N-endpoint) * np.arange(N)
Related
I have a problem with joining several matrices. Say I have four matrices A,B,C,D. And I want to join them in a way to obtain a new one
M = A B
C D
How can I do this using python numpy?
Considering all have same dimension, here's an example:
>>> A = np.arange(4).reshape(2,2)
>>> B, C, D = A*2, (A+1), (A+2)
>>> M = np.array([[A,B],[C,D]])
>>> M
array([[[[0, 1],
[2, 3]],
[[0, 2],
[4, 6]]],
[[[1, 2],
[3, 4]],
[[2, 3],
[4, 5]]]])
Or if you want something like this:
>>> M = np.concatenate([np.concatenate([A,B],axis = 1),np.concatenate([C,D],axis = 1)],axis = 0)
>>> M
array([[0, 1, 0, 2],
[2, 3, 4, 6],
[1, 2, 2, 3],
[3, 4, 4, 5]])
Using NumPy's hstack and vstack functions, you can generate M from any A, B, C, D as long as each two number of rows and/or number of columns match. See the following example:
import numpy as np
A = np.random.rand(3, 2) # (3 x 2)
B = np.random.rand(3, 4) # (3 x 4)
C = np.random.rand(4, 3) # (4 x 3)
D = np.random.rand(4, 3) # (4 x 3)
print(A, '\n')
print(B, '\n')
print(C, '\n')
print(D, '\n')
M = np.vstack((np.hstack((A, B)), np.hstack((C, D))))
# (3 x 6) (4 x 6)
# (7 x 6)
print(M)
Output:
[[0.60220154 0.77067838]
[0.7623169 0.54727146]
[0.20570341 0.56939493]]
[[0.322524 0.35260186 0.6581785 0.55662823]
[0.32034862 0.68664386 0.96432518 0.03410233]
[0.72779584 0.6705618 0.66460412 0.104223 ]]
[[0.20194483 0.49971436 0.50618483]
[0.89040491 0.25118623 0.67831283]
[0.30631334 0.69515443 0.70941023]
[0.41324506 0.23127909 0.29241595]]
[[0.0015009 0.43205507 0.08500188]
[0.48506546 0.46448833 0.61393518]
[0.51163779 0.81914233 0.21293481]
[0.33713576 0.33953848 0.9909197 ]]
[[0.60220154 0.77067838 0.322524 0.35260186 0.6581785 0.55662823]
[0.7623169 0.54727146 0.32034862 0.68664386 0.96432518 0.03410233]
[0.20570341 0.56939493 0.72779584 0.6705618 0.66460412 0.104223 ]
[0.20194483 0.49971436 0.50618483 0.0015009 0.43205507 0.08500188]
[0.89040491 0.25118623 0.67831283 0.48506546 0.46448833 0.61393518]
[0.30631334 0.69515443 0.70941023 0.51163779 0.81914233 0.21293481]
[0.41324506 0.23127909 0.29241595 0.33713576 0.33953848 0.9909197 ]]
You can switch vstack and hstack, if the number of columns of A/C and B/D match, and the number of rows of these concatenations are equal.
Hope that helps!
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]
This question already has answers here:
Numpy np.multiply vs *-Operator [duplicate]
(2 answers)
Closed 4 years ago.
The numpy.multiply documentation says:
Equivalent to x1 * x2 in terms of array broadcasting.
Is np.multiply(x1, x2) different to x1 * x2 in any circumstance?
Where would I find the implementations of each?
Note: An analogous question exists for division but it doesn't mention multiplication, nor imply that the the answer would be the same in the multiplicative case.
This question also asks for implementation details specific to multiplication.
Supplementing #COLDSPEED's answer I'd like to stress that for non array operands results can actually be quite different:
>>> import numpy as np
>>>
>>> 2 * [1, 2]
[1, 2, 1, 2]
>>> np.multiply(2, [1, 2])
array([2, 4])
Yes, np.multiply and the multiplication operator * work consistently for ndarray objects.
In [560]: x = np.array([1, 2, 3])
In [561]: y = np.array([4, 5, 6])
In [562]: x * y
Out[562]: array([ 4, 10, 18])
In [563]: np.multiply(x, y)
Out[563]: array([ 4, 10, 18])
The only major difference is in terms of matrix objects, for which, the * is setup to perform matrix multiplication (i.e., the dot product).
In [564]: x, y = map(np.matrix, (x, y))
In [565]: np.multiply(x, y)
Out[565]: matrix([[ 4, 10, 18]])
In [566]: x * y
ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
Also, as #PaulPanzer mentioned in his answer, they behave differently when multiplying pure-python lists with scalars.
This question already has answers here:
How to conditionally combine two numpy arrays of the same shape
(2 answers)
Closed 5 years ago.
I am using numpy and I'm trying to compare between 2 arrays and get the higher value between them into a new array
arr1= array([1,2,3,4])
arr2= array([6,0,2,4])
newarr = array([6,2,3,4])
is there any way to do that
Yes, np.maximum:
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([6, 0, 2, 4])
newarr = np.maximum(arr1, arr2)
print(newarr)
>>> [6 2 3 4]
This question already has an answer here:
Numpy error in Python
(1 answer)
Closed 6 years ago.
What I am trying to do is make a table based on a piece-wise function in Python. For example, say I wrote this code:
import numpy as np
from astropy.table import Table, Column
from astropy.io import ascii
x = np.array([1, 2, 3, 4, 5])
y = x * 2
data = Table([x, y], names = ['x', 'y'])
ascii.write(data, "xytable.dat")
xytable = ascii.read("xytable.dat")
print xytable
This works as expected, it prints a table that has x values 1 through 5 and y values 2, 4, 6, 8, 10.
But, what if I instead want y to be x * 2 only if x is 3 or less, and y to be x + 2 otherwise?
If I add:
if x > 3:
y = x + 2
it says:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
How do I code my table so that it works as a piece-wise function? How do I compare scalars to Numpy arrays?
You can possibly use numpy.where():
In [196]: y = np.where(x > 3, x + 2, y)
In [197]: y
Out[197]: array([2, 4, 6, 6, 7])
The code above gets the job done in a fully vectorized manner. This approach is generally more efficient (and arguably more elegant) than using list comprehensions and type conversions.
Start off not using numpy (or maybe you can, I don't know numpy) and just do in using regular python lists.
x = [ 1, 2, 3, 4, 5 ]
y = [ i * 2 if i < 3 else i + 2 for i in x ]
print y
Outputs:
[2, 4, 5, 6, 7]
Then you can make it a numpy array:
x = np.array(x)
y = np.array(y)