An error in a line after the code? - python

I'm trying to build a function for fitting of my data. I'm using CurveExpert and the lines are in python. I'm no expert so after trying, it says I have an error one line after the end of the return (last line). Can you please help me? I want to note that I tried to copy an existing format of another function.
Thanks in advance!
name = ur"Diffusion"
nindvar = 1
equation = r"8.4*erfc(a/(2*sqrt(13.6094*x*b)))"
latexequation = r"8.4\mathrm{erfc}{\left(a/{left(2\sqrt{13.6094 x b\right)}\right)}"
def evaluate(x,a,b):
"""
"""
y = 8.4*erfc(a/(2*sqrt(13.6094*x*b)))
return y
def initialize(x,y):
"""
"""
try:
a = 74
b = 0.37
return a,b

You can't have try without except.

Related

Python - Display Function - Prevent new line while using Latex Object

I am trying to output an answer to a differential equation.
My Code:
function = "cos(z)"
def Complex_Function(z):
f1=eval(function)
return f1
f_subsituted = Complex_Function(a+b*exp(x)) # make z = a+b e^x
DerivativeN1_Func = sm.diff(f_subsituted , a) #differntiate f_subsituted once
DerivativeN2_Func = sm.diff(DerivativeN1_Func , a) # differntiate f_subsituted twice
display(Latex(r"$y'''(x)+y(x)=-b^2 e^{-2x}$")) #Output needed (Here is the problem)
display(DerivativeN2_Func)
The correct output should be:
y'''(x)+y(x)=-b^2 e^{-2x} *DerivativeN2_Func
But whatever I try it shows the "DerivativeN2_Func" in a new line.
I tried to:
display(Latex(r"$y'''(x)+y(x)=-b^2 e^{-2x}$"), DerivativeN2_Func)
use (end"") method.
non of them worked.
Can you help me solving this issue please?

Python scipy minimize "numpy.float64 object is not callable

I have been trying to get over this error for a while and can't quite find a way to fix it. I'm trying to minimize a function, but whenever I call it I get the error in the title. I've looked at several other posts and have tried several different tactics, but no dice. here's the snippet in question here:
def objective_func(a, x_sum,y_sum):
alpha_sum = np.sum(a)
alpha_dot_sum= np.sum(np.dot(a[i],a[j]) for i in range(len(a)) for j in range(len(a)))
return (1/2) * (x_sum*y_sum*alpha_dot_sum)-alpha_sum
def Dual_SVM(data,c,a):
inputs = []
for example in data:
inputs.append(example[0:5])
outputs = []
for example in data:
outputs.append(example[len(example)-1])
bound = [(0,c)]
cons_function = np.sum(a*outputs)
cons = ({'type':'eq','fun':cons_function})
# inputs = []
x_sum= np.sum(np.dot(inputs[i],inputs[j]) for i in range(len(inputs)) for j in range(len(inputs)))
y_sum= np.sum(np.dot(outputs[i],outputs[j]) for i in range(len(outputs)) for j in range(len(outputs)))
sol = minimize(objective_func,x0=a,args=(x_sum,y_sum,),method='SLSQP',constraints=cons,bounds=bound)
return sol
Any feedback on this would be greatly appreciated. I know that the first argument needs to be a function and not just a function call, but I feel like I'm following the proper syntax. Not sure what to do here.
Fixed it. The problem was that cons_function was returning as a float, and not a function. A lambda function in its place fixed the problem.

Syntax error while trying to build dataframe in python

I am trying to create a function in python 3 that builds a dataframe from a csv file. However, I keep getting a syntax error when I call
y = (data_df["Status"].replace("underperform",0).replace("outperform",1).values.tolist())
This line of code is not running, because I never actually call the function. Here is all of my code.
def Build_Data_Set(features = ["DE Ratio","Trailing P/E"]):
data_df = pd.read_csv("key_stats.csv") #created in other file
X = np.array(data_df[features].values#.tolist())
y = (data_df["Status"].replace("underperform",0).replace("outperform",1).values.tolist())
return X,y
How should I go about fixing this error?
You're missing a closing parenthesis in your X = np.array(data_df[features].values#.tolist()) - it's there, but it's commented out of the code with the # sign.
Your python interpreter does not know that you actually wanted to end that line there and continues to search for a closing parenthesis. Before it finds one, it stumbles over the assignment in the next line, which is illegal and causes your syntax error.
You can simply do:
def Build_Data_Set(features = ["DE Ratio","Trailing P/E"]):
data_df = pd.read_csv("key_stats.csv") #created in other file
X = data_df[features].values # already returns an array
y = data_df["Status"].replace({"underperform": 0, "outperform":1}).values
return X,y
df = Build_Data_Set()

GHPython Von Koch function ERROR on Grasshopper

First time I use Python Script on Grasshopper. I can’t find where are my mistakes. I am almost at the end but now I have this Error message. Can anyone help me to find the mistake?
import rhinoscriptsyntax as rs
def koch(v1,v2) :
dist=rs.Distance(v1,v2)
p1=v2-v1
p1=rs.VectorUnitize(p1)
p1*=dist*dist1
p1+=v1
p2=v2-v1
cross=v2-v1
cross=rs.VectorUnitize(cross)
cross=rs.VectorRotate(cross, 90, (0,0,1))
cross*=dist*dist4
p2=rs.VectorUnitize(p2)
p2*=dist*dist2
p2+=v1+cross
p3=v2-v1
p3=rs.VectorUnitize(p3)
p3*=dist*dist3
p3+=v1
return (v1,p1,p2,p3,v2)
def recursive(v1,v2,gens, lineList):
if(gens>0):
newPts = koch(v1,v2)
l = rs.AddPolyline([newPts(0),newPts(1),newPts(2),newPts(3),newPts(4)])
lineList.append(l)
recursive(v1,newPts(0),gens-1)
return lineList
allLines=()
a=recursive(pt1,pt2,2,allLines)
screenshot
Your line l = rs.AddPolyline([newPts(0), newPts(1), newPts(2), newPts(3), newPts(4)]) is incorrect.
Accessing an item in a tuple requires square brackets. Replace with the following line:
l = rs.AddPolyline([newPts[0], newPts[1], newPts[2], newPts[3], newPts[4]])

How to use a variable in a IN clause Mysql. Python script

I am trying to optimize this block of code to use a single query rather than looping over and over.
while not (dataX):
i += 1
this_id = '/'.join(this_id.split('/')[0:-i])
if not this_id:
break
else:
dataX = db.conn[db_read].query("SELECT x AS xX FROM link WHERE _deleted = 0 AND _ref = %s AND _ntype = 'code' LIMIT 1;", data = (this_id,))
I want to use the IN clause with a variable that contains all possible substring but I can't get it to work.
this_id_list = "'/a/b/c/d/e' , '/a/b/c/d', '/a/b/c', '/a/b', '/a'"
result = db.conn[db_read].query("SELECT x AS xX FROM link WHERE _deleted = 0 AND _ref IN($this_id_list)")
Any idea what am I doing wrong and how to fix it? I would really appreciate any input! This is a Python script btw.
this_id_list = "'/a/b/c/d/e' , '/a/b/c/d', '/a/b/c', '/a/b', '/a'"
This should be a string

Categories