簡體   English   中英

是否有等效於R中的powertransform的Python函數

[英]Is there a Python function equivalent to powertransform in R

有相當於R中的powertransform的python函數嗎?

在R中: powerTransform(X, family = "bcnPower")

我知道並使用了以下功能/包:


    from scipy.stats import skew,boxcox_normmax
    from scipy.special import boxcox, inv_boxcox
    from  scipy.stats import yeojohnson_normmax
    from scipy.stats import boxcox_llf
    from sklearn.preprocessing import power_transform
    from sklearn.preprocessing import PowerTransformer

我正在嘗試使用python轉換list(vector)。


from scipy.stats import boxcox_normmax
vec =""" 4  5  5  6  5  5  3  7  7  6  5  5  8  8  3  2  3  5 10  6  7  5  2  3  1  3  4  4  5  2  5  4  5  6  5  4  2  6  3 10  4  7  5  2
7  7  3 11  5  4  4  2  2  4  6  3  4  5  6  5  8  7  4  3  5  7  3  3  6  5  3  6  6  3  9  7  9  7  2  4  2  6  4  2  5  3  4  2
7  3  7  5  5  1  5  7  1  4  5  7"""
vec = list(map(int,vec.split()))
print("min  val :",min(vec))
print(boxcox_normmax(vec,method="all"))

最小值:1

[0.62926218 0.58382934]


powerTransform(vec, family="bcnPower")

估計的轉化能力,λ[1] 0.5831778

位置伽瑪固定在其下限[1] 0.1

我想要提供相同輸出參數和結果的python函數。

如果沒有這樣的功能,我可以實現這樣的功能,怎么實現?

在Python中:

from sklearn.preprocessing import PowerTransformer
import pandas as pd
vec = """ 4  5  5  6  5  5  3  7  7  6  5  5  8  8  3  2  3  5 10  6  7  5  2  3  1  3  4  4  5  2  5  4  5  6  5  4  2  6  3 10  4  7  5  2
7  7  3 11  5  4  4  2  2  4  6  3  4  5  6  5  8  7  4  3  5  7  3  3  6  5  3  6  6  3  9  7  9  7  2  4  2  6  4  2  5  3  4  2
7  3  7  5  5  1  5  7  1  4  5  7"""
vec = list(map(int, vec.split()))
pt = PowerTransformer(method='box-cox', standardize=False)
data = pd.DataFrame(vec)
pt.fit(data)
print('Lambda =',pt.lambdas_)
print('First 10 elements:',pt.transform(data)[:10].reshape(1, -1))

#Lambda = [0.58382935]
#First 10 elements: [[2.13498738 2.67039083 2.67039083 3.16269828 2.67039083 2.67039083
#  1.54007639 3.62183527 3.62183527 3.16269828]]

如果您設置standardize=False power_transform會給出相同的結果。

在R中:

p = powerTransform(vec, family = "bcPower")
p$lambda
#      vec 
#0.5838294 
bcPower(vec, lambda=p$lambda)[1:10]
# [1] 2.134987 2.670391 2.670391 3.162698 2.670391 2.670391 1.540076
# [8] 3.621836 3.621836 3.162698

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM