scipy

Scipy sparse memory explosion with simple matrix multiplication

家住魔仙堡 提交于 2021-01-28 06:24:18
问题 I noted that Scipy must be storing some intermediate arrays when doing matrix multiplication. I assume this can be helpful in some cases, but it is a pain sometimes. Consider the following example: from scipy.sparse import coo_matrix n = 100000000000 row = np.array([0, 0]) col = np.array([0, n-1]) data = np.array([1, 1]) A = coo_matrix((data, (row, col)), shape=(2, n)) Yes, this is a very large matrix. However it has only two nonzero values. The result of B = A.dot(A.T) can be evaluated by

Apply square wave function to numpy array

谁说胖子不能爱 提交于 2021-01-28 03:46:19
问题 I can quickly apply a cos function as follows to a float32 numpy array: cos_f = np.vectorize(lambda x: math.cos(2 * math.pi * x)) signal = square_f(integral) However if I try this: square_f = np.vectorize(lambda x: sig.square(2 * math.pi * x)) signal = square_f(integral) it takes 15 seconds for 60,000 samples. integral has a length of 1024 and is used as a buffer. How should I be applying the square wave to my signal? 回答1: sig.square(2*np.pi * x) is three times slower than np.cos(2*np.pi * x)

Scipy poisson distribution with an upper limit

谁说胖子不能爱 提交于 2021-01-28 01:54:36
问题 I am generating a random number using scipy stats. I used the Poisson distribution. Below is an example: import scipy.stats as sct A =2.5 Pos = sct.poisson.rvs(A,size = 20) When I print Pos, I got the following numbers: array([1, 3, 2, 3, 1, 2, 1, 2, 2, 3, 6, 0, 0, 4, 0, 1, 1, 3, 1, 5]) You can see from the array that some of the number,such as 6, is generated. What I want to do it to limit the biggest number(let's say 5), i.e. any random number generated using sct.poisson.rvs should be equal

Scipy.misc.imread flatten argument — converting to grey scale

耗尽温柔 提交于 2021-01-28 01:50:21
问题 I'm trying to understand how this method transform an image in grey scale (if it uses a simple mean or a weighted mean) -- I have to reference this method. From the documentation I know that this method calls the convert(‘F’) method. From Pillow/PIL source code, I can find this method, however, I couldn't find what it does when the mode parameter is set to 'F'. Thank you. 回答1: In the docstring of the convert method of the Image object (seen in the code that you linked to), there is this: When

scipy block_diag of a list of matrices

雨燕双飞 提交于 2021-01-28 00:23:27
问题 How can I get a matrix which has as diagonal some matrices that I have in a list? I can get this if the matrices are not in a list for example: x = np.random.normal(0, 1, (3,2)) y = np.random.randint(-2, 2, (5,4)) sp.linalg.block_diag(x, y) # correct result while if: matrices = [x, y] sp.linalg.block_diag(matrices) # wrong result. How can I solve this? 回答1: import numpy as np from scipy.linalg import block_diag A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) C = [A,B] block_diag

Python surface fitting of variables of different dimensionto get unknown parameters?

。_饼干妹妹 提交于 2021-01-27 23:43:39
问题 I have a function that includes x and y as independent variables and I want to fit the parameters to the data and function and plot a surface figure. I saw that if the variables have two different dimensions, I can use np.meshgrid(x,y) , but then how do I find the parameters a,b,c? My code looks like this: import matplotlib.pyplot as plt from scipy.optimize import curve_fit import numpy as np x = np.array([1,0.5,0.33,0.25,0.2]) y = np.array([1e-9,1e-8,1e-7,1e-6,1e-5,1e-4,1e-3,1e-2,1e-1,1e0

Dynamically choose argument for which to minimize a function in python using scipy.optimize

蓝咒 提交于 2021-01-27 23:41:42
问题 I have a function which takes a list of variables as an argument and I would like to minimize this function using scipy.optimize.minimize. The problem is that it is decided on runtime for which variable in the argument list the minimization should be done. All other variables will get a fixed value. Let's make an example to clarify: a = 1 c = 1.1 d = -1.2 def func( b ): return function_to_minimize( array=[a,b,c,d] ) sol = scipy.optimize.minimize( func, [b0], args=(a,c,d) ) This works, however

Steady State Probabilities (Markov Chain) Python Implementation

↘锁芯ラ 提交于 2021-01-27 21:18:31
问题 Hi I am trying to generate steady state probabilities for a transition probability matrix. Here is the code I am using: import numpy as np one_step_transition = array([[0.125 , 0.42857143, 0.75 ], [0.75 , 0.14285714, 0.25 ], [0.125 , 0.42857143, 0. ]]) def steady_state_prop(p): dim = p.shape[0] q = (p-np.eye(dim)) ones = np.ones(dim) q = np.c_[q,ones] QTQ = np.dot(q, q.T) bQT = np.ones(dim) return np.linalg.solve(QTQ,bQT) steady_state_matrix = steady_state_prop(one_step_transition.transpose()

difference between 2 scipy sparse csr matrices

白昼怎懂夜的黑 提交于 2021-01-27 20:17:32
问题 I have 2 scipy.sparse.csr_matrix like this: A = [ 1 0 1 0 0 1 1 0 0 1 0 0 0 1 0 0 0 0 ] B = [ 1 0 1 0 1 1 1 1 0 1 0 0 1 1 1 0 0 0 ] I am willing to get the "new ones" that appeared in B but weren't in A. C = [ 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 ] 回答1: IIUC it should be pretty straightforward: In [98]: C = B - A In [99]: C Out[99]: <3x6 sparse matrix of type '<class 'numpy.int32'>' with 4 stored elements in Compressed Sparse Row format> In [100]: C.A Out[100]: array([[0, 0, 0, 0, 1, 0], [0, 1

optimize.root with a matrix equation

跟風遠走 提交于 2021-01-27 19:30:22
问题 I am trying to solve the following linear system using optimize.root AX = b With the following code. A = [[0,1,0],[2,1,0],[1,4,1]] def foo(X): b = np.matrix([2,1,1]) out = np.dot(A,X) - b return out.tolist() sol = scipy.optimize.root(foo,[0,0,0]) I know that I can simply use the numpy.linalg.solve to do this easily. But I am actually trying to solve a non linear system that is in matrix form. See my question here. So I need to find a way to make this method work. To do that I am trying to