0

I have a problem that I can not get to solve.

I have two classes, class 1 deals with the view, class 2 with the calculation.

Class 1 accesses class 2. In class 1 is an instance of class 2 to access the predict method. The method predict has one argument. I set this argument over the input field in class 1. Then this error always occurs : AttributeError: 'float' object has no attribute 'dot' : return X.dot(self.W) + self.b Error when i push the button

Class 1:


class Example1:
    self.linearregression = LinearRegression(self,None)

    test = tk.DoubleVar(self)
    test.set("1.0")

    self.predictbutton = ttk.Button(self.plotframe, text = "predict", command = lambda:self.linearregression.predict(test.get()))
        self.predictbutton.grid(row=1, column=0)

Class 2:

class LinearRegression():

    def __init__(self, learning_rate, iterations):
        self.learning_rate = learning_rate
        self.iterations = iterations
    # Function for model training
    def fit(self, X, Y):
        # no_of_training_examples, no_of_features
        self.m, self.n = X.shape
        # weight initialization
        self.W = np.zeros(self.n)
        self.b = 0
        self.X = X
        self.Y = Y
        # gradient descent learning
        for i in range(self.iterations):
            self.update_weights()
        return self

    # Helper function to update weights in gradient descent

    def update_weights(self):
        Y_pred = self.predict(self.X)

        # calculate gradients
        dW = - (2 * (self.X.T).dot(self.Y - Y_pred)) / self.m
        db = - 2 * np.sum(self.Y - Y_pred) / self.m
        # update weights
        self.W = self.W - self.learning_rate * dW
        self.b = self.b - self.learning_rate * db
        return self
    # Hypothetical function  h( x )
    def predict(self, X):
        return X.dot(self.W) + self.b


  • That error indicates that `X` is a `float` instead of (presumably) a vector. Maybe you meant to use `self.X` instead? – 0x5453 Aug 26 '22 at 13:39
  • No that’s not work :( – deutscherkaffee Aug 26 '22 at 13:44
  • In your button command you pass a floating point number `1.0` why do you expect it to have a attribute `dot`? – Thingamabobs Aug 26 '22 at 14:31
  • what do you mean? – deutscherkaffee Aug 26 '22 at 14:34
  • `predictbutton` calls `predict` with the argument `1.0`. And in the method predict you name the argument `X`. And in the methods body you try to use an attribute `dot` via `X.dot`. Maybe you are looking for `numpy.dot(X, self.W)` but that is just a guess. Also it is pointless to return to a `tk.Button` – Thingamabobs Aug 26 '22 at 14:40
  • With the help of Linear Regression, I try to enter an X-value to get a Y-value. That is, in my GUI, I have an input field where I enter an X value, which causes me to confirm that value via a button. By confirming, this value should be passed as an argument to the predict method. The class LinearRegression, I have not written myself, but copied and included in my program. – deutscherkaffee Aug 26 '22 at 14:50
  • https://www.geeksforgeeks.org/linear-regression-implementation-from-scratch-using-python/ – deutscherkaffee Aug 26 '22 at 14:50
  • I want to use the input field to simply pass a value(X) to this method so I can get a Y value out of it. I don't understand how to get rid of this error message :( – deutscherkaffee Aug 26 '22 at 14:51
  • In your shared link they don't pass a bare floating point number they use an array returned by [train_test_split](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html). I don't see how your program will work without rewriting it. – Thingamabobs Aug 26 '22 at 14:58
  • When I apply your change, I get this error message @Thingamabobs – deutscherkaffee Aug 26 '22 at 14:59
  • Oh, so my idea with the split link code won't work? If I pass a number through input field of the method @Thingamabobs – deutscherkaffee Aug 26 '22 at 15:01
  • In fact I'm not quite sure what you want to do. Your source takes in a [dataset](https://github.com/mohit-baliyan/references/blob/master/salary_data.csv). How do you expect it to be replaced with a single value or even a value pair? You should try to understand the code you want to use before you try to implement it somewhere else. – Thingamabobs Aug 26 '22 at 15:08
  • I have trained the model with my data set (30 X-values and 30 Y-values), so the result is a linear function. Now, with the help of this function, it is possible to output a Y-value from any X-value. I wanted to do this with the help of the input field, that I enter any X-value, press the button "predict" and the corresponding Y-value is output. – deutscherkaffee Aug 26 '22 at 15:15
  • This is not possible with the code as it is. They use [pandas.dataframe.dot](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dot.html#pandas-dataframe-dot) A single value is not an array. You could rewrite your code to pass an appropriated parameter to your function. – Thingamabobs Aug 26 '22 at 15:30
  • Hm, okay, in that i have no experience:( – deutscherkaffee Aug 26 '22 at 15:33
  • You could start now to gain some or you solve [mine](https://stackoverflow.com/q/72401377/13629335) and I solve your problem. :D – Thingamabobs Aug 26 '22 at 15:36
  • Interesting problem, but I have no idea xD is it difficult to rewrite my code so that I enter an X value and get out the corresponding Y value? – deutscherkaffee Aug 26 '22 at 15:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247590/discussion-between-thingamabobs-and-deutscherkaffee). – Thingamabobs Aug 26 '22 at 16:23

0 Answers0