import numpy as np
import math as m
def distance(list1_features, list2_features):
distance = (list1_features - list2_features)
"""The Euclidean distance between two arrays of feature values."""
return m.sqrt(np.sum((distance)**2))`enter code here`
distances_btw_song=[]
for index, row in lyrics.iterrows():
point1= np.array(lyrics.iloc[0:1,3:])
point2=np.array(lyrics.iloc[0:1,3])
distances=distance(point1,point2)
distances_btw_song.append(distances)
lyrics["Distance"]= distances_btw_song
lyrics.head()
I got this code from my partner and we are both using the same code however when the code reaches the distance(point1,point2) it gives me float object not callable but when my partner ran it, he got the table.
DataBase:
Traceback
The reuse of the variable distance is likely the culprit, though you should include more data about the error, like a stack trace. You somehow have the value of distance set to a float instead of a function. Depending on how you're running your code, there's a few ways this might accidentally happen. Again, you need to include more context around how the error occurred, not just the code and an error.
I figured out what was the problem I just changed
def distance(list1_features, list2_features):
to this
def euclidean_distance(list1_features, list2_features):
Related
I have defined my own class which takes in any matrix and is defined in such a way to convert this matrix into three numpy arrays inside a parenthesis (which I assume means it's a tuple). Furthermore, I have added a getitem method which allows output arrays to be subscript-able just like normal arrays.
My class is called MatrixConverter, and say x is some random matrix, then:
q=MatrixConverter(x)
Where q gives:
q=(array[1,2,3,4],array[5,6,7,8],array[9,10,11,12])
(Note that this is just an example, it does not produce three arrays with consecutive numbers)
Then, for example, by my getitem method, it allows for:
q[0] = array[1,2,3,4]
q[0][1] = 2
Now, I'm attempting to design a method to add en element into one of the arrays using the np.insert function such as the following:
class MatrixConverter
#some code here
def __change__(self,n,x):
self[1]=np.insert(self[1],n,x)
return self
Then, my desired output for the case where n=2 and x=70 is the following:
In:q.__change__(2,70)
Out:(array[1,2,3,4],array[5,6,70,7,8],array[9,10,11,12])
However, this gives me a TypeError: 'MatrixConverter' object does not support item assignment.
Any help/debugs? Should I perhaps use np.concentate instead?
Thank you!
Change your method to:
def __change__(self,n,x):
temp = np.insert(self[1],n,x)
self[1] = temp
return self
This will help you distinguish between a problem with the insert and a problem with the self[1] = ... setting.
I don't think the problem is with the insert call, but you need to write code that doesn't confuse you on such matters. That's a basic part of debugging.
Beyond that you haven't given us enough code to help you. For example what's the "getitem".
Expressions like array[1,2,3,4] tell me that you aren't actually copying from your code. That's not a valid Python expression, or array display.
When using Scipy's fmin function, I keep encountering the error message: ValueError: setting an array element with a sequence
I have seen that this question has been asked already some times, and I have read interesting posts such as:
ValueError: setting an array element with a sequence
Scipy optimize fmin ValueError: setting an array element with a sequence
Scipy minimize fmin - problems with syntax
..and have tried implementing the suggested solutions, such as adding '*args' to the cost function, appending the variables in the cost function to a list and vectorizing the variables. But nothing has worked for me so far.
I am quite new to programming in Python, so it is possible that I have read the solution and not known how to apply it.
A simplified version of the code, which I used to try to find the problem, is as follows:
import numpy as np
import scipy.optimize
from scipy.optimize import fmin
fcm28=40
M_test=np.array([32.37,62.54,208,410,802])
R_test=np.array([11.95,22.11,33.81,39.18,50.61])
startParams=np.array([fcm28,1,1])
def func(xarray):
x=xarray[0]
y=xarray[1]
z=xarray[2]
expo3=x*np.exp(-(y/M_test)**z)
cost=expo3-R_test
return cost
### If I write the following lines of code:
# xarray=(100,290,0.3)
# print(func(xarray))
# >> [ 2.557 -1.603 -0.684 1.423 -2.755] #I would obtain this output
func_optimised=fmin(func,x0=[fcm28,1,1],xtol=0.000001)
Objective: I am trying to obtain an exponential function 'expo3' (that takes 5 adjustment points, defined by vector 'M_test' on the horizontal axis and 'R_test' in the vertical axis.
What I am trying to minimise is the difference between the function 'expo3' and the adjustment points.
So, the exponential graph is meant to go as close as possible to the adjustment points, such as:
I obtain the following error message:
File "Example2.py", line 20, in <module>
func_optimised=fmin(func,x0=[fcm28,1,1],xtol=0.000001)
File "/home/.../python3.6/site-packages/scipy/optimize/optimize.py", line 443, in fmin
res=_minimize_neldermead(func,x0,args,callback=callback,**opts)
File "/home/.../python3.6/site-packages/scipy/optimize/optimize.py" line 586, in _minimize_neldermead
fsim[k] = func(sim[k])
ValueError: setting an array element with a sequence.
Can fmin be used to accomplish this task? Are there any viable alternatives?
Any help on how to solve this would be really appreciated.
As noted in the commments, your function must return a single value. Assuming that you want to perform a classic least squares fit, you could modify func to return just that:
def func(...):
# ... identical lines skipped
cost = sum((expo3-R_test)**2)
return cost
With that change, func_optimised becomes:
array([1.10633369e+02, 3.85674857e+02, 2.97121854e-01])
# or approximately (110.6, 385.6, 0.3)
Just as a pointer: you could alternatively use scipy.optimize.curve_fit for basically doing the same thing, but with a nicer API that allows you to directly provide the function skeleton + the sample points to fit.
I wish to use scipy's optimize.fmin function to find the minimum of a function, which is a function of both variables I wish to minimize over and parameters which do not change (are not optimized over).
I am able to do this when optimizing over a single variable here:
from scipy import optimize
c1=4
c2=-1
def f(x,c1,c2):
return x**2+c1+c2
guess_f=1
minimum = optimize.fmin(f,guess_f,args=(c1,c2),maxfun=400,maxiter=400,ftol=1e-2,xtol=1e-4)
However, I cannot get this to work when I add another variable to minimize over:
def g(x,y,c1,c2):
return x*y+c1+c2
guess_g=[1,1]
minimum2= optimize.fmin(g,guess_g,args=(c1,c2),maxfun=400,maxiter=400,ftol=1e-2,xtol=1e-4)
I get the following error message:
TypeError: g() missing 1 required positional argument: 'c2'
I did find Multiple variables in SciPy's optimize.minimize, and a solution is presented here in which the variables to be optimized over need to be grouped together as their own array. I try something like this below:
def g(params,c1,c2):
x,y=params
# print(params)
return x*y+c1*x+c2
guess_g=[1,1]
minimum2= optimize.fmin(g,guess_g,args=(c1,c2),maxfun=4000,maxiter=4000,ftol=1e-2,xtol=1e-4)
I do not receive a TypeError, but what I do get is the "Warning: Maximum number of function evaluations has been exceeded." message along with a RuntimeWarning: overflow encountered in double_scalars after removing the cwd from sys.path. (additionally, I tried using the optimize.minimize command to do the same thing, but was unable to get it to work when adding the extra arguments, but I do not post that code here as the question is already getting long).
So this does not seem to be the correct way to do this.
How do I go about optimizing with optimize.fmin function over multiple variables, while also giving my function additional arguments?
I'm currently working on a project relating to brownian motion, and trying to simulate some of it using Python (a language I'm admittedly very new at). Currently, my goal is to generate random numbers following a given probability density function. I've been trying to use the scipy library for it.
My current code looks like this:
>>> import scipy.stats as st
>>> class my_pdf(st.rv_continuous):
def _pdf(self,x,y):
return (1/math.sqrt(4*t*D*math.pi))*(math.exp(-((x^2)/(4*D*t))))*(1/math.sqrt(4*t*D*math.pi))*(math.exp(-((y^2)/(4*D*t))))
>>> def get_brown(a,b):
D,t = a,b
return my_pdf()
>>> get_brown(1,1)
<__main__.my_pdf object at 0x000000A66400A320>
All attempts at launching the get_brown function end up giving me these hexadecimals (always starting at 0x000000A66400A with only the last three digits changing, no matter what parameters I give for D and t). I'm not sure how to interpret that. All I want is to get random numbers following the given PDF; what do these hexadecimals mean?
The result you see is the memory address of the object you have created. Now you might ask: which object? Your method get_brown(int, int) calls return my_pdf() which creates an object of the class my_pdf and returns it. If you want to access the _pdf function of your class now and calculate the value of the pdf you can use this code:
get_brown(1,1)._pdf(x, y)
On the object you have just created you can also use all methods of the scipy.stats.rv_continous class, which you can find here.
For your situation you could also discard your current code and just use the normal distribution included in scipy as Brownian motion is mainly a Normal random process.
As noted, this is a memory location. Your function get_brown gets an instance of the my_pdf class, but doesn't evaluate the method inside that class.
What you probably want to do is call the _pdf method on that instance, rather than return the class itself.
def get_brown(a,b):
D,t = a,b # what is D,t for?
return my_pdf()_pdf(a,b)
I expect that the code you've posted is a simplification of what you're really doing, but functions don't need to be inside classes - so the _pdf function could live on it's own. Alternatively, you don't need to use the get_brown function - just instantiate the my_pdf class and call the calculation method.
I'm working on my Computer Graphics homework. Since we're allowed to choose the PL we want, I decided this would be a good occasion to learn some Python, but I ran into some trouble, eventually.
In one module I have some functions like this:
def function1 (a, b, matrix):
...
function2 (matrix)
def function2(matrix):
...
function3(x,y,matrix):
def function3(x,y,matrix):
...
matrix[x][y] = something
Now, from a different module, I call function1. It should then call function2 passing it the matrix, which should in turn call function3 passing it the matrix. However, I get a list assignment index out of range when attempting to access matrix[x][y.
If I try to call them on a matrix from the same module, it will work, so I thought that the functions might not realize they are receiving a matrix. I changed the function definitions to something like
function2(matrix = [[]])
but I still get the same error. I'm kind of stuck.
Sorry everyone, you were right.
There was this one pixel in a 500x500, which was actually at matrix[249][500].
When I made the checks, I checked if they're <=500 instead of <500, don't know why.
Thanks, I was pretty sure I was screwing something else up, especially after I added my (faulty) tests, since this is my first time writing python.