The error only length-1 arrays can be converted to Python scalars when trying to multiply matrix elements within a loop?
Your indexing seems to be incorrect. You're using [i][j]
when you need to use [i,j]
for numpy arrays.
>> A = np.matlib.identity(3)
>> A
matrix([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>> A[0][0]
matrix([[1., 0., 0.]])
>> A[0,0]
1.0
Thus, you're getting a row vector when you're expecting a scalar.
I should mention that a Google search on your error message turned up TypeError: only length-1 arrays can be converted to Python scalars while plot showing , which was helpful.
It's also good practice to print(type(suspect_thing))
or set breakpoints and use the debugger to inspect variables as you go. That can bubble these problems up right where you can see them. I, for example, copied your code into my editor and ran it, noting the line number where things went wrong. I was then able to see which variables needed checking out.
Error: [only length-1 arrays can be converted to Python scalars] when changing variable order
I wonder whether you data shows an exponential decay of rate. The mathematical model may not be the most suitable one.
See the doc string of curve_fit
f : callable
The model function, f(x, ...). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.
since your formula is essentially: k=A*ma.exp(-E/(R*T))
, the right order of parameters in func
should be (T, A, E)
or (T, E, A)
.
Regarding the order of A
and E
, they don't really matter. If you flip them, the result will get flipped as well:
>>> def func(T, A, E):
return A*ma.exp(-E/(R*T))
>>> so.curve_fit(func, T, k)
(array([ 8.21449078e+00, -5.86499656e+04]), array([[ 6.07720215e+09, 4.31864058e+12],
[ 4.31864058e+12, 3.07102992e+15]]))
>>> def func(T, E, A):
return A*ma.exp(-E/(R*T))
>>> so.curve_fit(func, T, k)
(array([ -5.86499656e+04, 8.21449078e+00]), array([[ 3.07102992e+15, 4.31864058e+12],
[ 4.31864058e+12, 6.07720215e+09]]))
I didn't get your typeerror
at all.
TypeError: only size-1 arrays can be converted to Python scalars when computing f1_score
Did you mean to format your print string with the score
variable instead? The error is with your print
call, not the f1_score
call, as seen from the stack trace. You're receiving this error because you used a format specifier for a single float and you're trying to insert an entire array (dev_y_pred
) rather than a single scalar value. Maybe you meant to do this: print('F1 Score: %.3f' % score)
Related Topics
Python (And Python C API): _New_ Versus _Init_
Using Lxml and Iterparse() to Parse a Big (+- 1Gb) Xml File
Setting Django Up to Use MySQL
String Formatting: Columns in Line
Sphinx's Autodoc's Automodule Having Apparently No Effect
How to Initialize Weights in Pytorch
How to Check That Multiple Keys Are in a Dict in a Single Pass
Shipping Python Modules in Pyspark to Other Nodes
Python - How to Check List Monotonicity
What Is the Most Compatible Way to Install Python Modules on a MAC
Equivalent of Numpy.Argsort() in Basic Python
Getting Values from Object Oriented Tkinter