scipy

Scipy Sparse Cumsum

谁都会走 提交于 2021-02-07 11:51:47
问题 Suppose I have a scipy.sparse.csr_matrix representing the values below [[0 0 1 2 0 3 0 4] [1 0 0 2 0 3 4 0]] I want to calculate the cumulative sum of non-zero values in-place, which would change the array to: [[0 0 1 3 0 6 0 10] [1 0 0 3 0 6 10 0]] The actual values are not 1, 2, 3, ... The number of non-zero values in each row are unlikely to be the same. How to do this fast? Current program: import scipy.sparse import numpy as np # sparse data a = scipy.sparse.csr_matrix( [[0,0,1,2,0,3,0,4

scipy curve_fit strange result

纵饮孤独 提交于 2021-02-07 10:47:15
问题 I am trying to fit a distribution with scipy's curve_fit. I tried to fit a one component exponential function which resulted in an almost straight line (see figure). I also tried a two component exponential fit which seemed to work nicely. Two components just means that a part of the equation repeats with different input parameters. Anyway, here is the one component fit function: def Exponential(Z,w0,z0,Z0): z = Z - Z0 termB = (newsigma**2 + z*z0) / (numpy.sqrt(2.0)*newsigma*z0) termA =

Plotting one sigma error bars on a curve fit line in scipy

不打扰是莪最后的温柔 提交于 2021-02-07 09:09:58
问题 I plotted a linear least square fit curve using scipy.optimize.curve_fit() . My data has some error associated to it and I added those while plotting the fit curve. Next, I want to plot two dashed lines representing one sigma error bar on the curve fit and shade region between those two lines. This is what I have tried so far: import sys import os import numpy import matplotlib.pyplot as plt from pylab import * import scipy.optimize as optimization from scipy.optimize import curve_fit xdata =

Scipy minimize fmin - problems with syntax

末鹿安然 提交于 2021-02-07 06:52:17
问题 I have a function which takes several arguments (one array and two floats) and returns a scalar (float). Now I want to minimize this function by varying two of the arguments: the two floats. The array is "unpacked" inside the function at its contents (arrays and floats) are then used. How can this be done using SciPy's fmin function? I am having a very hard time figuring out the right syntax for this.. The function is something like: def func(x, y, data) data1=data[0] data2=data[...] ... ...

ValueError: illegal value in 4-th argument of internal None when running sklearn LinearRegression().fit()

风格不统一 提交于 2021-02-07 06:25:06
问题 For some reason I cannot get this block of code to run properly anymore: import numpy as np from sklearn.linear_model import LinearRegression # Create linear data with some noise x = np.random.uniform(0, 100, 1000) y = 2. * x + 3. + np.random.normal(0, 10, len(x)) # Fit linear data with sklearn LinearRegression lm = LinearRegression() lm.fit(x.reshape(-1, 1), y) Traceback (most recent call last): File "<input>", line 2, in <module> File "C:\Python37\lib\site-packages\sklearn\linear_model\

Remove/set the non-zero diagonal elements of a sparse matrix in scipy

瘦欲@ 提交于 2021-02-07 03:26:32
问题 Say I would like to remove the diagonal from a scipy.sparse.csr_matrix . Is there an efficient way of doing so? I saw that in the sparsetools module there are C functions to return the diagonal. Based on other SO answers here and here my current approach is the following: def csr_setdiag_val(csr, value=0): """Set all diagonal nonzero elements (elements currently in the sparsity pattern) to the given value. Useful to set to 0 mostly. """ if csr.format != "csr": raise ValueError('Matrix given

Remove/set the non-zero diagonal elements of a sparse matrix in scipy

心已入冬 提交于 2021-02-07 03:12:45
问题 Say I would like to remove the diagonal from a scipy.sparse.csr_matrix . Is there an efficient way of doing so? I saw that in the sparsetools module there are C functions to return the diagonal. Based on other SO answers here and here my current approach is the following: def csr_setdiag_val(csr, value=0): """Set all diagonal nonzero elements (elements currently in the sparsity pattern) to the given value. Useful to set to 0 mostly. """ if csr.format != "csr": raise ValueError('Matrix given

Remove/set the non-zero diagonal elements of a sparse matrix in scipy

て烟熏妆下的殇ゞ 提交于 2021-02-07 03:08:34
问题 Say I would like to remove the diagonal from a scipy.sparse.csr_matrix . Is there an efficient way of doing so? I saw that in the sparsetools module there are C functions to return the diagonal. Based on other SO answers here and here my current approach is the following: def csr_setdiag_val(csr, value=0): """Set all diagonal nonzero elements (elements currently in the sparsity pattern) to the given value. Useful to set to 0 mostly. """ if csr.format != "csr": raise ValueError('Matrix given

Getting standard error associated with parameter estimates from scipy.optimize.curve_fit

ぃ、小莉子 提交于 2021-02-07 03:00:53
问题 I am using scipy.optimize.curve_fit to fit a curve to some data i have. The curves, for the most part, seem to fit very well. For some reason, pcov = inf when i print it off. What i really need is to calculate the error associated with the parameters i'm fitting, and am not sure how exactly to do this even if it does give me the covariance matrix. The model being fit to is: def intensity(x,R_out,R_in,K_in,K_out,a,b,c): K_in,K_out = abs(0.0),abs(K_out) if x<=R_in: return 2*R_out*(K_out*np.sqrt

Getting standard error associated with parameter estimates from scipy.optimize.curve_fit

别来无恙 提交于 2021-02-07 03:00:41
问题 I am using scipy.optimize.curve_fit to fit a curve to some data i have. The curves, for the most part, seem to fit very well. For some reason, pcov = inf when i print it off. What i really need is to calculate the error associated with the parameters i'm fitting, and am not sure how exactly to do this even if it does give me the covariance matrix. The model being fit to is: def intensity(x,R_out,R_in,K_in,K_out,a,b,c): K_in,K_out = abs(0.0),abs(K_out) if x<=R_in: return 2*R_out*(K_out*np.sqrt