-1

I am new to python. Currently, I want call several defs from one class to another, however it always generate error message. The example code is shown below:

class A:
    def __init__(self, k=3, tol=0.0001, max_iter=300):
        self.k = k
        self.tol = tol
        self.max_iter = max_iter

    def fit(self, data):

        self.centroids = {}

        for i in range(self.k):
            self.centroids[i] = data[i+50]

        for i in range(self.max_iter):
            self.classifications = {}

            for i in range(self.k):
                self.classifications[i] = []

            for featureset in data:
                distances = [np.linalg.norm(featureset - self.centroids[centroid]) for centroid in self.centroids]
                classification = distances.index(min(distances))
                self.classifications[classification].append(featureset)
            prev_centroids = dict(self.centroids)

            for classification in self.classifications:
                self.centroids[classification] = np.average(self.classifications[classification], axis=0)

            optimized = True

            for c in self.centroids:
                original_centroid = prev_centroids[c]
                current_centroid = self.centroids[c]
                if np.sum((current_centroid - original_centroid) / original_centroid * 100.0) > self.tol:
                    #print(np.sum((current_centroid - original_centroid) / original_centroid * 100.0))
                    optimized = False

            if optimized:
                break
            
    def cluster_labels(self,data):
        cluster_labels = []
        for featureset in data:
            distances=[np.linalg.norm(featureset - self.centroids[centroid]) for centroid in self.centroids]
            cluster_labels.append(distances.index(min(distances)))
        return cluster_labels

class B:
        a = A
        a.fit(a,reduced_data)
        y_pred = a.predict(reduced_data)
        labels = a.cluster_labels(reduced_data)

However, it shows error that AttributeError: type object 'A' has no attribute 'k'on line for i in range(self.k):I am wondering where is the mistake and how should I call it?

夏思阳
  • 57
  • 1
  • 4
  • 10
  • 3
    You need to create an *instance* by writing ``a = A()``, otherwise a is just a reference to A. – Mike Scotty Sep 15 '20 at 06:23
  • 1
    You should use brackets to construct an instance: `a = A()`. That being said, I suggest you to read an introductory tutorial for Python first. – Selcuk Sep 15 '20 at 06:23
  • Can you show us how you initialise the class – monsieuralfonse64 Sep 15 '20 at 06:24
  • @MikeScotty Thank you for pointing out the problem. It is solved now – 夏思阳 Sep 15 '20 at 06:28
  • You can make that method in A as ```@staticmethod```, in case you don't want to instantiate object of A. see: https://docs.python.org/3/library/functions.html#staticmethod – drd Sep 15 '20 at 06:32

2 Answers2

0

You are using inheritance concept. So you have to define parent class name in parentheses of child class so that you can call them by using super() function. Example based on your code. :-"Class B(A)"

User super function to call parent method function. 'super(). function_name'

0

You need to correct following:

  1. You might want to create a function instead of class B.

  2. Don't need to pass parameter a in fit function. Correct in this form.

    a.fit(reduced_data)