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]
Related
This question already has answers here:
Find elements of array one nearest to elements of array two
(1 answer)
Find nearest indices for one array against all values in another array - Python / NumPy
(2 answers)
Closed 3 years ago.
Is there a numpy-thonic way to get the nearest values between two arrays:
Example:
a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
b = np.array([1.1, 3.2, 4.9, 7.2])
c = find_nearests(a, b)
c would be the size of b with the nearest elements from a:
print(c)
([1, 3, 5, 7])
This question is a direct reference to the following one that applies for searching one element in a tab: Find nearest value in numpy array
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 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)
This question already has answers here:
Retrieving array elements with an array of frequencies in NumPy
(3 answers)
Closed 8 years ago.
Suppose I want to generate a 1D array like this:
1 1 1 1 2 2 2 3 3 4
In general I am looking for something with this form:
Element N-repetition
1 n-0
2 n-1
3 n-2
4 n-3
. .
. .
. .
n n-(n-1)=1
This is of course possible by combining arrays of
sizes n, n-1, n-2, ..., but I am wondering if there is
a better, vectorized way of doing this?
Its very simple with Numpy's repeat:
n = 4
a = np.arange(1,n+1)
The array a looks like:
array([1, 2, 3, 4])
And you basically want to repeat it with the reverse of a, so:
np.repeat(a, a[::-1])
Gives:
array([1, 1, 1, 1, 2, 2, 2, 3, 3, 4])
I came up with something myself, but it's a little complicated:
def makegenarr(n):
def genarr(x):
return np.repeat(x, n-(x-1))
return(genarr)
x = np.arange(1, 5)
mapfunc = makegenarr(x.shape[0])
np.apply_along_axis(genarr, 0, x)
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)