0
votes

given the following information:

http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data

i need to have an output that:

  1. Sepal length 2. Sepal width 3. Petal length 4. Petal width 5. Class: a. Iris Setosa b. Iris Versicolour c. Iris Virginica

The code needs to have an output file named iris-summary.txt.

The output file should give the average values of the sepal and petal lengths and widths. A suggested output format is: average sepal length, average sepal width, average petal length, average petal width, Class In this case the correct output will be formatted as: 6.588, 2.974, 5.552, 2.026, Iris-virginica 5.006, 3.418, 1.464, 0.244, Iris-setosa 5.936, 2.77, 4.26, 1.326, Iris-versicolor

I know i need to use dictionaries and list comprehension. I also know i need to use the f.split(",") capability to add keys to the dictionaries where I can then add an then average them.

If anyone can help me with this code, thanks.

This is just some fiddling I did but I know I am very very far off. I pretty much only created the lists and dictionaries and know that the keys have to be appended still. Do i need to use both dictionary and list?

from __future__ import division, print_function

f = ('iris.txt', 'r')

f2 = ('iris-summary.txt', 'w')

for line in f
    c = f.split(',')

l_sl = []
l_sw = []
l_pl = []
l_pw = []
l_c = []

d_sepal_length = {}
d_sepal_width = {}
d_pedal_length = {}
d_pedal_width = {}
d_class = {}
1
update these lists inside the loop, then use sum and len to get averages. - Elazar
can you show me an example for just the sepal length. from the looks of it I will just forego using dictionaries. i am not very comfortable with them anyways. - user2352956

1 Answers

0
votes
k = [s.split(',') for s in open('iris.txt').readlines()]
sl = sum(s[0] for s in k)/len(k)

etc.