2 回答
TA贡献1875条经验 获得超5个赞
使用熊猫:
数据:
X = [[5.1, 3.5, 1.4, 0.2, 0.0],
[4.9, 3.0, 1.4, 0.2, 0.0],
[4.7, 3.2, 1.3, 0.2, 0.0],
[4.6, 3.1, 1.5, 0.2, 0.0],
[5.0, 3.6, 1.4, 0.2, 0.0],
[5.4, 3.9, 1.7, 0.4, 0.0]]
代码:
编写函数def valores以产生所需的转换
创建一个数据框X
应用于valores数据框中的相应列
import pandas as pd
def valores(x):
return [round(((y - x.mean()) / x.std()), 4) for y in x]
df = pd.DataFrame(X)
df[[0, 1, 2, 3]] = df[[0, 1, 2, 3]].apply(lambda x: valores(x))
输出:
0 1 2 3 4
0.5207 0.3401 -0.3627 -0.4082 0.0
-0.1736 -1.1175 -0.3627 -0.4082 0.0
-0.8678 -0.5345 -1.0882 -0.4082 0.0
-1.2149 -0.8260 0.3627 -0.4082 0.0
0.1736 0.6316 -0.3627 -0.4082 0.0
1.5620 1.5062 1.8137 2.0412 0.0
TA贡献1780条经验 获得超5个赞
不优雅:
out = []
for i in range(1+len(valores)//len(X)):
aux = []
for j in range(len(X[0])):
aux.append(valores[i+len(X)*j])
out.append(aux)
print(out)
[[0.5207, 0.3401, -0.3627, -0.4082, 0.0], [-0.1736, -1.1175, -0.3627, -0.4082, 0.0], [-0.8678, -0.5345, -1.0882, -0.4082, 0.0], [-1.2149, -0.826, 0.3627, -0.4082, 0.0], [0.1736, 0.6316, -0.3627, -0.4082, 0.0], [1.562, 1.5062, 1.8137, 2.0412, 0.0]]
添加回答
举报