# MinmaxScaler: Normalise a 4D array of input

## Question:

I have a 4D array of input that I would like to normalise using `MinMaxScaler`

. For simplicity, I give an example with the following array:

```
A = np.array([
[[[0, 1, 2, 3],
[3, 0, 1, 2],
[2, 3, 0, 1],
[1, 3, 2, 1],
[1, 2, 3, 0]]],
[[[9, 8, 7, 6],
[5, 4, 3, 2],
[0, 9, 8, 3],
[1, 9, 2, 3],
[1, 0, -1, 2]]],
[[[0, 7, 1, 2],
[1, 2, 1, 0],
[0, 2, 0, 7],
[-1, 3, 0, 1],
[1, 0, 1, 0]]]
])
A.shape
(3,1,5,4)
```

In the given example, the array contains 3 input samples, where each sample has the shape `(1,5,4)`

. Each column of the input represents 1 variable (feature), so each sample has `4 features`

.

I would like to normalise the input data, But `MinMaxScaler`

expects a 2D array `(n_samples, n_features)`

like dataframe.

How then do I use it to normalise this input data?

## Answers:

" If you want a scaler for each channel, you can reshape each channel of the data to be of shape (10000, 5*5). Each channel (which was previously 5×5) is now a length 25 vector, and the scaler will work. You’ll have to transform your evaluation data in the same way with the scalers in channel_scalers."

Maybe this will help, not sure if this is what you’re looking for exactly, but…

Python scaling with 4D data

Vectorize the data

```
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
A_sq = np.squeeze(A)
print(A_sq.shape)
# (3, 5, 4)
scaler.fit(np.squeeze(A_sq).reshape(3,-1)) # reshape to (3, 20)
#MinMaxScaler()
```

You can use the below code to normalize 4D array.

```
from sklearn.preprocessing import MinMaxScaler, StandardScaler
scaler = MinMaxScaler(feature_range=(0, 1))
def norm(arr):
arrays_list=list()
objects_list=list()
for i in range(arr.shape[0]):
temp_arr=arr[i]
temp_arr=temp_arr[0]
scaler.fit(temp_arr)
temp_arr=scaler.transform(temp_arr)
objects_list.append(scaler)
arrays_list.append([temp_arr])
return objects_list,np.array(arrays_list)
```

pass the array to the function like

```
objects,array=norm(A)
```

it will return a list of MinMax objects and your original array with normalize values.

**Output:**