0

Taking help from here, I was writing my own package. I have a folder filter which contains __init__.py (completely empty) and pca.py. pca.py has a class pca and that class has a method performPCA which takes two arguments. Then, I have this code:

from filter import pca
....
pca.performPCA(x,2)

When I run this, I'm getting an error

AttributeError: module 'filter.pca' has no attribute 'performPCA'

I know this question has an answer here, but I have everything that answer asks (the only difference is that my __init__.py is empty, which I think is totally fine). Please tell me where am I wrong. Thanks!

test.py goes as:

from filter import pca
print(pca)
import pandas as pd

x=pd.read_csv('Assignment-DS_gene_data.csv')
meta=pd.read_csv('Assignment-DS_Meta_data_sheet_.csv')
x.rename(columns={'Unnamed: 0':'col1'}, inplace=True )
del x['symbol']
del x['col1']
p=pca.pca2
#print(p)
xNew=p.performPCA(x,2)

pca.py goes as:

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from mpl_toolkits.mplot3d import Axes3D

class pca2:
    #Choose this method if only projected data is required
    def perfromPCA(self,data,nComp):
        pcaModel = PCA(n_components=nComp)
        principalComponents = pcaModel.fit_transform(data)
        colNames = []
        for i in range(1,nComp+1):
            colNames.append('PC '+str(i))
        principalDf = pd.DataFrame(data = principalComponents
             , columns = colNames)
        return principalDf

    #Choose this method if plot and projected data both are required
    #For this, nComp can either be 2 or 3
    def performPCAPlot(self,data,nComp,metaData,column):
        principalDf = performPCA(data,nComp)
        if nComp == 2:
            plt.scatter(principalDf,data=metaData['column'])
            #plt.xlabel('PC1')
            #plt.ylabel('PC2')
            plt.show()
        else:
            fig = plt.figure()
            #to do
        return principalDf
1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented May 29, 2019 at 11:26

2 Answers 2

0

Since you have another class inside pca.py, you should try creating an object first and then accessing it.

from filter import pca
....
p = pca()
p.performPCA(x,2)
Sign up to request clarification or add additional context in comments.

7 Comments

I'm sorry but it isn't working. I'm getting the error 'pca' object has no attribute 'performPCA'
Did you try debugging? Maybe add a breakpoint and check what actually pca is and what attributes it does have?
This is the p in your code: <filter.pca.pca object at 0x7f61f2899e48>
I meant like an actual debugging using IDE, not just print statements.
|
-1

Create an object first and then call the function on that object.

myPCA = pca()
myPCA.performPCA(x,2)

Edit

You are creating a python package by adding the _ init _.py file in the filter directory further reading subsection 6.4

You can import and use it like this (working for me with python 3.7.0)

from filter.pca import pca

myPCA = pca()
myPCA.performPCA(2,2)

or after renaming the class to pca2:

from filter.pca import pca2

myPCA = pca2()
myPCA.performPCA(2,2)

1 Comment

The proper instantiation should be pca.pca2()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.