[英]MinmaxScaler: Normalise a 4D array of input
I have a 4D array of input that I would like to normalise using MinMaxScaler
.我有一个 4D 输入数组,我想使用
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)
.在给定的示例中,数组包含 3 个输入样本,其中每个样本的形状为
(1,5,4)
。 Each column of the input represents 1 variable (feature), so each sample has 4 features
.输入的每一列代表 1 个变量(特征),因此每个样本有
4 features
。
I would like to normalise the input data, But MinMaxScaler
expects a 2D array (n_samples, n_features)
like dataframe.我想规范化输入数据,但
MinMaxScaler
需要一个二维数组(n_samples, n_features)
如 dataframe。
How then do I use it to normalise this input 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.您可以使用以下代码规范化 4D 数组。
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将数组传递给 function 之类的
objects,array=norm(A)
it will return a list of MinMax objects and your original array with normalize values.它将返回一个 MinMax 对象的列表和带有规范化值的原始数组。
Output: Output:
" 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 5x5) 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." “如果你想要每个通道的缩放器,你可以将数据的每个通道重塑为 (10000, 5*5) 的形状。每个通道(以前是 5x5)现在是一个长度为 25 的向量,缩放器将起作用. 你必须以与 channel_scalers 中的缩放器相同的方式转换你的评估数据。” Maybe this will help, not sure if this is what you're looking for exactly, but... Python scaling with 4D data
也许这会有所帮助,不确定这是否正是您要找的东西,但是...... Python 用 4D 数据缩放
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.