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?
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).
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.
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)