Efficient least squares fit of 2D polynomials to data?
I have some data on a regular 2D grid that I would like to decompose into an orthogonal basis function set. What is the most efficient way to do the fit?
Here's some sample code:
import numpy as np sze = (128,128) x, y = np.linspace(-1, 1, sze), np.linspace(-1, 1, sze) xv, yv = np.meshgrid(x, y) rho = np.sqrt(xv**2 + yv**2) phi = np.arctan2(yv, xv) data = (rho * np.cos(phi)) data[rho > 1] = 0 # now have some synthetic data -- need to fit to a basis function set that has single-index members: # 0: 1 # 1: rho * cos(phi) # 2: rho * sin(phi) # ... # 24: 70 rho^8 - 140 rho^6 + 90 rho^4 - 20 rho^2 + 1
See also questions close to this topic
Shell is not showing text from my prints
I'm trying to create a code for my lists work and i have written this at the moment:
list = [ ] def Menu(choice): print("""Menu: 1. View entire list 2. View one item 3. Reset list 4. Edit list 5. Quit """) choice = int(input("Please choose an option: ")) while True: if choice == "1": print (list) Menu() elif choice == "2": oneItem = int(input('Please enter an item to view (1-5): ')) oneItem = oneItem - 1 oneView = list[(oneView)] print (oneView) Menu() elif choice == "3": list = [None]*6 Menu() elif choice == "4": def carryon(): for i in range(6): add = input("Please enter a new item for the list: ") list.append(add) carryOn = input("Would you like to carry on? (y/n): ") if carryOn == "y": carryon() else: break break Menu()
When I run the code in shell the menu appears fine:
Menu: 1. View entire list 2. View one item 3. Reset list 4. Edit list 5. Quit Please choose an option: 3
But then it just comes up blank. I can type things in still but nothing happens. Even after i press enter it just carries on. I use python 3.4 (gui)
How the zip() function in Python works?
I am new to python and I'm trying to understand how this function do what it does. I want to know what is happening behind it to understand it better.
Uploading to S3 using Python Requests
I'd like to upload xml's directly to S3 without the use of modules like boto, boto3, or tinys3.
So far I have written:
url = "https://my-test-s3.s3.amazonaws.com" with open(xml_file,'rb') as data: requests.put(url, data=data)
and I've gone and head and set the AllowedOrigin on my S3 bucket to accept my server's address.
This does not error when running, however, it also does not seem to be uploading anything.
Any help would be appreciated --- I'd like to (a) get the thing to upload and (b) figure out how to apply AWSAccessKey and AWSSecretAccessKey to the request
Finding scaling factor of related datasets
I have two datasets from two sources vovering one signal and want to find the factor between the two. They have different resolutions in x and y as well as one set being more noisy than the other.
the following gives a simple approximation, though the actual data does not follow an easy-to-fit polynomial.
import numpy as np import matplotlib.pyplot as plt datax1 = np.linspace(0,100,1000) datay1 = np.around(datax1,-1)**2 datax2 = np.linspace(0,100,80)+np.random.normal(0,0.2,80) datay2 = (datax2**2)*np.random.normal(5,0.5)+np.random.normal(0,500,80) plt.title('Data 1 VS Data 2') plt.plot(datax1,datay1,'b',label='Data 1') plt.plot(datax2,datay2,'r',label='Data 2') plt.legend() plt.savefig('img.png', bbox_inches='tight', dpi=72)
I need to automate finding this factor since I have more datasets to analyse, but SciPy's
curve_fitdoes not play nice with
import scipy.optimize as opt import scipy.interpolate as interp def func(x,k): fun=interp(datax1,datay1*k) return fun(x) print opt.curve_fit(func,datax2,datay2)
TypeError: 'module' object is not callableat the definition of
Is there any way to do this with numpy or scipy or do I have to build my own least-squares function to find the scaling of the data?
Scipy Curve Fit Optimize not working for log scale values
So I am trying to fit a set of data points to this equation:
abs(I) = Io(exp((qV)/(nKT)) - 1) --- Shockley diode equation
to a bunch of data points I was given. Knowing the V and the I values, I need to optimize the Io and the n values to get me data closely matching the data set I was given.
However, scipy optimize curve fit is not giving me the values I want, which is where n = ~1.15 and Io = ~1.8E-13, and is instead giving me n = 2.12 and I = 2.11E-11. I suspect this is due to the data set values being very small numbers, messing with the optimization, but even when i set the initial guess to be n = 1.15 and Io = 1.8E-13, the optimization values do not change.
Does anyone have any tips on how to fix this?
import numpy as np import math import matplotlib.pyplot as plt from scipy.optimize import curve_fit Voltage = np.array([-0.5 , -0.49, -0.48, -0.47, -0.46, -0.45, -0.44, -0.43, -0.42, -0.41, -0.4 , -0.39, -0.38, -0.37, -0.36, -0.35, -0.34, -0.33, -0.32, -0.31, -0.3 , -0.29, -0.28, -0.27, -0.26, -0.25, -0.24, -0.23, -0.22, -0.21, -0.2 , -0.19, -0.18, -0.17, -0.16, -0.15, -0.14, -0.13, -0.12, -0.11, -0.1 , -0.09, -0.08, -0.07, -0.06, -0.05, -0.04, -0.03, -0.02, -0.01, 0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 , 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ]) Current = np.array([ 6.99000000e-13, 6.83000000e-13, 6.57000000e-13, 6.46000000e-13, 6.19000000e-13, 6.07000000e-13, 5.86000000e-13, 5.73000000e-13, 5.55000000e-13, 5.37000000e-13, 5.27000000e-13, 5.08000000e-13, 4.92000000e-13, 4.75000000e-13, 4.61000000e-13, 4.43000000e-13, 4.32000000e-13, 4.18000000e-13, 3.99000000e-13, 3.91000000e-13, 3.79000000e-13, 3.66000000e-13, 3.54000000e-13, 3.43000000e-13, 3.34000000e-13, 3.18000000e-13, 3.06000000e-13, 2.96000000e-13, 2.86000000e-13, 2.77000000e-13, 2.66000000e-13, 2.59000000e-13, 2.54000000e-13, 2.43000000e-13, 2.33000000e-13, 2.22000000e-13, 2.16000000e-13, 2.07000000e-13, 2.00000000e-13, 1.94000000e-13, 1.85000000e-13, 1.77000000e-13, 1.68000000e-13, 1.58000000e-13, 1.48000000e-13, 1.35000000e-13, 1.21000000e-13, 1.03000000e-13, 7.53000000e-14, 4.32000000e-14, 2.33000000e-15, 6.46000000e-14, 1.57000000e-13, 2.82000000e-13, 4.58000000e-13, 7.07000000e-13, 1.06000000e-12, 1.57000000e-12, 2.28000000e-12, 3.29000000e-12, 4.75000000e-12, 6.80000000e-12, 9.76000000e-12, 1.39000000e-11, 1.82000000e-11, 2.57000000e-11, 3.67000000e-11, 5.21000000e-11, 7.39000000e-11, 1.04000000e-10, 1.62000000e-10, 2.27000000e-10, 3.21000000e-10, 4.48000000e-10, 6.21000000e-10, 8.70000000e-10, 1.20000000e-09, 1.66000000e-09, 2.27000000e-09, 3.08000000e-09, 4.13000000e-09, 5.46000000e-09, 7.05000000e-09, 8.85000000e-09, 1.11000000e-08, 1.39000000e-08, 1.74000000e-08, 2.05000000e-08, 2.28000000e-08, 2.52000000e-08, 2.91000000e-08]) def diode_function(V, n, Io): kt = 300 * 1.38 * math.pow(10, -23) q = 1.60 * math.pow(10, -19) I_final = Io * (np.exp( (q * V) / (n * kt) ) - 1) return abs(I_final) p0 = [1.15, 1.8e-13] popt, pcov = curve_fit(diode_function, Voltage, Current, p0 = p0) print(popt) fig = plt.figure() ax = fig.add_subplot(121) ax.set_title('I_d vs V_d') ax.set_xlabel('V_d') ax.set_ylabel('I_d') ax.set_yscale('log') plt.plot(Voltage, Current, 'ko', label="Original Data") plt.plot(Voltage, diode_function(Voltage, *popt), 'r-', label="Fitted Curve") plt.legend(loc='best') ax = fig.add_subplot(122) ax.set_title('I_d vs V_d') ax.set_xlabel('V_d') ax.set_ylabel('I_d') ax.set_yscale('log') popt = [1.15,1.8e-13] plt.plot(Voltage, Current, 'ko', label="Original Data") plt.plot(Voltage, diode_function(Voltage, *popt), 'r-', label="Fitted Curve") plt.legend(loc='best') plt.show()
The left graph is with scipy optimization and the right graph is the one I want
Universal basis for graph analog of Boolean circuits?
The NAND gate is universal for Boolean circuits. That means that any function 2^n -> 2 can be built out of NAND gates.
Let Omega be the subgraph classifier, the graph with two vertices t,f and five edges
in:t->t, out1:t->t, out2:t->f, out3:f->t, out4:f->f.
There's obviously a finite set of operations that covers all graph homomorphisms from Omega^n to Omega, because the set of all operations of that form is finite. But how small can that set be? I'd be satisfied with a formula parametric in n, but surprised if it actually depends on n; I'd expect it to be a finite set of binary operations.
Map an xyz coordinate inside a hexahedron to normalized 0-1 space
The problem I am trying to solve is as follows:
I have 8 coordinates in a 3D space that define a hexahedron (a distorted cube).
Next, I have a new arbitrary 3D coordinate in that space.
How do I map this new coordinate into the hexahedron's space where 0-1 defines the space inside the hexahedron in each axis?
Research I have done leads me to think the answer lies in matrix math "change of basis" operations. However, since it appears that the solution would need to account for a non-linear transformation (the coordinates of any face of the hexahedron could form a trapezoid, for example) I am not sure where to look next. It seems the solution should be a fairly straightforward matrix math... but what? I am not sure where to even look next.