0
from numpy import array
import matplotlib
import matplotlib.pyplot as plt
from fileread import file2matrix
datingDataMat,datingLabels = file2matrix('iris_data.txt')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,1], datingDataMat[:,2],15.0*array(datingLabels), 15.0*array(datingLabels))
plt.show()

This code is displaying error as::

TypeError: unsupported operand type(s) for *: 'float' and 'numpy.ndarray'

According to the author i should be able to generate different colors based on datalabels.

2
  • 1
    can you post a few lines of your iris_data.txt? so we know what is in your datafile? Commented Oct 25, 2013 at 8:18
  • 40920 8.326976 0.953952 largeDoses 14488 7.153469 1.673904 smallDoses 26052 1.441871 0.805124 didntLike 75136 13.147394 0.428964 didntLike 38344 1.669788 0.134296 didntLike 72993 10.141740 1.032955 didntLike 35948 6.830792 1.213192 largeDoses 42666 13.276369 0.543880 largeDoses 67497 8.631577 0.749278 didntLike 35483 12.273169 1.508053 largeDoses 50242 3.723498 0.831917 didntLike 63275 8.385879 1.669485 didntLike 5569 4.875435 0.728658 smallDoses Commented Nov 12, 2013 at 6:21

3 Answers 3

3

The array should contains numeric values.

>>> 15.0 * array([1,2])
array([ 15.,  30.])

>>> 15.0 * array(['1','2'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'float' and 'numpy.ndarray'

Check the value of datingLabels.

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

3 Comments

datinglabels contains strings only.Strange how the author has shown it is working
@PavanRavishankar, Try ax.scatter(datingDataMat[:,1], datingDataMat[:,2]).
@tcp : do one thing, convert all datingLabels inside iris.txt to 0, 1, 2. e.g for didntLike save 0 , for smallDoses save 1 likewise, then it will work
2

I faced the similar problem. Here is what I did . I converted the label to contain numeric values.I am working on python 2.7 , not sure if 3.3 version would have handled it automatically.

newdatLabel = []

for item in datLabel:

if item == 'largeDoses':

    newdatLabel.append(2)

elif item == 'smallDoses':

    newdatLabel.append(1)

elif item == 'didntLike':

    newdatLabel.append(0)

Comments

2

Here's another approach

Author provides datingTestSet2.txt

You can download it here (I assume you've done it already)

http://www.manning.com/pharrington/

you can find forth column's values are numeric in this file

but still datingLabels is filled with string values like ['3', '2', '1', .....]

so 15.0*array(datingLabels) doesn't work

To convert the type of an array, use the .astype() method

like 15.0*array(datingLabels).astype(float)

from numpy import array 
import matplotlib
import matplotlib.pyplot as plt
from fileread import file2matrix
datingDataMat,datingLabels = file2matrix('datingDataTest2.txt')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,1], datingDataMat[:,2],15.0*array(datingLabels).astype(float), 15.0*array(datingLabels).astype(float))
plt.show()

it should work!

1 Comment

thanx @J.U. , I think there is one more problem with the kNN.py at line 36, it was trying to convert a string type into int .. so I removed int() conversion then it worked pretty fine ,, well datingDataTest2.txt is correct isn't it??

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.