How to standardize arrays in Julia?

I have two arrays (m and n) and each has one column with 100 elements. the first array is sampled from a normal distribution with mean 0 and standard deviation 1, the second one is sampled from a normal distribution with mean 0 and standard deviation 3.

I want to add these two arrays to get a new array. the question is how can I transform or standardize the two arrays?

m = randn(rng, Float64, (100, 1))
100×1 Array{Float64,2}:
  0.18397640704112656
 -1.2763483553703483
  1.031320291955949
 -0.9108047660076668
  0.7546031994011986
 -1.294747093887164
 -0.3089435053618516
  1.3066837674632528
  1.4488604613960603
  0.7781507199519165
  0.6526940815724932
  0.10432909231227107
  0.7417843503426419
  ⋮

using Distributions
d = Normal(0,3)
n = rand(d,100)
100-element Array{Float64,1}:
  4.943981277982362
  0.923775176919609
  1.3120277665167939
 -3.413412184066308
  0.35350629626912417
 -3.0048652472107475
 -4.584157706726311
 -0.683813163262671
 -2.7436028613278802
  2.3003065716043736
  0.3479689939195823
  5.163561281577351
  0.6694418345732615
  ⋮

I tried the fit(ZScoreTransform, m; dims=nothing, center=true, scale=true) in Julia for m and n and I got error for m X must contain at least two columns. and for n TypeError: in keyword argument dims, expected Integer, got a value of type Nothing

looks like the ZScoreTransform is for matrix transformation, is there any way to standardize the arrays in Julia?

thanks :slight_smile:

Any reason you can’t do the normalization by hand? n ./ 3

what is (./) used for ? could you explain that command a bit ?

It’s element-by-element division. These “dotted” operators have documentation here: Multi-dimensional Arrays · The Julia Language

1 Like

standardize(ZScoreTransform, m, dims=1) and standardize(ZScoreTransform, n) work with your example. Using fit and then transform would be equivalent. Note that you need dims=1 to indicate that variables are stored in columns (by default variables are assumed to be in rows, though this is deprecated).

thank you for helping, it worked.

Welcome to the community @Shazman.

Just to make sure that everyone knows, this question was also posted to StackOverflow.

Update: The link I provided now shows "This question was voluntarily removed by its author".