numpy

Which numpy operations copy and which mutate?

社会主义新天地 提交于 2021-02-09 09:47:22
问题 Is there a general rule of thumb for knowing which operations on a numpy.ndarray produce a copy of the values and which mutate them in-place? I'm pretty new to numpy and I'm sure I'll learn the hard way eventually, but I was wondering if there were general principles driving mutability that might help speed my learning. 回答1: Functions that mutate in place Relatively few numpy functions mutate in place. For the most part, numpy functions return array views when they can, and copies when they

Which numpy operations copy and which mutate?

久未见 提交于 2021-02-09 09:45:43
问题 Is there a general rule of thumb for knowing which operations on a numpy.ndarray produce a copy of the values and which mutate them in-place? I'm pretty new to numpy and I'm sure I'll learn the hard way eventually, but I was wondering if there were general principles driving mutability that might help speed my learning. 回答1: Functions that mutate in place Relatively few numpy functions mutate in place. For the most part, numpy functions return array views when they can, and copies when they

python3数字、日期和时间

隐身守侯 提交于 2021-02-09 09:01:17
1、对数值进行取整 # 使用内建的round(value,ndigits)函数来取整,ndigits指定保留的位数,在取整时会取值在偶数上,如1.25取一位会取整1.2,1.26会取整1.3 In [1]: round(1.23,1 ) Out[ 1]: 1.2 In [ 2]: round(1.25,1 ) Out[ 2]: 1.2 In [ 3]: round(1.26,1 ) Out[ 3]: 1.3 In [ 4]: round(1.2645,3 ) Out[ 4]: 1.264 # 如果参数ndigits为负数的话会相应的取整到十位、白位和千位 In [1]: a = 1234567 In [ 2]: round(a,-1 ) Out[ 2]: 1234570 In [ 3]: round(a,-3 ) Out[ 3]: 1235000 # 通过格式化操作取小数精度 In [4]: x = 1.23456 In [ 5]: format(x, ' 0.2f ' ) Out[ 5]: ' 1.23 ' In [ 6]: ' value is {:0.3f} ' .format(x) Out[ 6]: ' value is 1.235 ' 2、执行精确的小数计算 # 在数学计算中由于CPU的浮点运算单元特性导致会引入微小的误差 In [11]: a = 4.2 In [ 12

Delete nan AND corresponding elements in two same-length array

谁说我不能喝 提交于 2021-02-09 08:57:27
问题 I have two lists of the same length that I can convert into array to play with the numpy.stats.pearsonr method. Now, some of the elements of these lists are nan , and can thus not be used for that method. The best thing to do in my case is to remove those elements, and the corresponding element in the other list. Is there a practical and pythonic way to do it? Example: I have [1 2 nan 4 5 6 ] and [1 nan 3 nan 5 6] and in the end I need [1 5 6 ] [1 5 6 ] (here the number are representative of

Finding the most common subarray within a numpy array

一曲冷凌霜 提交于 2021-02-09 08:29:34
问题 Example data: array( [[ 1., 1.], [ 2., 1.], [ 0., 1.], [ 0., 0.], [ 0., 0.]]) with a desired result of >>> [0.,0.] ie) The most common pair. Approaches that don't seem to work: Using statistics as numpy arrays are unhashable. Using scipy.stats.mode as this returns the mode over each axis, eg) for our example it gives mode=array([[ 0., 1.]]) 回答1: You can do this efficiently with numpy using the unique function: pairs, counts = np.unique(a, axis=0, return_counts=True) print(pairs[counts.argmax(

NumPy: how to left join arrays with duplicates

冷暖自知 提交于 2021-02-09 07:36:57
问题 To use Cython, I need to convert df1.merge(df2, how='left') (using Pandas ) to plain NumPy , while I found numpy.lib.recfunctions.join_by(key, r1, r2, jointype='leftouter') doesn't support any duplicates along key . Is there any way to solve it? 回答1: Here's a stab at a pure numpy left join that can handle duplicate keys: import numpy as np def join_by_left(key, r1, r2, mask=True): # figure out the dtype of the result array descr1 = r1.dtype.descr descr2 = [d for d in r2.dtype.descr if d[0]

NumPy: how to left join arrays with duplicates

寵の児 提交于 2021-02-09 07:36:51
问题 To use Cython, I need to convert df1.merge(df2, how='left') (using Pandas ) to plain NumPy , while I found numpy.lib.recfunctions.join_by(key, r1, r2, jointype='leftouter') doesn't support any duplicates along key . Is there any way to solve it? 回答1: Here's a stab at a pure numpy left join that can handle duplicate keys: import numpy as np def join_by_left(key, r1, r2, mask=True): # figure out the dtype of the result array descr1 = r1.dtype.descr descr2 = [d for d in r2.dtype.descr if d[0]

Indexing 3d numpy array with 2d array

别说谁变了你拦得住时间么 提交于 2021-02-09 06:52:08
问题 I would like to create a numpy 2d-array based on values in a numpy 3d-array, using another numpy 2d-array to determine which element to use in axis 3. import numpy as np #-------------------------------------------------------------------- arr_3d = np.arange(2*3*4).reshape(2,3,4) print('arr_3d shape=', arr_3d.shape, '\n', arr_3d) arr_2d = np.array(([3,2,0], [2,3,2])) print('\n', 'arr_2d shape=', arr_2d.shape, '\n', arr_2d) res_2d = arr_3d[:, :, 2] print('\n','res_2d example using element 2 of

Indexing 3d numpy array with 2d array

三世轮回 提交于 2021-02-09 06:49:53
问题 I would like to create a numpy 2d-array based on values in a numpy 3d-array, using another numpy 2d-array to determine which element to use in axis 3. import numpy as np #-------------------------------------------------------------------- arr_3d = np.arange(2*3*4).reshape(2,3,4) print('arr_3d shape=', arr_3d.shape, '\n', arr_3d) arr_2d = np.array(([3,2,0], [2,3,2])) print('\n', 'arr_2d shape=', arr_2d.shape, '\n', arr_2d) res_2d = arr_3d[:, :, 2] print('\n','res_2d example using element 2 of

Indexing 3d numpy array with 2d array

谁说我不能喝 提交于 2021-02-09 06:49:37
问题 I would like to create a numpy 2d-array based on values in a numpy 3d-array, using another numpy 2d-array to determine which element to use in axis 3. import numpy as np #-------------------------------------------------------------------- arr_3d = np.arange(2*3*4).reshape(2,3,4) print('arr_3d shape=', arr_3d.shape, '\n', arr_3d) arr_2d = np.array(([3,2,0], [2,3,2])) print('\n', 'arr_2d shape=', arr_2d.shape, '\n', arr_2d) res_2d = arr_3d[:, :, 2] print('\n','res_2d example using element 2 of