def mulitopt(fitinput, min_, max_, steps, other_guesses, y_sigmas): better_fits_found = -1 best_fit_quality = 1e10 best_fit_params = [] inital_guesses = np.exp(np.linspace(np.log(min_), np.log(max_), steps)) for guess in inital_guesses: params, quality = simplex.optimize(*fitinput, y_sigmas=y_sigmas, p0=np.append([guess], other_guesses)) if quality < best_fit_quality: best_fit_quality = quality best_fit_params = params better_fits_found += 1 info("Better Fits found: %s", better_fits_found) return [best_fit_params, best_fit_quality]
def find_classifier(A, b, c): n = len(A.D[1]) R_sq = IDs.copy() for i in IDs: if n <= len(R_sq): break R_sq.add(-i) print("=== LP: Find vertex ===") if not simplex.find_vertex(A, b, R_sq): raise Error("Unable to find start vertex") print("=== LP: Run simplex ===") soln = simplex.optimize(A, b, c, R_sq) w = Vec(FEATUREs, {f: soln[f] for f in FEATUREs}) gamma = soln["gamma"] return (w, gamma)
import data as d import helpers as hel import plot_helpers as plot import table import simplex # Amplifier Bandpass Curve def gaus(x, mu, sigma, scale, noise_level): return noise_level + scale * 1 / sqrt(2 * pi * sigma) * exp( -(x - mu)**2 / 2 / sigma**2) initial_guess = [36e3, 100, 10, 0.1] params, quality = simplex.optimize(d.amplifier_f, d.amplifier_U, gaus, p0=initial_guess) fit = lambda x: gaus(x, *params) U_max = max(d.amplifier_U) U_half = U_max / sqrt(2) f_with_max_U = d.amplifier_f[argwhere(d.amplifier_U == U_max)] half_U_freq_left = optimize.brentq(lambda x: fit(x) - U_half, 34e3, f_with_max_U) half_U_freq_right = optimize.brentq(lambda x: fit(x) - U_half, f_with_max_U, 38e3) Q_exp = f_with_max_U / (half_U_freq_right - half_U_freq_left) print("Bandpass Quality:", Q_exp) plot.plot(d.amplifier_f, d.amplifier_U, "Frequenz in kHz", "Durchgelassene Spannung in V", "amplifier.pdf", fit)
# print(A.D[0]) features = {'texture(worst)', 'area(worst)'} A = make_matrix(matutil.mat2rowdict(F), d, features) b = make_b(A.D[0]) c = make_c(features, F.D[0]) R_square = F.D[0].copy() n = len(A.D[1]) it = iter(F.D[0]) while len(R_square) < n: R_square.add(-next(it)) print(A.D[1]) find_vertex(A, b, R_square) print(R_square) print('vertex found') x = optimize(A, b, c, R_square) pred = Vec(F.D[0], {}) for k, r in matutil.mat2rowdict(F).items(): s = 0 for f in features: s += r[f] * x[f] if s > x['gamma']: pred[k] = 1 else: pred[k] = -1 print(pred) print(d) # print(x['texture(worst)']) # print(x['area(worst)']) # print(x['gamma'])
# Fitting refractive indices def polynom(X, a1, a2): return [np.sqrt(a1 + a2 * x**2) for x in X] def laurent(lambda_, a0, a2): return [np.sqrt(a0 + a2 / x ** 2) for x in lambda_] # polynom_fit = hel.autofit(spectral_lines, n**2, polynom, p0=(3, 1e-10)) # laurent_fit = hel.autofit(spectral_lines, n**2, laurent) polynom_fit, polynom_quality = simplex.optimize(spectral_lines, n, polynom, p0=[3, 1e10]) laurent_fit, laurent_quality = simplex.optimize(spectral_lines, n, laurent, p0=[3, 1e10]) plt.clf() x_messung = spectral_lines y_messung = n x_limit = plot.autolimits(x_messung) x_flow = np.linspace(*x_limit, num=1000) y_messung = y_messung plt.plot(x_flow, polynom(x_flow, *polynom_fit), 'g-', label="Fit mit $x^2$") plt.plot(x_flow, laurent(x_flow, *laurent_fit), 'b-', label="Fit mit $x^{-2}$") plt.plot(x_messung, y_messung, 'r+', label="Messwerte")
import pprint import data as d import helpers as hel import plot_helpers as plot import table import simplex # Amplifier Bandpass Curve def gaus(x, mu, sigma, scale, noise_level): return noise_level + scale * 1 / sqrt(2 * pi * sigma) * exp(-(x - mu)**2 / 2 / sigma**2) initial_guess = [36e3, 100, 10, 0.1] params, quality = simplex.optimize(d.amplifier_f, d.amplifier_U, gaus, p0=initial_guess) fit = lambda x: gaus(x, *params) U_max = max(d.amplifier_U) U_half = U_max / sqrt(2) f_with_max_U = d.amplifier_f[argwhere(d.amplifier_U == U_max)] half_U_freq_left = optimize.brentq(lambda x: fit(x) - U_half, 34e3, f_with_max_U) half_U_freq_right = optimize.brentq(lambda x: fit(x) - U_half, f_with_max_U, 38e3) Q_exp = f_with_max_U / (half_U_freq_right - half_U_freq_left) print("Bandpass Quality:", Q_exp) plot.plot(d.amplifier_f, d.amplifier_U, "Frequenz in kHz", "Durchgelassene Spannung in V", "amplifier.pdf", fit)