Note that there are some explanatory texts on larger screens.

plurals
  1. POPython table classification
    primarykey
    data
    text
    <p>I have text file(test.data), which include some values and class name, for example</p> <pre><code>4.5,3.5,U1 4.5,10.5,U2 4.5,6,U1 3.5,10.5,U2 3.5,10.5,U2 5,7,U1 7,6.5,U1 </code></pre> <p>I need to classify this data to rows(matrix), where 1,2 row etc is data, last one is class. So I started with this code:</p> <pre><code>reader = csv.reader(open('test.data', 'r')) result = [] for row in reader: result.append(row) print result </code></pre> <p>output:</p> <pre><code>[['4.5', '3.5', 'U1'], ['4.5', '10.5', 'U2'], ['4.5', '6', 'U1'], ['3.5', '10.5', 'U2'], ['3.5', '10.5', 'U2'], ['5', '7', 'U1'], ['7', '6.5', 'U1']] </code></pre> <p>This all work ok, but now I need from this data make matrix classification. In this case I want to make matrix:</p> <pre><code>test data=[data1, data2,.....,class name1] test data2=[data1, data2,.....,class name2]... </code></pre> <p>I need this "matrix"<strong>(test data,test data2)</strong>, because I will then choose from every <strong>test data</strong> just <strong>2/3</strong> data which will be named "<strong>choosen</strong>", other 1/3 data must stay in test data,....</p> <p>So what I need as output:</p> <pre><code>choosen=[data,data,......class name1] # 2/3 from every **test.data** test data=[data1, data2,.....,class name1] # other 1/3 from test data test data2=[data1, data2,.....,class name1] # other 1/3 from test data 2 </code></pre> <blockquote> <p>.</p> <p>. . . .</p> </blockquote> <p>.</p> <p>Many thanks for help</p> <hr> <p><strong>EDIT2:</strong></p> <p>If I use your code I get:</p> <pre><code>{ 'U1': [ ['4.5','3.5'], ['4.5','6'], ['5','7'], ['7','6.5'] ], 'U2': [ ['4.5','10.5'], ['3.5','10.5'], ['3.5','10.5'] ] } </code></pre> <p>But I don't have everytime this data: </p> <pre><code>4.5,3.5,U1 4.5,10.5,U2 4.5,6,U1 3.5,10.5,U2 3.5,10.5,U2 5,7,U1 7,6.5,U1 </code></pre> <p>I have also: </p> <pre><code>4.5,3.5,4.5,10.5, U1 3.5,10.5,3.5,10.5,U2 4.5,12.5,3.5,12.5,U2 </code></pre> <p>...... (so I don't know that class is on second row as you write on your code), but I know that last row is CLASS</p> <p>So how can I change your code:</p> <pre><code>reader = csv.reader(open('test.data', 'r')) result = {} for row in reader: uclass=row[2] #-------&gt; must be last row not second !!!! if result.has_key(uclass): result[uclass].append([row[0],row[1]]) #----&gt;not just 2 row's, on other data I have for example 5 rows.. else: result[uclass]=[[row[0],row[1]]] #----&gt;not just 2 row's, on other data I have for example 5 rows.. print repr(result) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload