The following code executed from an IDLE window produces an error shown below.
import numpy as np
testarray = np.array([1,2,3], int)
Here is the error...
Traceback (most recent call last):
File "C:\Test\numpy.py", line 1, in <module>
import numpy as np
File "C:\Test\numpy.py", line 2, in <module>
testarray = np.array([1,2,3], int)
AttributeError: 'module' object has no attribute 'array'
>>>
If I do the same thing in the Shell, it works just fine...
>>> import numpy as np
>>> testarray = np.array([1,2,3], int)
>>> testarray
array([1, 2, 3])
>>>
This has been holding me up all day... anyone know how fix it? Perhaps I'm doing something wrong.
Note: If I just execute the code above without the testarray, no error gets returned.
You named a file numpy.py. Python sees that in the module search path and thinks it's the implementation of numpy. Pick a different name.
Related
Why am I having this error
import numpy as np
np.array([1,2,3,4])
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2304/609253908.py in
----> 1 np.array([1,2,3,4])
NameError: name 'np' is not defined
Your error code says that np.array([1, 2, 3, 4]) is in line 1 so you are trying to use it before importing, you need to import numpy first, as shown in your question.
You can just go with numpy
import numpy
numpy.array([1,2,3,4])
Have you installed or updated numpy?
Try this once in your terminal.
>>> pip install numpy
import numpy as np
arr = np.array([1,2,3,4,5])
newarr = np.array_split(arr, 3)
print(newarr)
I got this error:
AttributeError: partially initialized module 'numpy' has no attribute 'array'
(most likely due to a circular import)
The issue might be the you probably have a file named numpy.py in your working directory. Since it has the same name as the module, it would shadow it would lead to this error. Rename that file and remove its numpy.pyc file.
My program prints a variable predictions when executed, which is a 2-D numpy array. I am attempting to write the output to a text file. I ensured the shape is 2D by using predictions.shape and checked the type to be <class numpy.ndarray> by using type(predictions)
The error I am receiving is:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
predictor()
File "/Users/owner/Desktop/algo/predict.py", line 146, in predictor
predictions.savetxt("predictions.txt", "a", delimiter=',', fmt='%d', header='', footer='')
AttributeError: 'numpy.ndarray' object has no attribute 'savetxt'
This is returned after I call the function in the python module.
Im sure Ive imported numpy correctly, am I using the savetxt() function incorrect?
I tried searching on Stack but couldn't find anything, Thank you.
Yes, the savetext method is not a method on the array, but in the numpy package. You can use it by calling the method and passing in the array:
numpy.savetxt("predictions.txt", predictions, delimiter=',', fmt='%d', header='', footer='')
or np.savetext(..) if you did import numpy as np
I've a big problem using the import method of Atocad ActiveX, the doc says its signature is:
RetVal = (Document)object.Import((String)FileName, (Variant (three-element array of doubles))InsertionPoint, (Double)ScaleFactor)
I'm using the following snippet of code:
import array
import comtypes.client
acad = comtypes.client.GetActiveObject("AutoCAD.Application")
doc = acad.ActiveDocument
ms = doc.ModelSpace
doc.Import("C:/path/to/the/drawing.dwg", array.array('d', [0, 0, 0]), 1.0)
But I get the following error:
Traceback (most recent call last):
File ".\script.py", line 30, in <module>
doc.Import("C:/path/to/the/drawing.dwg", array.array('d', [0, 0, 0]), 1.0
)
_ctypes.COMError: (-2147024809, 'The parameter is incorrect.', (u'Invalid argument', u'AutoCAD', u'C:\\Program Files\\Au
todesk\\AutoCAD 2015\\HELP\\OLE_ERR.CHM', -2145320901, None))
The problem is: which parameter is incorrect?!! Usually I always pass coordinates to autocad methods as I did in the second argument, it always works, can you please help me?
EDIT I've tried to pass [0.0, 0.0, 0.0] same error
Not sure if you ever figured it out, but you get the COMError whenever there is a pending command in AutoCAD. The COMError is because both your script and the program itself are trying to access the same interface. You need to manually hit escape twice in AutoCAD and it shouldn't give you the error. If you ever find a way to make the python code use the COM interface to hit escape for you, let me know.
Can anyone suggest how to use PCA built in function in pyscripter. As a start i imported pca
from matplotlib.mlab import PCA
and then code area,
results = PCA(arr1)
print("-----------------------PCA-----------------------")
print(results)
where arr1 is the dataset(numerical) read from a file.But I am getting Attrubute error as:
Traceback (most recent call last):
File "C:\Users\pooja\Documents\project\traingsom.py", line 282, in <module>
results = PCA(arr1)
File "C:\Python27\lib\site-packages\matplotlib\mlab.py", line 845, in __init__
n, m = a.shape
AttributeError: 'list' object has no attribute 'shape'
Please help..
In the source code of mlab, it says '*a*: a numobservations x numdims array'.
You should use it like this:
import numpy as np
from matplotlib.mlab import PCA
dataMatrix = np.array(aListOfLists) # Convert a list-of-lists into a numpy array. aListOfLists is the data points in a regular list-of-lists type matrix.
myPCA = PCA(dataMatrix) # make a new PCA object from a numpy array object
Hope it helps. :)