Posts

Showing posts with the label numpy

Fit data within two function bounds

Fit data within two function bounds I have a set of data which, according to a theory, is bounded within two bounds. The upper bound is : f(x)=1/x (x<0.5) The lower bound is : f(x)=1+1/(2x)(x<0.5) The data I got is close to one of these two bounds. I’m trying to find a function to describe these data. But if I use normal fitting method, the fitted curve can be outside of these two bounds. How can I force my fitted curve between these two bounds by using scipy.curve_fit? I'm trying to use Pade approximant to do the fitting, the code I'm using is following: def pfuncp_3_1(x, a0, a1, a2, a3, b1): p1 = ((a0+a1*x+a2*x**2+a3*x**3)*(1+b1*x)**(-1)-1-1/(2*x)<0)*2.0 p2 = ((a0+a1*x+a2*x**2+a3*x**3)*(1+b1*x)**(-1)-1/x>0)*2.0 return (a0+a1*x+a2*x**2+a3*x**3)*(1+b1*x)**(-1) + p1 + p2 def ffuncp_3_1(x, a0, a1, a2, a3, b1): return (a0+a1*x+a2*x**2+a3*x**3)*(1+b1*x)**(-1) def data_fitter(pfunc, ffunc, fit_x, fit_y, new_x): popt, pcov = opt.curve_fit(pfunc, fit_x,...

'int' object not callable when defining function [on hold]

'int' object not callable when defining function [on hold] I have the following code: from __future__ import division import numpy as np def gauss(k): x, y = np.arange(100.0)/100.0, np.arange(100.0)/100.0 return((0.5**2/((2*3.14159)*(k**2)))*np.exp(-((1-(x-k))**2)/(2(k**2))).sum()*np.exp(-((1+k-y)**2)/(2(k**2))).sum()) And I get an error: 'int' object is not callable I've seen answers to questions similar to this one, but I'm still stuck because I can't work out what integer in this code could possibly be causing the proble. Any help would be greatly appreciated. This question appears to be off-topic. The users who voted to close gave this specific reason: The import is irrelevant: 2(k**2) . – jonrsharpe Jun 29 at 18:42 2(k**2) 1 Answer 1 ...

remove a character from a data numpy array

remove a character from a data numpy array I am currently importing a .txt file to a numpy array. The .txt file is organized by three columns with hundreds of thousands of rows. Occasionally, for no apparent reason, the third column will have a attached to the number. This causes genfromtxt to import the number as nan . I have tried using a replace function for numpy , but I believe this function is looking for a string because I get an "EOL while scanning string literal" error code.enter image description here numpy genfromtxt nan numpy Any advice? import numpy as np import numpy.core.defchararray as np_f FLR = np.genfromtxt("C:UsersbrandDownloadsPythonMyFilesaupnipam_scan41_3DFLR.txt") FLR = np_f(FLR, '', '') x = FLR[:,][:,0] y = FLR[:,][:,1] z = FLR[:,][:,2] I've added a picture of what the raw data looks like to show what I mean by it having a "" in the data Following is the complete error code I get File "<ipython-...

Conjugate transpose of self using numpy syntax

Conjugate transpose of self using numpy syntax I am trying to translate this MATLAB code into Python. The following is the code: Y=C*Up(:,1:p-1)'*Y; And this is my translation thus far: Y = C * Up[:, 1:p-1] * Y I am having trouble with the syntax for the conjugate transpose of self that is used in the MATLAb code. I am not certain that my first idea: Y = C * Up[:, 1:p-1].getH() * Y would be correct. Does anyone have any ideas? Can you provide sample inputs for the matrix (matrices)? – rahlf23 Jun 29 at 17:47 1:p-1 - remember, python indexing is 0 based, MATLAB 1 based. – hpaulj Jun 29 at 18:32 1:p-1 Is Up (possibly) complex? If not then plain transpose is enou...

insert to Db2 using Flask and sqlalchemy

insert to Db2 using Flask and sqlalchemy I have a Flask application with sqlalchemy in it. Running manage db init , manage db migrate and manage db upgrade worked perfectly. So the tables are created on db2 without any problems. When i try to do an insert, i get all kinds of errors. manage db init manage db migrate manage db upgrade Traceback (most recent call last): File "*path*/lib/python3.6/site-packages/ibm_db_dbi.py", line 1258, in _execute_helper return_value = ibm_db.execute(self.stmt_handler, parameters) Exception: Binding Error: During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context context) File "/Users/sebastianlindblom/Documents/GrowSmarter/metrolive-api-env/lib/python3.6/site-packages/ibm_db_sa/ibm_db.py", line 107, in do_ex...

Making a 2 dimensional list/matrix with different number of columns for each row

Making a 2 dimensional list/matrix with different number of columns for each row I would like to make a list or matrix that has a known number of rows(3), but for each row the number of elements will be different. So it could look something like this: [[4, 6, 8], [1, 2, 3, 4], [0, 2, 3, 4, 8]] Each row will have a known maximum of elements(8). So far I have tried the following: Sets1=np.zeros((3,8)) for j in range(3): Sets1[0,:]=[i for i, x in enumerate(K[:-1]) if B[x,j]==1 or B[x+1,j]==1] I want this because I want to have a list for each j in range(3) over which I can do a for loop and add constraints to my ILP. Any help will be greatly appreciated! Final expected output for the given sample? – Divakar Jun 29 at 8:35 You shouldn't call that a matrix because matrix is a rectangular array of numbers. It's...

Efficient way to work with 2D Arrays (multiple formats)

Efficient way to work with 2D Arrays (multiple formats) I have an efficiency/performance question in Python: I am planning to store data in some kind of matrix format in a "storage" For example: elementID - nodeID - elementType - Value My current process is to loop through the raw data information and check if there is already an entry with "elementID, nodeID, elementType" in the storage. Now, when considering a second case, also a second value is occuring. What I want to do is now to add the second value to the associated case in the storage. So: elementID - nodeID - elementType - Value1 - Value2 Currently I am working with 2D arrays, so [[a,b,c],[b,c,d],....]. Since this is implying "for loops", the time required to check if the entry is already in the storage is increasing tremendously. Also to add the additional column to the storage is requiring the search for the corresponding entry. In some cases, it is also possible that there are many value column...

Vectorized assignment in Numpy

Vectorized assignment in Numpy Let's assume I have a large 2D numpy array, e.g. 1000x1000 elements. I also have two 1D integer arrays of length L, and a float 1D arrray of the same length. If I want to simply assign floats to different positions in the original array according to integer array, I could write: mat = np.zeros((1000,1000)) int1 = np.random.randint(0,999,size=(50000,)) int2 = np.random.randint(0,999,size=(50000,)) f = np.random.rand(50000) mat[int1,int2] = f But if there were collisions i.e. multiple floats corresponding to single location, all but the last would be overwritten. Is there a way to somehow aggregate all the collisions, e.g. mean or median of all the floats falling at the same location? I would like to take advantage of vectorization and hopefully avoid interpreter loops. Thanks! Consider the ufunc .at method, e.g. np.add.at indexing with array – hpaulj 2 days ago ...