Find nearest values in numpy array [duplicate] - python

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

Related

Making column that aggregates the values of another column w/ Python [duplicate]

This question already has an answer here:
Cumsum as a new column in an existing Pandas data
(1 answer)
Closed 2 years ago.
Let sat I have a DataFrame with column A.
A= (1,2,3,4,5,6...n)
I want to create column B like this:
B=(1,3,6,10,15,21...n)
Explicitly: i+(sum of all the previous numbers)
Probably simple, but hard for me:P Very new to programming
Thanks!
from itertools import accumulate
A = [1, 2, 3, 4, 5, 6]
B = list(accumulate(A)) #->[1, 3, 6, 10, 15, 21]

How to get the product of two matrix? [duplicate]

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]

Get ascending order of numbers in array in python [duplicate]

This question already has answers here:
Translate integers in a numpy array to a contiguous range 0...n
(2 answers)
Closed 4 years ago.
I have a Numpy array with some numbers and I would like to get order the items ascending order.
For example, I have a list:
[4, 25, 100, 4, 50]
And I would like to use a function to get this:
[1, 2, 4, 1, 3]
Any ideas how to do this?
There is a convenient method via pandas:
import pandas as pd
lst = [4, 25, 100, 4, 50]
res = pd.factorize(lst, sort=True)[0] + 1
# [1 2 4 1 3]

making a new array out of 2 arrays with the higher value inside [duplicate]

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]

How to delete repeated elements from array keeping the order unchanged in python? [duplicate]

This question already has answers here:
numpy.unique with order preserved
(7 answers)
Closed 6 years ago.
I have sequence of integer elements in array "a" ,given below
a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
I need the output like
output=[2,1,5,4,8,6,87,62,3]
I tried the built-in functions like set or unique but it arrange the
resulting sequence in ascending order, I want to keep the order unchanged.
Can anyone help?
You can use sorted with key=list.index
>>> a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
>>> new_a = sorted(set(a), key=a.index)
>>> new_a
[2, 1, 5, 4, 8, 6, 87, 62, 3]
a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
b = []
You can use
[b.append(x) for x in a if x not in b]
or easier to read
for x in a:
if x not in b:
b.append(x)
>>> [2, 1, 5, 4, 8, 6, 87, 62, 3]`

Categories