Numpy / Scipy: why is it throwing error
I am trying to convert MATLAB code to Python.
My MATLAB code:
ASE_lamda1=1000e-9; ASE_lamda2=1100e-9; del_lamda= 2e-9; ASE_lamda = (ASE_lamda1:del_lamda: ASE_lamda2)';
Below I am trying to use eqv. Python code:
#!/usr/bin/python
import numpy as np
ASE_lamda1 = 9.9999999999999995e-07
ASE_lamda2 = 1100e-9
del_lamda = 2e-9
ASE_lamda = np.transpose(np.arange[ASE_lamda1:del_lamda:ASE_lamda2])
But I am getting the following error:
Traceback (most recent call last):
File "tasks.py", line 22, in <module>
ASE_lamda = np.transpose(np.arange[ASE_lamda1:del_lamda:ASE_lamda2])
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
I'm not sure about this error - what it means since I don't have much experience with Python / Numpy / Scipy.
+3
source to share
1 answer
np.arange[ASE_lamda1:del_lamda:ASE_lamda2]
it should be
np.arange(ASE_lamda1, ASE_lamda2, del_lamda)
This returns
array([ 1.00000000e-06, 1.00200000e-06, 1.00400000e-06,
1.00600000e-06, 1.00800000e-06, 1.01000000e-06,
...
1.09000000e-06, 1.09200000e-06, 1.09400000e-06,
1.09600000e-06, 1.09800000e-06, 1.10000000e-06])
It's a 1D array, so carrying it over is no-op. You may need to change or change it to 2D depending on what you are going to do with it. An easy way to change the array to 2D is to use a slice and numpy.newaxis
:
In [54]: ASE_lamda[:, np.newaxis]
Out[54]:
array([[ 1.00000000e-06],
[ 1.00200000e-06],
...
[ 1.09800000e-06],
[ 1.10000000e-06]])
In [55]: ASE_lamda[np.newaxis, :]
Out[55]:
array([[ 1.00000000e-06, 1.00200000e-06, 1.00400000e-06,
1.00600000e-06, 1.00800000e-06, 1.01000000e-06,
...
1.09000000e-06, 1.09200000e-06, 1.09400000e-06,
1.09600000e-06, 1.09800000e-06, 1.10000000e-06]])
If you are switching to NumPy from MATLAB, have a look at NumPy for Matlab Users .
+2
source to share