1
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
import matplotlib.pyplot as plt

iris_df = DataFrame()

iris_data_path = 'Z:\WORK\Programming\Python\irisdata.csv'

iris_df = pd.read_csv(iris_data_path,index_col=False,header=None,encoding='utf-8')

iris_df.columns = ['sepal length','sepal width','petal length','petal width','class']

print iris_df.columns.values
print iris_df.head()
print iris_df.tail()
irisX = irisdata[['sepal length','sepal width','petal length','petal width']]
print irisX.tail()
irisy = irisdata['class']
print irisy.head()
print irisy.tail()

colors = ['red','green','blue']
markers = ['o','>','x']

irisyn = np.where(irisy=='Iris-setosa',0,np.where(irisy=='Iris-virginica',2,1))

Col0 = irisdata['sepal length']
Col1 = irisdata['sepal width']
Col2 = irisdata['petal length']
Col3 = irisdata['petal width']

plt.figure(num=1,figsize=(16,10))
plt.subplot(2,3.1)
for i in range(len(colors)):
    xs = Col0[irisyn==i]
    xy = Col1[irisyn==i]
    plt.scatter(xs,xy,color=colors[i],marker=markers[i])
plt.legend( ('Iris-setosa', 'Iris-versicolor', 'Iris-virginica') )
plt.xlabel(irisdata.columns[0])
plt.ylabel(irisdata.columns[1])

plt.subplot(2,3,2)
for i in range(len(colors)):
    xs = Col0[irisyn==i]
    xy = Col2[irisyn==i]
    plt.scatter(xs,xy,color=colors[i],marker=markers[i])
plt.xlabel(irisdata.columns[0])
plt.ylabel(irisdata.columns[2])

plt.subplot(2,3,3)
for i in range(len(colors)):
    xs = Col0[irisyn==i]
    xy = Col3[irisyn==i]
    plt.scatter(xs,xy,color=colors[i],marker=markers[i])
plt.xlabel(irisdata.columns[0])
plt.ylabel(irisdata.columns[3])

plt.subplot(2,3,4)
for i in range(len(colors)):
    xs = Col1[irisyn==i]
    xy = Col2[irisyn==i]
    plt.scatter(xs,xy,color=colors[i],marker=markers[i])
plt.xlabel(irisdata.columns[1])
plt.ylabel(irisdata.columns[2])

plt.subplot(2,3,5)
for i in range(len(colors)):
    xs = Col1[irisyn==i]
    xy = Col3[irisyn==i]
    plt.scatter(xs,xy,color=colors[i],marker=markers[i])
plt.xlabel(irisdata.columns[1])
plt.ylabel(irisdata.columns[3])

plt.subplot(2,3,6)
for i in range(len(colors)):
    xs = Col2[irisyn==i]
    xy = Col3[irisyn==i]
    plt.scatter(xs,xy,color=colors[i],marker=markers[i])
plt.xlabel(irisdata.columns[2])
plt.ylabel(irisdata.columns[3])
plt.show()

This is code from Howard Bandy's book Quantitative Technical Analysis. The problem is that it is giving me errors even though I typed it out exactly like it is in the book.

I still get the series imported but unused and undefined name irisdata errors/warnings.

This is in the console:

Code:

  runfile('Z:/WORK/Programming/Python/Scripts/irisplotpairsdata2.py', wdir='//AMN/annex/WORK/Programming/Python/Scripts')
['sepal length' 'sepal width' 'petal length' 'petal width' 'class']
   sepal length  sepal width  petal length  petal width        class
0           5.1          3.5           1.4          0.2  Iris-setosa
1           4.9          3.0           1.4          0.2  Iris-setosa
2           4.7          3.2           1.3          0.2  Iris-setosa
3           4.6          3.1           1.5          0.2  Iris-setosa
4           5.0          3.6           1.4          0.2  Iris-setosa
     sepal length  sepal width  petal length  petal width           class
145           6.7          3.0           5.2          2.3  Iris-virginica
146           6.3          2.5           5.0          1.9  Iris-virginica
147           6.5          3.0           5.2          2.0  Iris-virginica
148           6.2          3.4           5.4          2.3  Iris-virginica
149           5.9          3.0           5.1          1.8  Iris-virginica
Traceback (most recent call last):

  File "<ipython-input-100-f0b2002668bd>", line 1, in <module>
    runfile('Z:/WORK/Programming/Python/Scripts/irisplotpairsdata2.py', wdir='//AMN/annex/WORK/Programming/Python/Scripts')

  File "C:\MyPrograms\Spyder(Python)\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\MyPrograms\Spyder(Python)\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "Z:/WORK/Programming/Python/Scripts/irisplotpairsdata2.py", line 24, in <module>
    irisX = irisdata[['sepal length','sepal width','petal length','petal width']]

TypeError: list indices must be integers, not list

Obviously, the program does not run.

I'm using spyder with python 2.7. Which is the platform he was using in the book.

Thanks for any insight.

antiseptic
  • 27
  • 1
  • 7

1 Answers1

1

Well Python is not wrong. You imported Series but never used, which is a warning that does not cause crash. The crash happens because you are dereferencing a variable, irisdata, which was never defined before. (Ctrl + f irisdata in your code and take a look.) Judging by your code, irisdataprobably needs to contain the parsed data of Z:\WORK\Programming\Python\irisdata.csv doesn't it? So you need to parse that out and assign it to irisdata. See this post

eg.

import csv
...
irisdata = list(csv.reader(open(iris_data_path, 'rb')))
Community
  • 1
  • 1
initialxy
  • 1,193
  • 8
  • 17
  • Hmm... I'm thinking that it's supposed to parse the csv file using Series. If that makes sense. Like, pd.read_csv should use Series. I'm a total python newb, so I don't know if that makes sense or not. – antiseptic Jul 11 '15 at 23:34
  • Perhaps you were supposed to use this. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.from_csv.html#pandas.Series.from_csv eg. `irisdata = Series.from_csv(iris_data_path)` – initialxy Jul 12 '15 at 02:13
  • Actually upon further inspection, I have suspicion that this lines should have been `irisX = iris_df[['sepal length','sepal width','petal length','petal width','class']]`, where `iris_df` is already assigned with parsed data at `iris_df = pd.read_csv(...)` I'm quite certain that this script doesn't end here, since the last lines seems to be preparing to plot a chart where `Series` will be used. All this are just my guess work. You really should be getting all that missing information from your book. If you are certain that you made no mistake, then you should rant at the book's publisher. – initialxy Jul 12 '15 at 02:56
  • You're right. I didn't post the whole code. Sorry, but I didn't think it would be important. I did have a few typos, but those have been cleared now. I'm almost thinking there's another typo in there, but I rewrote the whole thing and doubled checked, but didn't see any. Thanks for your insight. – antiseptic Jul 12 '15 at 05:02
  • Yea, the code is incorrect. I found the the code at his sight and the downloadable version works. – antiseptic Jul 13 '15 at 00:36