numpy

pandas: Group by splitting string value in all rows (a column) and aggregation function

最后都变了- 提交于 2021-02-08 13:49:19
问题 If i have dataset like this: id person_name salary 0 [alexander, william, smith] 45000 1 [smith, robert, gates] 65000 2 [bob, alexander] 56000 3 [robert, william] 80000 4 [alexander, gates] 70000 If we sum that salary column then we will get 316000 I really want to know how much person who named 'alexander, smith, etc' (in distinct) makes in salary if we sum all of the salaries from its splitting name in this dataset (that contains same string value). output: group sum_salary alexander 171000

Can numpy einsum() perform outer addition?

∥☆過路亽.° 提交于 2021-02-08 13:49:17
问题 In numpy, we can perform "outer addition" between two vectors a and b like this: a=np.c_[1,2,3] b=np.c_[4,5,6] result=a+b.T # alternatively this can be a.T+b Is it possible to use einsum to make the same calculation? Any other fast alternatives? How about if a equals b ? 回答1: Another fast alternative to this operation is to use: np.add.outer(a,b) 来源: https://stackoverflow.com/questions/17602796/can-numpy-einsum-perform-outer-addition

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 3)

戏子无情 提交于 2021-02-08 13:14:09
问题 I have written code using matmul, but I am getting the following error: "ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 3)" Code: R = [[0.40348195], [0.38658295], [0.82931052]] V = [0.33452744, 0.33823673, 0.32723583] print("Rt_p: ", R) B = np.matmul(V,np.transpose(R))/pow(LA.norm(R), 2) print("B", B) 回答1: You are transposing a Matrix with 3 rows and 1 column to a Matrix with 3 columns and 1

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 3)

只谈情不闲聊 提交于 2021-02-08 13:13:58
问题 I have written code using matmul, but I am getting the following error: "ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 3)" Code: R = [[0.40348195], [0.38658295], [0.82931052]] V = [0.33452744, 0.33823673, 0.32723583] print("Rt_p: ", R) B = np.matmul(V,np.transpose(R))/pow(LA.norm(R), 2) print("B", B) 回答1: You are transposing a Matrix with 3 rows and 1 column to a Matrix with 3 columns and 1

Operation on numpy arrays contain rows with different size

寵の児 提交于 2021-02-08 13:12:48
问题 I have two lists, looking like this: a= [[1,2,3,4], [2,3,4,5],[3,4,5,6,7]], b= [[5,6,7,8], [9,1,2,3], [4,5,6,7,8]] which I want to subtract from each other element by element for an Output like this: a-b= [[-4,-4,-4,-4],[7,2,2,2],[-1,-1,-1,-1,-1]] In order to do so I convert each of a and b to arrays and subtract them I use: np.array(a)-np.array(b) The Output just gives me the error: Unsupported Operand type for-: 'list' and 'list' What am I doing wrong? Shouldn't the np.array command ensure

Operation on numpy arrays contain rows with different size

吃可爱长大的小学妹 提交于 2021-02-08 13:12:45
问题 I have two lists, looking like this: a= [[1,2,3,4], [2,3,4,5],[3,4,5,6,7]], b= [[5,6,7,8], [9,1,2,3], [4,5,6,7,8]] which I want to subtract from each other element by element for an Output like this: a-b= [[-4,-4,-4,-4],[7,2,2,2],[-1,-1,-1,-1,-1]] In order to do so I convert each of a and b to arrays and subtract them I use: np.array(a)-np.array(b) The Output just gives me the error: Unsupported Operand type for-: 'list' and 'list' What am I doing wrong? Shouldn't the np.array command ensure

Add value to every “other” field ((i+j)%2==0) of numpy array

半世苍凉 提交于 2021-02-08 12:53:07
问题 I have an m -by- n numpy array, and I'd like to add 1.0 to all entries [i, j] when (i + j) % 2 == 0 , i.e., "to every other square". I could of course simply iterate over the fields import numpy as np a = np.random.rand(5, 4) for i in range(a.shape[0]): for j in range(a.shape[1]): if (i + j) % 2 == 0: a[i, j] += 1.0 but needless to say this is really slow. Any idea of how to improve on this? 回答1: You can easily do the operation in two steps, like import numpy as np a = np.zeros((5, 14)) #

Add value to every “other” field ((i+j)%2==0) of numpy array

99封情书 提交于 2021-02-08 12:53:06
问题 I have an m -by- n numpy array, and I'd like to add 1.0 to all entries [i, j] when (i + j) % 2 == 0 , i.e., "to every other square". I could of course simply iterate over the fields import numpy as np a = np.random.rand(5, 4) for i in range(a.shape[0]): for j in range(a.shape[1]): if (i + j) % 2 == 0: a[i, j] += 1.0 but needless to say this is really slow. Any idea of how to improve on this? 回答1: You can easily do the operation in two steps, like import numpy as np a = np.zeros((5, 14)) #

How to add a scalar to a numpy array within a specific range?

♀尐吖头ヾ 提交于 2021-02-08 12:23:46
问题 Is there a simpler and more memory efficient way to do the following in numpy alone. import numpy as np ar = np.array(a[l:r]) ar += c a = a[0:l] + ar.tolist() + a[r:] It may look primitive but it involves obtaining a subarray copy of the given array, then prepare two more copies of the same to append in left and right direction in addition to the scalar add. I was hoping to find some more optimized way of doing this. I would like a solution that is completely in Python list or NumPy array,

Pythonic and efficient way to do an elementwise “in” using numpy

只谈情不闲聊 提交于 2021-02-08 12:23:33
问题 I'm looking for a way to efficiently get an array of booleans, where given two arrays with equal size a and b , each element is true if the corresponding element of a appears in the corresponding element of b . For example, the following program: a = numpy.array([1, 2, 3, 4]) b = numpy.array([[1, 2, 13], [2, 8, 9], [5, 6], [7]]) print(numpy.magic_function(a, b)) Should print [True, True, False, False] Keep in mind this function should be the equivalent of [x in y for x, y in zip(a, b)] Only