python - 如何使用 sklearn.preprocessing 的 StandardScaler 仅标准化一列
问题描述
如果我有清单说
l = [[1, 2], [1, 3], [4, 5], [5, 10]]
我如何只能使用 sklearn.preprocessing -> StandardScaler 规范化第 2、3、5、10 列
解决方案
您不必依赖 的功能StandardScaler
来执行此操作。相反,您可以提取如下的第二列l
:
import numpy as np
# Making an array from list l. dtype needs to be float
# so that the second column of l_arr can be replaced with
# its scaled counterpart without it being truncated to
# integers.
l_arr = np.array(l, dtype=float)
# Extracting the second column from l_arr
l_arr_2nd_col = l_arr[:,1]
# Converting l_arr_2nd_col into a column vector
l_arr_2nd_col = np.atleast_2d(l_arr_2nd_col).T
完成后,您可以StandardScaler
按如下方式使用:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(l_arr_2nd_col)
l_arr_2nd_col_scaled = scaler.transform(l_arr_2nd_col)
# ravel is needed, because l_arr[:,1] has shape (4,), but
# l_arr_2nd_col_scaled has shape (4,1).
l_arr[:,1] = l_arr_2nd_col_scaled.ravel()
此时,您可以这样做:
# Replaces l entirely, including replacing the integers in its
# first column with their floating-point counterparts.
l = l_arr.tolist()
或这个:
# Replace only selected components of l
for l_elem, l1_scaled in zip(l, l_arr[:,1]):
l_elem[1] = l1_scaled
推荐阅读
- python - Selenium 卡在登录阶段,即使真正的浏览器已经登录(需要会话数据)
- tensorflow - 每次使用 Ray Tune 进行第二次试验时内存不足
- javascript - 在 ECMAScript 6 中使用 PHP 变量
- erlang - 没有 RC4 支持,Erlang 将无法启动
- c - C 中的输入与下一个与 C 中的 Struct 合并
- angular - 参数 'user' 隐含一个 'any' type.ts
- javascript - 如何对复杂对象数组进行排序?
- python-3.x - textfsm 返回空值
- python - Python代码来提取多个“开始”和“结束”之间的内容
- python - AttributeError:“类”对象没有属性“rep_call”