Matrix addition: combinations of elements that sum up to a specific value - python

using Python,
I have two matrices A and B of size 100,000x25 and a vector C of size 25.
I know for loops are slow so I want an efficient way of adding every combination of numbers from A with B and C and collecting all indices of those elements that sum greater than 10.
Small Example for demonstrating
_____
A =
[1, 2 ]
[5, 8 ]
B =
[3, 6]
[4, 2]
C= [3,4,7,1]
In the above example, A[0,1]+B[0,0]+C[0] = 5+3+3 =11 which is what I am trying to find I.e all combinations that sum upto a specific value.
I tried for loops but takes way long. And my estimate is almost 2 days for the matrices that I have. Any suggestions?

Related

Finding the count of divisors of each array item within the array

I have an array like: [2,4,8,2]. I want to find the divisor of each item in the list but the divisors can only be elements of the array.
2 is divisible by [2,2] so the asked number is 2
4 is divisible by [2,4,2] so the asked number is 3
8 is divisible by [2,4,8,2] so the asked number is 4
2 is divisible by [2,2] so the asked number is 2
I have a solution where I utilized double for-loop but I believe there needs to be a more optimal solution. Any ideas?
Thanks!
You can't avoid the double loop but you can optimize them and combine them in a list comprehension:
from collections import Counter
L = [2,4,8,2]
counts = Counter(L)
R = [ sum(c for d,c in counts.items() if n%d==0) for n in L ]
print(R)
[2, 3, 4, 2]

Iterate through upper triangular matrix in Python

I am using Python and I have a XxY matrix where X=Y and I want to iterate over the upper triangular matrix in a specific way such that it starts with and proceeds with and and so on and so forth until the last row and column. Therefore, I tried to create a double loop which loops over the columns one by one and within that loop I created another loop which loops over the rows always adding one row. However, I got stuck in defining how to add the next row for every column in the second loop. Here is what I got so far (for simplicity I just created an array of zeros):
import pandas as pd
import numpy as np
# number of columns
X = 10
# number or rows
Y = X
U = np.zeros((Y,X))
for j in range(X):
for z in range():
My initial idea was to create an array of Yx1 with y = np.asarray(list(range(0,Y)))and use it for the second loop but I don't understand how to implement it. Can somebody please help me? Is there maybe a simpler way to define such an iteration?
With Numpy, you can get the indices for the upper triangular matrix with triu_indices_from and index into the array with that:
import numpy as np
a = np.arange(16).reshape([4, 4])
print(a)
#[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]]
indices = np.triu_indices_from(a)
upper = a[indices]
print(upper)
# [ 0 1 2 3 5 6 7 10 11 15]

Assign values from small matrix to specified places in larger matrix

I would like to know if there exists a similar way of doing this (Mathematica) in Python:
Mathematica
I have tried it in Python and it does not work. I have also tried it with numpy.put() or with simple 2 for loops. This 2 ways work properly but I find them very time consuming with larger matrices (3000×3000 elements for example).
Described problem in Python,
import numpy as np
a = np.arange(0, 25, 1).reshape(5, 5)
b = np.arange(100, 500, 100).reshape(2, 2)
p = np.array([0, 3])
a[p][:, p] = b
which outputs non-changed matrix a: Python
Perhaps you are looking for this:
a[p[...,None], p] = b
Array a after the above assignment looks like this:
[[100 1 2 200 4]
[ 5 6 7 8 9]
[ 10 11 12 13 14]
[300 16 17 400 19]
[ 20 21 22 23 24]]
As documented in Integer Array Indexing, the two integer index arrays will be broadcasted together, and iterated together, which effectively indexes the locations a[0,0], a[0,3], a[3,0], and a[3,3]. The assignment statement would then perform an element-wise assignment at these locations of a, using the respective element-values from RHS.

Python NumPy shape

Can someone please explain this code to me
case = np.array([[1,2], [2,4], [3,5]])
I understand the above gives 2 columns and 3 rows.
But the code below I don't understand. Please help me to understand it.
np.arange(0, case.shape[0]+4)
np.arange() returns evenly spaced values within a given interval.
In this case, since case.shape[0] is the first axis of the array, which has 3 arrays in it, the range goes from 0 to 3+4=7 (end not included).
case = np.array([[1,2], [2,4], [3,5]])
case
array([[1, 2],
[2, 4],
[3, 5]])
Numpy.arange will provide a series of numbers starts from 0 to case.shape[0] +4 . Here case.shape is (3,2) (Three Rows and Two Columns) . So Case[0] will be 3 and case[1] will be 2 . So np.arrange will be a series of numbers from 0 to 3+4 = 7 where 0 is included and 7 is excluded and output will be 0,1,2,3,4,5,6

Best way to find the maximum sum of multiple arrays given constraints on index

Say I have 3 sorted arrays each of length 4, and I want to choose an index from each array such that the sum of the indexes are equal to 4. How would I find the maximum possible sum without testing all possible choices?
For instance I have the following arrays
1 : [0,0,0,8]
2 : [1,4,5,6]
3 : [1,5,5,5]
Then the solution would be 3,0,1. Because 3 + 0 + 1 = 4 and 8 + 1 + 5 is
the maximum combination where the sum of the indexes are 4.
I need a solution that can be generalized to n arrays of size m where the sum of the indexes could equal anything.
For instance, it could be asked that this be solved with 1000 arrays all of size 1000 where the sum of the index is 2000.
If there is a python package somewhere that does this please let me know.
This will achieve it , no sure the speed is meet your requirement
df1=pd.DataFrame([[0,0,0,8],[1,4,5,6],[1,5,5,5]])
import functools
df=pd.DataFrame(list(itertools.product([0,1,2,3],[0,1,2,3],[0,1,2,3])))
df=df.loc[df.sum(1)<=4,:]
df.index=df.apply(tuple,1)
df.apply(lambda x : df1.lookup(df.columns.tolist(),list(x.name)),1).sum(1).idxmax()
Out[751]: (3, 0, 1)
df.apply(lambda x : df1.lookup(df.columns.tolist(),list(x.name)),1).sum(1).max()
Out[752]: 14

Categories

Resources