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. :)
Related
I am trying to calculate the euclidean distance between two points in my Python code. However, when I import scipy.spatial in the following way: from scipy.spatial import distance and when I call: distance=distance.euclidean([5,2],[1,1]), I get this error: AttributeError: 'numpy.float64' object has no attribute 'euclidean'. When I import scipy like this: from scipy.spatial import distance as dist and call distance=dist.euclidean([5,2],[1,1]) it works. I don't understand how by renaming the import it actually works and when I don't rename it, it throws that error?
All works as expected, but you overwrite distance and errors are expected.
from scipy.spatial import distance
This is a normal import, and distance is a module. you can check this with
#distance is a module now
type(distance)
output:
module
Here you overwrite distance - this is bad style, and prone to errors.
distance=distance.euclidean([5,2],[1,1])
From here, distance is no longer a module, but overwritten to a numpy.float64. You can no longer do distance.euclidean() at this point. If you do, you get the error you showed, i.e.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-34-70ad947b0b7c> in <module>
1 # .euclidean() is not defined for a numpy.float64
2 # a error is expected
----> 3 distance=distance.euclidean([5,2],[1,1])
AttributeError: 'numpy.float64' object has no attribute 'euclidean'
Again, you can check indeed distance is a float with type(distance) which will return numpy.float64.
If you import with a different name as:
from scipy.spatial import distance as dist
distance=dist.euclidean([5,2],[1,1])
Nothing is overwritten. So all is good and we are fine and stay fine.
Or use
my_distance=distance.euclidean([5,2],[1,1])
Don't mixup modules names with variables.
Don't overwrite to different types in general to avoid confusion/errors.
For me this code runs without trouble:
from scipy.spatial import distance
a=distance.euclidean([5,2],[1,1])
print(a)
from scipy.spatial import distance as dist
b=dist.euclidean([5,2],[1,1])
print(b)
Output:
4.123105625617661
4.123105625617661
My scipy is version 1.4.1
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'm new to Python (and am using Spyder) and am trying to create some histograms of when the top movies from IMDB were created. I have imported the matplotlib, numpy and pandas, as well as the .txt file, but when I run the following lines of code:
plt.hist(data.year, bins=np.arange(1950, 2013), color='#cccccc')
I receive an error message:
Traceback (most recent call last):
File "stdin", line 1, in <module>
AttributeError: 'module' object has no attribute 'hist'
What am I doing wrong?
Your question provide very poor information and insight in your code. More data..
Meanwhile check if you actually import your modules correctly, should have:
import matplotlib.pyplot as plt
in order to use hist function
So this is a really weird problem I've been getting. I'm basically trying to create a practice codebook which uses SIFT features of images that are clustered by the kmeans algorithm in Python. However whenever I run the code I get the following error
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\Python\assignment2\SIFT_Dectection.py", line 34, in <module>
codebook, dis = cluster.vq.kmeans(codebook_construction(files[:20]),3)
File "C:\Python27\lib\site-packages\scipy\cluster\vq.py", line 513, in kmeans
No = obs.shape[0]
AttributeError: 'list' object has no attribute 'shape'
I assume that this is an error within the vq script for the Scipy library. However, I have other friends who are working on this as well and I am using the exact same code as them with the scipy library but I'm still getting this problem. I've also tried to completely uninstall Python reinstalling everything. I'm running the thing on Windows 7 btw. The code I'm using looks something like this:
import cv2
import glob
from scipy import cluster
files = glob.glob('101_ObjectCategories/*/*.jpg')
def codebook_construction(im):
codebook = []
for image in im:
img = cv2.imread(image)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT()
kp, desc = sift.detectAndCompute(gray, None)
if codebook == []:
codebook = desc
else:
codebook = np.vstack((codebook, desc))
return codebook
codebook, dis = cluster.vq.kmeans(codebook_construction(files[:20]),3)
The glob function there calls for a library of images I've downloaded from Caltech. I've searched high and low for an answer but it seems that no one has been having similar problems. Hopefully I can get some guidance here
The issue looks to be that kmeans is expecting an array, and you're feeding it a list. Try changing the last line of your codebook_construction() function to:
return scipy.array(codebook)
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.