scipy

How to minimize a real function with only integer input

北战南征 提交于 2021-02-11 15:33:03
问题 Which optimization algorithms work for integer input, float output? One thought is just using Brent search but making up a method that interpolates two nearest points to fake a real number input as opposed to an integer input. My second thought is that seems like such a common need, there must already be something in scipy to do it and/or an algorithm more suited for it? Bisect certainly works for this, but for huge inputs, its convergence time could be improved. Something hybrid like Brent

ModuleNotFoundError: No module named 'scikit'

江枫思渺然 提交于 2021-02-11 15:19:11
问题 I am trying to import sklearn in my jupyter notebook on a macOS Mojave 10.14.3. macOS comes standard with python 2.7. I have installed python3 in addition python 2.7. I am launching jupyter from a virtual env with python3. When I launch jupyter and open a notebook it shows it is running python 3.7.x. When I list "!pip show scipy" from inside the notebook it says scipy 1.2.1 is installed. Still, with all the above, when I say "import scikit" or "import sklearn" I get a ModuleNotFoundError: No

How to convert mat file to numpy array

时间秒杀一切 提交于 2021-02-11 15:14:27
问题 I want to convert a mat file with size 600 by 600 to numpy array and I got this error "float() argument must be a string or a number, not 'dict'" I am wondering how can I fix it. import numpy as np import scipy.io as sio test = sio.loadmat('Y7.mat') data=np.zeros((600,600)) data[:,:]=test 回答1: In [240]: from scipy.io import loadmat Using a test mat file that I have from past SO questions: In [241]: loadmat('test.mat') Out[241]: {'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on:

How to set bounds for only one parameter

北城以北 提交于 2021-02-11 14:44:13
问题 I'm using curve_fit from scipy.optimize to fit my data. I have a function that fits three parameters (Z1, Z2, Z3). I wannt to provide bounds. However, I'd like to only provide a bound to Z2 (Z2 shall be below 40). I do not want to set bounds for Z1 and Z3. Is that possible? popt, pcov = curve_fit(func, xdata, ydata, p0 = [Z1, Z2, Z3], bounds = ((10, 20, 5), (100, 50, 100,))) # This way I provide bounds to Z1, Z2 and Z3 # I, however, only want to say that Z2 < 40 # Also interesting would be to

exponential decay with scipy just gives step function

微笑、不失礼 提交于 2021-02-11 14:26:35
问题 I'm trying to do an exponential fit with a set of data: import matplotlib.pyplot as plt import numpy as np import scipy.optimize as opt def func(x, a, b, c): return a * np.exp(x / -b) + c epr_data = np.loadtxt('T2_text', skiprows=1) time = epr_data[:, 1] intensity = epr_data[:, 2] optimizedParameters, pcov = opt.curve_fit(func, time, intensity) print(optimizedParameters) plt.plot(time, intensity, func(time, *optimizedParameters), label="fit") plt.show() but i just get this step function and

TypeError: Improper input: N=5 must not exceed M=2

霸气de小男生 提交于 2021-02-11 14:19:40
问题 I'm trying to use scipy.optimize.curve_fit with a custom fit function (roughly following this tutorial): # Fit function def fit_function(x, y, x0, y0, A, FWHM): return A*np.exp(1)*4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2*np.exp(-4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2) # Open image file img = Image.open('/home/user/image.tif') # xdata X, Y = img.size xRange = np.arange(1, X+1) yRange = np.arange(1, Y+1) xGrid, yGrid = np.meshgrid(xRange, yRange) xyGrid = np.vstack((xGrid.ravel(), yGrid

How to convert class 'sympy.core' to 'number' or 'float' for optimization?

霸气de小男生 提交于 2021-02-11 14:13:13
问题 I'm a Python initiator and I'd like to solve the following problems, but I don't know what the cause is.I approached the problem using 'fsolve' an optimization tool. First of all, I'm trying to solve a nonlinear equation, but I've approached it in two cases. One case worked out well. But I can't find another case. First case (complete case) from sympy import * from scipy.optimize import fsolve import numpy as np y= symbols('y') b_1, b_2 = symbols ('b_1,b_2') b = 1 f = b_1 + b_2*(y/b)**2 K1 =

Solving implicit function and passing in three arguments

有些话、适合烂在心里 提交于 2021-02-11 13:43:19
问题 In the equation above I want to solve for f and pass in Re, D, and epsilon. Here is my code below: import math from scipy.optimize import fsolve # Colebrook Turbulent Friction Factor Correlation def colebrook(Re, D, eps): return fsolve(-(1 / math.sqrt(f)) - 2 * math.log10(((eps / D) / 3.7) + (2.51 / Re * math.sqrt(f))), f) Would I use fsolve() or solve()? I read up on fsolve() on Python's main site, however I don't understand some of the inputs it wants. Thank you in advance! Also, I am using

How to organize list of list of lists to be compatible with scipy.optimize fmin init array

时间秒杀一切 提交于 2021-02-11 13:27:36
问题 I am very amateur when it comes to scipy. I am trying to use scipy's fmin function on a multidimensional variable system. For the sake of simplicity I am using list of list of list's. My data is 12 dimensional, when I enter np.shape(DATA) it returns (3,2,2) , I am not even sure if scipy can handle that many dimensions, if not no problem I can reduce them, the point is that the optimize.fmin() function doesn't accept list based arrays as x0 initial parameters, so I need help either rewriting

How to organize list of list of lists to be compatible with scipy.optimize fmin init array

有些话、适合烂在心里 提交于 2021-02-11 13:27:34
问题 I am very amateur when it comes to scipy. I am trying to use scipy's fmin function on a multidimensional variable system. For the sake of simplicity I am using list of list of list's. My data is 12 dimensional, when I enter np.shape(DATA) it returns (3,2,2) , I am not even sure if scipy can handle that many dimensions, if not no problem I can reduce them, the point is that the optimize.fmin() function doesn't accept list based arrays as x0 initial parameters, so I need help either rewriting