用法:
class dask_ml.preprocessing.StandardScaler(*, copy=True, with_mean=True, with_std=True)
通過去除均值和縮放到單位方差來標準化特征。
樣本
x
的標準分數計算如下:z = (x - u) /s
其中
u
是訓練樣本的平均值,如果with_mean=False
則為零,而s
是訓練樣本的標準差,如果with_std=False
則為 1。通過計算訓練集中樣本的相關統計數據,居中和縮放在每個特征上獨立發生。然後使用
transform()
存儲平均值和標準偏差以用於以後的數據。數據集的標準化是許多機器學習估計器的共同要求:如果單個特征或多或少看起來不像標準正態分布數據(例如,均值和單位方差為 0 的高斯),它們可能表現不佳。
例如,學習算法的目標函數中使用的許多元素(例如支持向量機的 RBF 核或線性模型的 L1 和 L2 正則化器)假設所有特征都以 0 為中心並且具有相同順序的方差。如果一個特征的方差比其他特征大幾個數量級,它可能會主導目標函數並使估計器無法按預期正確地從其他特征中學習。
這個縮放器也可以通過傳遞
with_mean=False
應用於稀疏 CSR 或 CSC 矩陣,以避免破壞數據的稀疏結構。在用戶指南中閱讀更多信息。
- copy:布爾,默認=真
如果為 False,請盡量避免複製並改為進行就地縮放。這不能保證總是在原地工作;例如如果數據不是 NumPy 數組或 scipy.sparse CSR 矩陣,則仍可能返回一個副本。
- with_mean:布爾,默認=真
如果為 True,則在縮放之前將數據居中。當嘗試在稀疏矩陣上時,這不起作用(並且會引發異常),因為將它們居中需要構建一個密集矩陣,在常見的用例中,它可能太大而無法放入內存。
- with_std:布爾,默認=真
如果為 True,則將數據縮放到單位方差(或等效地,單位標準差)。
- scale_:ndarray 形狀 (n_features,) 或無
數據的每個特征相對縮放以實現零均值和單位方差。通常這是使用
np.sqrt(var_)
計算的。如果方差為零,我們無法獲得單位方差,並且數據留下as-is,給出比例因子1。當with_std=False
時scale_
等於None
。- mean_:ndarray 形狀 (n_features,) 或無
訓練集中每個特征的平均值。當
with_mean=False
時等於None
。- var_:ndarray 形狀 (n_features,) 或無
訓練集中每個特征的方差。用於計算
scale_
。當with_std=False
時等於None
。- n_features_in_:int
擬合期間看到的特征數。
- feature_names_in_:ndarray 形狀(
n_features_in_
,) 擬合期間看到的特征名稱。僅當
X
具有全為字符串的函數名稱時才定義。- n_samples_seen_:形狀的 int 或 ndarray (n_features,)
估計器為每個特征處理的樣本數。如果沒有丟失的樣本,
n_samples_seen
將是一個整數,否則它將是一個 dtype 數組 int. 如果使用sample_weights
,它將是一個浮點數(如果沒有丟失數據)或一個 dtype 浮點數總和到目前為止看到的權重。將在新調用時重置以適應,但在partial_fit
調用中遞增。
參數:
屬性:
注意:
NaN 被視為缺失值:在擬合中被忽略,在變換中保持不變。
我們對標準差使用有偏差的估計器,相當於
numpy.std(x, ddof=0)
。請注意,ddof
的選擇不太可能影響模型性能。有關不同縮放器、轉換器和規範器的比較,請參閱示例/預處理/plot_all_scaling.py。
例子:
>>> from sklearn.preprocessing import StandardScaler >>> data = [[0, 0], [0, 0], [1, 1], [1, 1]] >>> scaler = StandardScaler() >>> print(scaler.fit(data)) StandardScaler() >>> print(scaler.mean_) [0.5 0.5] >>> print(scaler.transform(data)) [[-1. -1.] [-1. -1.] [ 1. 1.] [ 1. 1.]] >>> print(scaler.transform([[2, 2]])) [[3. 3.]]
相關用法
- Python dask_ml.preprocessing.MinMaxScaler用法及代碼示例
- Python dask_ml.preprocessing.Categorizer用法及代碼示例
- Python dask_ml.preprocessing.OrdinalEncoder用法及代碼示例
- Python dask_ml.preprocessing.LabelEncoder用法及代碼示例
- Python dask_ml.preprocessing.PolynomialFeatures用法及代碼示例
- Python dask_ml.preprocessing.QuantileTransformer用法及代碼示例
- Python dask_ml.preprocessing.RobustScaler用法及代碼示例
- Python dask_ml.preprocessing.BlockTransformer用法及代碼示例
- Python dask_ml.preprocessing.DummyEncoder用法及代碼示例
- Python dask_ml.wrappers.ParallelPostFit用法及代碼示例
- Python dask_ml.feature_extraction.text.CountVectorizer用法及代碼示例
- Python dask_ml.linear_model.LinearRegression用法及代碼示例
- Python dask_ml.wrappers.Incremental用法及代碼示例
- Python dask_ml.metrics.mean_squared_log_error用法及代碼示例
- Python dask_ml.model_selection.GridSearchCV用法及代碼示例
- Python dask_ml.feature_extraction.text.FeatureHasher用法及代碼示例
- Python dask_ml.ensemble.BlockwiseVotingClassifier用法及代碼示例
- Python dask_ml.model_selection.train_test_split用法及代碼示例
- Python dask_ml.decomposition.PCA用法及代碼示例
- Python dask_ml.feature_extraction.text.HashingVectorizer用法及代碼示例
注:本文由純淨天空篩選整理自dask.org大神的英文原創作品 dask_ml.preprocessing.StandardScaler。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。