5
$\begingroup$

I have a data set of 100 rows and 10 columns. I have imported it as Tabular. Then, I want to apply log followed by z-score to all columns except one, which represents the class label.

What is the easiest way to do so?

$\endgroup$

1 Answer 1

4
$\begingroup$

A general approach would be to use TransformColumns together with ColumnwiseThread and Standardize.

tab = Tabular[{{2., 2.78, "A"}, {1., 3.14, "B"}, {3., 1.68, "C"}}, 
               {"x", "y", "class"}]

TransformColumns[tab, "x" -> Function[ColumnwiseThread[Standardize[Log[#x]]]]]

enter image description here

However, currently, it seems you would have to do this "manually" for every column. I see two possible ways to make this apply to all columns (except the "class" one):

(* Method 1 *)
ToTabular[KeyValueMap[Function[#1 -> If[#1 != "class", Standardize[Log[#2]], #2]],
   FromTabular[tab, "Columns"]], "Columns"]

(* Method 2 *)
ToTabular[KeyValueMap[
  Function[{col, vals}, col -> If[col != "class", Standardize[Log[vals]], vals]], 
  FromTabular[tab, "Columns"]], "Columns"]

enter image description here

$\endgroup$
2
  • $\begingroup$ Also TransformColumns[tab, Map[(c |-> c -> Function[ColumnwiseThread[Standardize[Log[Slot[c]]]]])]@ Select[FreeQ["class"]]@ColumnKeys[tab]] $\endgroup$ Commented 2 days ago
  • $\begingroup$ Many thanks! Much appreciated. $\endgroup$ Commented yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.