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)
Related
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 4 months ago.
Improve this question
I was trying to create this [80000, 104000, 135000...] list in Python. Its the value, starting at 80,000 multiplied by 1.3 each time I want
What i've tried:
a = [num*1.5 for num in ??? if num>=80000] #???--> i've tried range(10)
I should be able to do this but I can't find any solutions rn..
I must use list-comprehensions, if possible.
Some help would be nice, thank you!
There is a very basic mathematical operation that represents multiplying by the same value many time: power.
a = [80000 * (1.3**n) for n in range(100)]
You could write your own generator then use that in conjunction with a list comprehension.
def numgen(start, factor, limit):
for _ in range(limit):
yield int(start)
start *= factor
mylist = [value for value in numgen(80_000, 1.3, 10)]
print(mylist)
Output:
[80000, 104000, 135200, 175760, 228488, 297034, 386144, 501988, 652584, 848359]
import numpy as np
print(80000 * 1.3**np.arange(3))
# [ 80000. 104000. 135200.]
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]])
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.
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]
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 4 years ago.
Improve this question
For generating a probability density function of some cases, maybe 1 million observations are considered. When I work with numpy array, I was encountered by size limit 32.
Is it too few ?
In this case, how can we store more than 32 elements without distributing the elements into different columns and maybe arrays in arrays ?
import numpy
my_list = []
for i in range(0, 100):
my_list.append(i)
np_arr = numpy.ndarray(np_arr) # ValueError: sequence too large; cannot be greater than 32
When you create an array with numpy.ndarray, the first argument is the shape of the array. Interpreting that list as a shape would indeed give a huge array. If you just want to turn the list into an array, you want numpy.array:
import numpy
my_list = []
for i in range(0, 100):
my_list.append(i)
np_arr = numpy.array(my_list)