Write a Python function that performs feature scaling on a dataset using both standardization and min-max normalization. The function should take a 2D NumPy array as input, where each row represents a data sample and each column represents a feature. It should return two 2D NumPy arrays: one scaled by standardization and one by min-max normalization. Make sure all results are rounded to the nearest 4th decimal.
Example:
- Input:
data = np.array([[1, 2], [3, 4], [5, 6]]) - Output:
([[-1.2247, -1.2247], [0.0, 0.0], [1.2247, 1.2247]], [[0.0, 0.0], [0.5, 0.5], [1.0, 1.0]])
这道题要求用两种方法对数据进行标准化,这个是比较简单的,用z-score和min-max,所以我们分别写两个小函数来标准化。z-score用标准正态分布,而min-max一般我们用[0, 1]区间,但也可以用任意的[a, b]区间,所以这里的函数,就写个通用的,但默认是一般使用的[0, 1]。
然后就是对着数据的每一列进行标化,存一个新的数据框。
import numpy as np
def z_score(x):
return (x - np.mean(x))/np.std(x)
def min_max(x, a = 0, b = 1):
return (x - np.min(x)) / (np.max(x) - np.min(x)) * (b-a) + a
def feature_scaling(data: np.ndarray) -> (np.ndarray, np.ndarray):
m, n = data.shape
standardized_data = np.zeros([m, n])
normalized_data = np.zeros([m, n])
for j in range(n):
x = data[:, j]
standardized_data[:, j] = np.round(z_score(x), 4)
normalized_data[:, j] = np.round(min_max(x), 4)
return standardized_data, normalized_data
