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

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]

Related

I am a python student, and I have a problem [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 1 year ago.
Improve this question
def thing():
_list = [1,1,5,6,8,3,4,6,10,23,5,12,67,3,25,2,6,5,4,3,2,1]
_list1 = [str(i) for i<=5 in lista]
return " ".join(_list1)
print(thing()))
I am new to this type of list managment, I am trying to put in _list1 only integers that are less then 5
so like the name of your function, it's created to calculate the sum of integers.
So basically, if your n=0 then your program will return 0 as result.
If not (else), it gonna give you a result of the calculation in return.
And here you want to print the interger sum of 451 which will give you the result of n % 10 + integer_sum(int(n / 10))

Get maximum subset in multidimensional array [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
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]])

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.

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)

Python compare rows and columns of a table [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 8 years ago.
Improve this question
I am looking for an efficient way to compare rows and columns of a table against each other (>= gets 1, otherwise 0) and store the result.
Example:
0.3642286 0.7945753 0.3527125
0.3642286 1 1 0
0.7945753 0 1 0
0.3527125 1 1 1
I have 21 tables with 480*480 rows and columns. What would be a proper way of generating and storing such a matrix?
All you really need is two loops.
def compare(first, second):
result = []
for x in first:
result.append([])
for y in second:
result[-1].append(1 if x >= y else 0)
result = [list(i) for i in zip(*result)]
return result
You might consider NumPy (1) if you are regularly handling large multi-dimensional arrays.

Categories