0

I am new to the python programming. I met a problem now that I want to call some results from one class to another class as one of the __init__ attributes. Here is the code shown below: (reduced_data is numerical data in vector)

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:
        x = np.linalg.norm(reduced_data-[1,1])
        k = (x-5)^2
        a = A()
        a.fit(reduced_data)
        y_pred = a.predict(reduced_data)
        labels = a.cluster_labels(reduced_data)

Basically, I want to pass the value k in class B as the attribute of class A in def __init__(self, k=k in class B, tol=0.0001, max_iter=300):And how could I achieve it?

3 Answers 3

1

Quick answer: change the third line in class B to: a = A(k=k).

But ask yourself a more fundamental question. Why is "class B" a class at all? As you have written it, it doesn't have any "self" properties, so it has no preserved state. You calculate y_pred and labels exactly once. These are class attributes. After that, they never change. I suggest that you just eliminate the class and place the last six lines in the main body of your code.

Sign up to request clarification or add additional context in comments.

Comments

1

You can pass the parameters from the __init__ method when creating a new instance of that object.

I.e. a = A(k).

Note that in class B, the code is not inside a method. You may want to put that code outside of the class definition. In Python, code doesn't have to inside a class.

Comments

0

Your class A doesn't have the attribute k, however it's instance does.

So you either should make k a class attribute of A or pass the value of instance of A.k to B each time.

Comments

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.