Iterar pelas colunas de um dataset
Moderator: hwatanuki
Eu tenho um dataset com mais de 30 colunas e gostaria de calcular a correlação entre cada uma delas (e de preferência colocar os resultados em uma matriz de correlação). Existe alguma forma de criar um loop que roda a função CORRELATION() para todos os pares de colunas possíveis?
Um exemplo para deixar mais claro:
Dado um dataset de colunas y, x1, x2 e x3, gostaria de criar a seguinte tabela com as correlações entre cada uma (em anexo).
Um exemplo para deixar mais claro:
Dado um dataset de colunas y, x1, x2 e x3, gostaria de criar a seguinte tabela com as correlações entre cada uma (em anexo).
- AndreFB
- Posts: 3
- Joined: Wed Mar 24, 2021 5:51 pm
Olá André.
A abordagem recomendada para problemas nos quais não se pode referenciar diretamente campos do dataset (como linhas, colunas, etc) é utilizando template language: https://hpccsystems.com/training/documentation/ecl-language-reference/html/Templates.html#:~:text=The%20Template%20language%20is%20a,to%20implement%20the%20user's%20choices.
Acredito que o código a seguir seja o que você procura.
Repare que a estrutura record é exportada como XML (#EXPORTXML). Isso permite que seja possível utilizar o #FOR, uma estrutura de loop que percorre o arquivo XML exportado. Caso persistam dúvidas a respeito do trecho de código referente à exportação do arquivo como XML, há esse outro exemplo retirado da própria documentação da linguagem: https://hpccsystems.com/training/documentation/ecl-language-reference/html/_EXPORTXML.html
A abordagem recomendada para problemas nos quais não se pode referenciar diretamente campos do dataset (como linhas, colunas, etc) é utilizando template language: https://hpccsystems.com/training/documentation/ecl-language-reference/html/Templates.html#:~:text=The%20Template%20language%20is%20a,to%20implement%20the%20user's%20choices.
Acredito que o código a seguir seja o que você procura.
- Code: Select all
//*****************************
//This is test data -- requirement being #records = # of REAL fields
filelayout := RECORD
UNSIGNED myid;
REAL f1;
REAL f2;
REAL f3;
REAL f4;
END;
GFred := DATASET([ {1, 11.0, 12.0, 13.0, 99.0},
{2, 21.0, 22.0, 23.0, 24.0},
{3, 31.0, 32.0, 33.0, 34.0},
{4, 41.0, 42.0, 43.0, 44.0}
],filelayout);
// ThisDS := MyMod.Myfile;
// ****************************
//makes the code not reliant on any specific file/record structure names
ds := 'GFred'; //constant for the file def name
thislayout := RECORDOF(GFred); //replace ThisDS with whatever filename is actually used
// ****************************
#EXPORTXML(Fred,thislayout);
#DECLARE(outputStr)
#DECLARE(ProjStr)
#DECLARE(FldStr)
#DECLARE (Ndx)
#SET (Ndx, 0);
#FOR (Fred)
#FOR (Field)
#SET (Ndx, %Ndx% + 1)
#IF ( %Ndx% = 1)
#SET(outputStr,'SetVals := [')
#SET(ProjStr,'thislayout XF(OneRec L, INTEGER C) := TRANSFORM\n SELF.' + %'{@label}'% + ' := C;\n')
#ELSEIF ( %Ndx% = 2)
#APPEND(outputstr,ds + '.' + %'{@label}'%);
#APPEND(projstr,' SELF.' + %'{@label}'% + ' := CORRELATION(' + ds + ',SetVals[C],' + %'{@label}'% + ');\n');
#ELSE
#APPEND(outputstr,',' + ds + '.' + %'{@label}'%);
#APPEND(projstr,' SELF.' + %'{@label}'% + ' := CORRELATION(' + ds + ',SetVals[C],' + %'{@label}'% + ');\n');
#END
#END
#END
#APPEND(outputstr,'];\nOutRecCnt := COUNT(SetVals);\nOneRec := DATASET([{0}],{UNSIGNED1 h});');
#APPEND(projstr,'END;\n P := NORMALIZE(OneRec,OutRecCnt,XF(LEFT,COUNTER));\n');
%outputstr%;
%projstr%;
GenCode := %'outputstr'% + '\n' + %'projstr'%;
//produces this code:
// SetVals := [GFred.f1,GFred.f2,GFred.f3,GFred.f4];
// OutRecCnt := COUNT(SetVals);
// OneRec := DATASET([{0}],{UNSIGNED1 h});
// thislayout XF(OneRec L, INTEGER C) := TRANSFORM
// SELF.myid := C;
// SELF.f1 := CORRELATION(GFred,SetVals[C],f1);
// SELF.f2 := CORRELATION(GFred,SetVals[C],f2);
// SELF.f3 := CORRELATION(GFred,SetVals[C],f3);
// SELF.f4 := CORRELATION(GFred,SetVals[C],f4);
// END;
// P := NORMALIZE(OneRec,OutRecCnt,XF(LEFT,COUNTER));
OUTPUT(GenCode);
OUTPUT(P);
Repare que a estrutura record é exportada como XML (#EXPORTXML). Isso permite que seja possível utilizar o #FOR, uma estrutura de loop que percorre o arquivo XML exportado. Caso persistam dúvidas a respeito do trecho de código referente à exportação do arquivo como XML, há esse outro exemplo retirado da própria documentação da linguagem: https://hpccsystems.com/training/documentation/ecl-language-reference/html/_EXPORTXML.html
- OlivAl01
- Posts: 1
- Joined: Tue Dec 22, 2020 5:01 pm
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest