ValueError:操作数不能与形状一起广播-inverse_transform- Python [英] ValueError: operands could not be broadcast together with shapes - inverse_transform- Python

查看:637
本文介绍了ValueError:操作数不能与形状一起广播-inverse_transform- Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 ValueError 问题已经问过许多。我仍在努力寻找答案,因为我在代码中使用了 inverse_transform

I know ValueError question has been asked many times. I am still struggling to find an answer because I am using inverse_transform in my code.

说我有一个数组 a

a.shape
> (100,20)

和另一个数组 b

b.shape
> (100,3)

当我执行 np.concatenate

hat = np.concatenate((a, b), axis=1)

现在 hat 的形状是

hat.shape    
(100,23)

之后,我尝试执行此操作

After this, I tried to do this,

inversed_hat = scaler.inverse_transform(hat)

执行此操作时,出现错误:

When I do this, I am getting an error:


ValueError:操作数不能与形状(100,23)(25,)(100,23)一起广播

ValueError: operands could not be broadcast together with shapes (100,23) (25,) (100,23)

此广播错误是否在 inverse_transform 中?任何建议都会有所帮助。

Is this broadcast error in inverse_transform? Any suggestion will be helpful. Thanks in advance!

推荐答案

尽管您未指定,但我假设您使用的是<$ c $ scikit Learn的 StandardScaler 中的c> inverse_transform()。您需要先拟合数据。

Although you didn't specify, I'm assuming you are using inverse_transform() from scikit learn's StandardScaler. You need to fit the data first.

import numpy as np
from sklearn.preprocessing import MinMaxScaler


In [1]: arr_a = np.random.randn(5*3).reshape((5, 3))

In [2]: arr_b = np.random.randn(5*2).reshape((5, 2))

In [3]: arr = np.concatenate((arr_a, arr_b), axis=1)

In [4]: scaler = MinMaxScaler(feature_range=(0, 1)).fit(arr)

In [5]: scaler.inverse_transform(arr)
Out[5]:
array([[ 0.19981115,  0.34855509, -1.02999482, -1.61848816, -0.26005923],
       [-0.81813499,  0.09873672,  1.53824716, -0.61643731, -0.70210801],
       [-0.45077786,  0.31584348,  0.98219019, -1.51364126,  0.69791054],
       [ 0.43664741, -0.16763207, -0.26148908, -2.13395823,  0.48079204],
       [-0.37367434, -0.16067958, -3.20451107, -0.76465428,  1.09761543]])

In [6]: new_arr = scaler.inverse_transform(arr)

In [7]: new_arr.shape == arr.shape
Out[7]: True

这篇关于ValueError:操作数不能与形状一起广播-inverse_transform- Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆