accessing and modifying the i,j element in a knime table

Hi,

It is impossible that this has not been asked before but I am baffled and can’t seem to find a previous post that answers this directly. What on earth is the code-free knime equivalent of:

table = zeros((numRows, numCols))
colsToModify = someListOfIntegers #length is numRows

for i in numRows:
table[i, colsToModify[i]] = 1

It’s a one-hot encoding example using a previously defined dictionary assigning characters in a string to an integer value. One To Many doesn’t seem to do it because it will only produce the one-hot encoding for the current string being examined and there are many different strings that all need to follow the same embedding.

I have the zeros matrix using Create Table Structure and Add Empty Rows. After a bunch of manipulations culminating with String Replace (Dictionary) and String Manipulation, I can output a column containing the numRows long list of column numbers (or column names that could be used as variables) to modify. How do I actually bring these together and use this to modify the specific elements at a given (i, j) position?

Thanks a lot,
Alan Bilsland

Ok.
Can you confirm me that your input is

One zero table (n x m)
One table with one column and n rows with integers (<colNumbers)

And that your goal is substitute in first table 0 with 1 according formula
t[i, c[i]] =1

Am I right?

Hi,

Thanks a lot for getting back to me. Yes that’s it exactly.

Alan

Hi @abilsland,

can you share your workflow or a data sample so it’s easier to comprehend what you would like to archive?

Kind regards
Mike

Can you try this:
KNIME_forum_23265_pigreco.knwf (16.2 KB)

I used column loop and compare column data with current iteration (using flow variable created by loop start node).
If you want you can use this node appending it to your zero table generator (in m node it’s a simple table creator).

1 Like

Well that definitely works for me so thanks very much for your extremely fast help with this. Being totally new to Knime though (I just did the courses in the online summit last month so that is the extent of my knowledge), I am still wondering if there is an alternative “knimey” way to do it that doesn’t involve any code at all. If a code snippet is the simplest way to go then obviously that’s what should be used, but it’s such a fundamental operation that surely there must also be an easy-ish way to do it with standard nodes. If not, no worries - you have given me a working solution so thanks again. I’ve posted the workflow as it stands including your solution. It’s part of bigger workflow that will then feed the one-hot array to various pre-built keras models.

One_hot.knar (48.0 KB)

1 Like

Eureka!
I’ve changed workflow according your specs.
This is a “beta version” where I used your dictionary and replaced index values (by workflow) according your goal.
If you want you can change dictionary in your csv file but I supposed dictionary as unmodifiable
If you want you can change list of column splitter positions (and names) using a flow variable created dinamically.
Try it and give me a feeback, please!
One_hot_pigreco.knar (24.0 KB)

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.