numpy

Python - np.inf not larger than number

馋奶兔 提交于 2021-02-17 06:30:26
问题 Why np.inf > 1e999 returns False? I'm using Python 3.7 回答1: The notation 1e999 results in a float -- and this float is larger than the maximum possible value. So 1e999 becomes inf . A quick test in Ipython: In [11]: 1e999 == np.inf Out[11]: True 来源: https://stackoverflow.com/questions/63585238/python-np-inf-not-larger-than-number

Numpy two matrices, pairwise dot product of rows [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-17 06:25:25
问题 This question already has answers here : Vectorized way of calculating row-wise dot product two matrices with Scipy (5 answers) Closed 4 years ago . We are currently working on a python project and have to vectorize a lot due to performance constraints. We end up with the following calculation: We have two numpy arrays of shape (20,6) and want to calculate the pairwise dot product of the rows, i.e. we should obtain a (20,1) matrix in the end, where each row is the scalar obtained by the

Calculating percentile for each gridpoint in xarray

只愿长相守 提交于 2021-02-17 06:07:38
问题 I am currently using xarray to make probability maps. I want to use a statistical assessment like a “counting” exercise. Meaning, for all data points in NEU count how many times both variables jointly exceed their threshold. That means 1th percentile of the precipitation data and 99th percentile of temperature data. Then the probability (P) of join occurrence is simply the number of joint exceedances divided by the number of data points in your dataset. <xarray.Dataset> Dimensions: (latitude:

Calculating percentile for each gridpoint in xarray

你离开我真会死。 提交于 2021-02-17 06:03:56
问题 I am currently using xarray to make probability maps. I want to use a statistical assessment like a “counting” exercise. Meaning, for all data points in NEU count how many times both variables jointly exceed their threshold. That means 1th percentile of the precipitation data and 99th percentile of temperature data. Then the probability (P) of join occurrence is simply the number of joint exceedances divided by the number of data points in your dataset. <xarray.Dataset> Dimensions: (latitude:

Creating a boolean array by testing if each element in numpy array is between 2 numbers

五迷三道 提交于 2021-02-17 05:46:25
问题 I have a numpy array of numbers and i want to create a boolean array of the same size and dimensions that says whether or not that element lies between 2 numbers. For example: a=np.array([[1,2,3],[4,5,6],[7,8,9]]) I know if I write, print a>3 I get an array that has the first three elements "False" and the rest "True" np.array([[False,False,False],[True,True,True],[True,True,True]]) But i want to get a boolean array where the conditions are such that a>3 and a<8 Is there a way to do this

How to visualize a list of strings on a colorbar in matplotlib

别来无恙 提交于 2021-02-17 05:20:11
问题 I have a dataset like x = 3,4,6,77,3 y = 8,5,2,5,5 labels = "null","exit","power","smile","null" Then I use from matplotlib import pyplot as plt plt.scatter(x,y) colorbar = plt.colorbar(labels) plt.show() to make a scatter plot, but cannot make colorbar showing labels as its colors. How to get this? 回答1: I'm not sure, if it's a good idea to do that for scatter plots in general (you have the same description for different data points, maybe just use some legend here?), but I guess a specific

Dot product between 2D and 3D numpy arrays

你离开我真会死。 提交于 2021-02-17 04:55:16
问题 I have 2 arrays x and y with shapes (2, 3, 3) , respectively, (3, 3) . I want to compute the dot product z with shape (2, 3) in the following way: x = np.array([[[a111, a121, a131], [a211, a221, a231], [a311, a321, a331]], [[a112, a122, a132], [a212, a222, a232], [a312, a322, a332]]]) y = np.array([[b11, b12, b13], [b21, b22, b23], [b31, b32, b33]]) z = np.array([[a111*b11+a121*b12+a131*b13, a211*b21+a221*b22+a231*b23, a311*b31+a321*b32+a331*b33], [a112*b11+a122*b12+a132*b13, a212*b21+a222

Increasing range in np.arange by 1 increases range by 2 instead

对着背影说爱祢 提交于 2021-02-17 04:29:30
问题 I'm not sure if this is a bug or if I'm doing something wrong. I've got the following code: r_div = 200 r_max = 1.4 numMax=.84 lowerBin = int((numMax - .2)/(r_max/r_div)) upperBin = int((numMax + .2)/(r_max/r_div)) k =np.arange((r_max/r_div)*lowerBin,(r_max/r_div)*(upperBin+1),r_max/r_div) When I run np.shape(k), I get (59). Now, if I change the upper limit by one in the last line: k =np.arange((r_max/r_div)*lowerBin,(r_max/r_div)*(upperBin),r_max/r_div) and run np.shape(k) again, it gives me

Increasing range in np.arange by 1 increases range by 2 instead

江枫思渺然 提交于 2021-02-17 04:27:08
问题 I'm not sure if this is a bug or if I'm doing something wrong. I've got the following code: r_div = 200 r_max = 1.4 numMax=.84 lowerBin = int((numMax - .2)/(r_max/r_div)) upperBin = int((numMax + .2)/(r_max/r_div)) k =np.arange((r_max/r_div)*lowerBin,(r_max/r_div)*(upperBin+1),r_max/r_div) When I run np.shape(k), I get (59). Now, if I change the upper limit by one in the last line: k =np.arange((r_max/r_div)*lowerBin,(r_max/r_div)*(upperBin),r_max/r_div) and run np.shape(k) again, it gives me

Numpy reshape an array with specific order

人盡茶涼 提交于 2021-02-17 04:26:25
问题 Let's say I have this array x: x = array([1, 2, 3, 4, 5, 6, 7, 8]) x.shape = (8,1) I want to reshape it to become array([[1, 3, 5, 7], [2, 4, 6, 8]]) this is a reshape(2, 4) on x but in the straight forward way: y = x.reshape(2,4) y becomes array([[1, 2, 3, 4], [5, 6, 7, 8]]) and that's not what I want. Is there a way to transform the array in that specific way? 回答1: In[4]: x.reshape(4, 2).T Out[4]: array([[1, 3, 5, 7], [2, 4, 6, 8]]) 回答2: The easiest way to do this is to specify the order