rasbt / python-machine-learning-book

The "Python Machine Learning (1st edition)" book code repository and info resource
MIT License
12.18k stars 4.39k forks source link

TypeError: can't multiply sequence by non-int of type 'float' #63

Open sprakashsingh opened 6 years ago

sprakashsingh commented 6 years ago

I am getting this error at the np.dot for the Iris data set. Can you explain the solution ?

Following is traceback : Traceback (most recent call last): File "Perceptron.py", line 61, in ppn.train(x, y) File "Perceptron.py", line 24, in train update = self.eta * (target - self.predict(xi)) File "Perceptron.py", line 35, in predict return np.where(self.net_input(X) >= 0.0, 1, -1) File "Perceptron.py", line 32, in netinput return np.dot(X, self.w[1:]) + self.w_[0]

Lijantropique commented 6 years ago

Hello,

Glad to see you're trying the algorithms described in the book. However, I would recommend adding more context to your questions: page? chapter? section? class? method?? what input values did you use (if any)? and even better, the actual script you used.

Based on the traceback, I assume you are in chapter 2, "Implementing a perceptron learning algorithm in Python". I implemented that one some time ago and it worked just fine. Therefore, my first assumption is that you have some typo in your script (we would only know if you provide your actual code).

The type of error you mentioned is typical of trying to multiply by a string (e.g., '1.0' * 1.). Check your code and post more questions (and more context) if required.

Cheers!

rasbt commented 6 years ago

I agree with @Lijantropique : a bit more information would help, and I suspect a typo somewhere in the code. I just checked the code notebook and it seems to work fine with the most recent NumPy version.

If you don't like to use Jupyter Notebooks, you can also run the code from https://github.com/rasbt/python-machine-learning-book/blob/master/code/optional-py-scripts/ch02.py

I.e., in your terminal, you can run

python3 python-machine-learning-book/code/optional-py-scripts/ch02.py

and check if that works. Then, I would recommend comparing the Perceptron code in ch02.py with your script to find out any differences that may be causing your issue.