Winds
Winds

Reputation: 397

python how to append new data by literately checking the existing data

I am working on machine learning project and would like to append data to the current table by literately checking the current data:

to be specific

X, y = form_results(results)
// X & y are numpy arrays 

the current data set looks like the following:

x[0]  x[1]   y
 1     4     1
 2     5     3
 3     6     4

how could I make it into the set as below:

x[0]  x[1]   y
 1     4     1
 1     5     0
 1     6     0
 2     4     0
 2     5     3
 2     6     0
 3     4     0
 3     5     0
 3     6     4

explanation: list out all the possibility match of x[0] & x[1] and if the match does not exist in the original table, append one row with the new match of x[0] & x[1] and make the value of y to be 0.

current rough though: // but this does not work

new_data = []
for x in enumerate(X), y:
    for i, j in x:
       if x[i] x[j]not in x:
        new_data.append(x[0], x[1], y)

X = numpy.vstack(X, new_data)

I am sorry to ask this kind of stupid questions but I could not find a way out.

Thanks in advance for your kindness.

Upvotes: 0

Views: 160

Answers (1)

Bryce Drew
Bryce Drew

Reputation: 6729

x = [[1, 2, 3], [4, 5, 6]]
y = [1, 3, 4]
new_data = []
for i, x0 in enumerate(x[0]):
    for j, x1 in enumerate(x[1]):
        #print x0, x1, y[i] if i == j else 0
        new_data.append([x0, x1, y[i] if i == j else 0])

This makes an array with the rows being as you want them to be.

I don't know numpy, so I can't help you with anything related to that. Someone should take what I've written and edit it or make a new answer with numpy in it.

Upvotes: 1

Related Questions