Get maximum subset in multidimensional array [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Given:
a=np.array([[-0.00365169, -1.96455717, 1.44163783, 0.52460176, 2.21493637],
[-1.05303533, -0.7106505, 0.47988974, 0.73436447, -0.87708389],
[-0.76841759, 0.8405524, 0.91184575, -0.70652033, 0.37646991]])
I would like to get the maximum subset (in this case, the first row):
[-0.00365169, -1.96455717, 1.44163783, 0.52460176, 2.21493637]
By using print(np.amax(a, axis=0)), I'm getting the wrong result:
[-0.00365169 0.8405524 1.44163783 0.73436447 2.21493637]
How can we get the correct maximum subset?

You can sum along columns and then find the index with the maximum value with argmax:
a[np.argmax(a.sum(axis=1))]

If you make some change:
a=np.array([[-0.00365169, -10.96455717, 1.44163783, 0.52460176, 2.21493637],
[-1.05303533, -0.7106505, 0.47988974, 0.73436447, -0.87708389],
[-0.76841759, 0.8405524, 0.91184575, -0.70652033, 0.37646991]])
The solution will be not right:
a[np.argmax(a.sum(axis=1))]
Try this:
arr = np.where(a == np.amax(a, axis=0))[0]
counts = np.unique(arr)
ind = np.argmax(counts)
print(a[arr[ind]])

Related

Append the values if it is more than something [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I want to append the number in A to 5 if it is more than 5.
A = [1,2,3,4,5,6,7,8,9,10]
To something like this:
A = [1,2,3,4,5,5,5,5,5,5]
You can try using map
A = [1,2,3,4,5,6,7,8,9,10]
list(map(lambda x: x if x<5 else 5, A))
You can use the min function to take the smaller of each list item and 5:
[min(i, 5) for i in A]
Demo: https://replit.com/#blhsing/InsidiousDigitalIntegrationtesting

Please explain the following inline python code? index = [n for n, value in enumerate(self.Variable[i]) if value == 1] [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to understand the following code.
The code is from a class method.
Code snippet
index = [n for n, value in enumerate(self.Variable[i]) if value == 1]
The above code can be rewritten as:
indices = []
for n, value in enumerate(self.BUSES[i]):
if value==1:
indices.append(n)
enumerate returns a pair of (index, value at that index) for a given list. So you are testing if value at a given index is 1, and if that is true, you add the index to indices.

Adding elements in arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm facing a problem.
I have two elements as follow :
[array([130.05297852, 159.25004578, 140.36545944]),
array([115.27301025, 160.63392258, 132.83247375])]
and
[39.44091796875,
52.175140380859375]
and I would like to have something like that :
[array([130.05297852, 159.25004578, 140.36545944, 39.44091796875]),
array([115.27301025, 160.63392258, 132.83247375, 52.175140380859375])]
How can I manage to do this ? Thanks !
You can append elements with the append function.
for i in range(len(small_array)):
bigger_array[i].append(small_array[i])
this appends the first element to the first array, and the second element to the second array.
EDIT:
with numpy arrays you can adapt the previous method in this way:
for i in range(len(small_array):
np.append(bigger_array[i], small_array[i])
import numpy as np
a = [
np.array([130.05297852, 159.25004578, 140.36545944]),
np.array([115.27301025, 160.63392258, 132.83247375])
]
add_to_a = np.array([39.44091796875, 52.175140380859375])
result = []
for i, j in zip(a, add_to_a):
final = np.append(i, j)
result.append(final)
print(result) # If you need a normal array
result = np.array(result) # Making ND Array
print(result)

Convert Array of Symbols to Array of 1 and 0 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Suppose we have an array/string of stock symbols: ['AMD','AMZN','BABA','FB'].
I need to be able to convert the supplied stock symbol to 1 and others to 0.
For example if we supplied 'AMZN' to the array above the resulting array should look: [0,1,0,0]. If 'FB' result should look like [0,0,0,1].
I need to feed it into an AI algorithm.
def get_binary_array(input_array, stock_ticker):
return [1 for thing in input_array if thing == stock_ticker else 0]
This is probably what you are looking for:
arr = ['AMD','AMZN','BABA','FB']
value = 'AMD'
one_hot = [int(value==i) for i in arr]

max() function on list with an integrer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a list composed of integers, and I would like to do this :
freeSushi = max(sushiPrices <= sushiPrice)
sushiPrice being an integer and sushiPrices being a list.
Any idea of how I can do this ?
You can use filter before applying max
max(filter(lambda price: price <= sushiPrice, sushiPrices)
This would work, if "something" was a list of prices less than or equal to sushiPrice:
freeSushi = max(something)
So how can we create a list of prices less than sushiPrice? Comprehend?
Use a for loop like so:
for i in sushiPrices:
if i > sushiPrice:
del sushiPrices[sushiPrices.index(i)]
freeSushi = max(sushiPrices)

Categories