python_特征转化_apply_FunctionTransformer

python_特征转化_apply_FunctionTransformer

对特征进行转化

# 라이브러리를 임포트합니다.
import numpy as np
from sklearn.preprocessing import FunctionTransformer
​
# 创建矩阵
features = np.array([[2, 3],
                     [2, 3],
                     [2, 3]])
​
features
# # 定义函数
def add_ten(x):
    return x + 10# # 创建转换器
ten_transformer = FunctionTransformer(add_ten)# 装换特征矩阵
ten_transformer.transform(features)
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\preprocessing\_function_transformer.py:97: FutureWarning: The default validate=True will be replaced by validate=False in 0.22.
  "validate=False in 0.22.", FutureWarning)
array([[12, 13],
       [12, 13],
       [12, 13]])
#加载库
import pandas as pd
​
# 创建数据帧
df = pd.DataFrame(features, columns=["feature_1", "feature_2"])# 应用函数
df.apply(add_ten)
feature_1	feature_2
0	12	13
1	12	13
2	12	13

你可能感兴趣的:(数据处理)