Python compare rows and columns of a table [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 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.

Related

need to make this into a recursive function [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 1 year ago.
Improve this question
i need to convert this over to a recursive function. the program basically prints out all variations of a string.
def comb(L):
for i in range(3):
for j in range(3):
for k in range(3):
# check if the indexes are not
# same
if (i!=j and j!=k and i!=k):
print(L[i], L[j], L[k])
# Driver Code
comb([1, 2, 3])
output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
This might get you started:
Every permutation of a list can be made by picking a value from the list & putting it at the front of every permutation of what is left in the list after taking that value out. (Note that the "what is left" part is a smaller list than what you started with.)
When your list is small enough, it is the only permutation.

Finding all possible combinations of sum product to reach a given target [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 1 year ago.
Improve this question
There has been a solution to find the possible combination of numbers to reach a given target number. However, I have a different situation below, where a,b, and c are product types and I like to find the combination of sum products of a,b and c to reach the target total.
a = 50sqft
b = 70sqft
c = 100sqft
Total = 5000sqft
I like to find all possible combinations of numbers (integer solution) of a,b,c to get to 5000, and how can I create a python function for that?
Results :
(100a,0b,0c)=5000
(23a,5b,8c)=5000
...
...
Thanks in advance!!
I got a solution :
a=50
b=70
c=100
for i in range(101): # This si 101 here to give 100a=5000
for j in range(100):
for k in range(100):
if i*a + j*b + k*c == 5000:
print('({}a,{}b,{}c)=5000'.format(i,j,k))

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]])

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]

How do I return a list of numbers below a certain threshold without using complex list functions? [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 4 years ago.
Improve this question
I can't figure out how to do this without using complex functions, please help. this is the docstring of the code:
'''
finds all numbers in the list below a certain threshold
:param numList: a list of numbers
:threshold: the cutoff (only numbers below this will be included)
:returns: a new list of all numbers from numList below the threshold
'''
One approach
def filterList(numList, threshold):
return list(filter(lambda x: x < threshold, numList))
Another approach:
filteredList = [x for x in numList if x < threshold]

Categories