# How to standardize arrays in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046>\
**Category:** Statistics\
**Tags:** question\
**Created:** [January 27, 2021, 11:01am UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046 "2021-01-27T11:01:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [January 27, 2021, 11:01am UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046/1 "2021-01-27T11:01:49Z")

</div>

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?

```julia
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 🙂

---

<div class="post-metadata">

**Author:** ![nateybear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nateybear/32/20645_2.png) [@nateybear](https://discourse.julialang.org/u/nateybear)\
**Post date:** [January 27, 2021, 11:51am UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046/2 "2021-01-27T11:51:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [January 27, 2021, 12:01pm UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046/3 "2021-01-27T12:01:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nateybear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nateybear/32/20645_2.png) [@nateybear](https://discourse.julialang.org/u/nateybear)\
**Post date:** [January 27, 2021, 12:07pm UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046/4 "2021-01-27T12:07:15Z")

</div>

It’s element-by-element division. These “dotted” operators have documentation here: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting)

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [January 27, 2021, 12:50pm UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046/5 "2021-01-27T12:50:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [January 27, 2021, 1:18pm UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046/6 "2021-01-27T13:18:37Z")

</div>

thank you for helping, it worked.

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [January 27, 2021, 1:25pm UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046/7 "2021-01-27T13:25:43Z")

</div>

Welcome to the community @Shazman.

Just to make sure that everyone knows, this question was also posted to [StackOverflow](https://stackoverflow.com/questions/65909853/how-to-standardize-arrays-in-julia).

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [March 8, 2022, 7:46am UTC](https://discourse.julialang.org/t/how-to-standardize-arrays-in-julia/54046/8 "2022-03-08T07:46:16Z")

</div>

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