ValueError:操作数无法与形状一起广播 - inverse_transform- Python

时间:2017-08-23 18:29:07

标签: python arrays numpy scikit-learn broadcast

我知道很多ValueError已经问过inverse_transform个问题a。我仍在努力寻找答案,因为我在代码中使用了a.shape > (100,20)

说我有一个数组b

b.shape
> (100,3)

和另一个数组np.concatenate

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

当我做hat时,

hat.shape    
(100,23)

现在inversed_hat = scaler.inverse_transform(hat) 的形状是

inverse_transform

在此之后,我试图这样做,

for arg in "$@"
do
  if [ "$arg" == "refs/heads/master" ]
  then
    DEST="/path/to/production"
    git --work-tree=$DEST checkout -f
  elif [ "$arg" == "refs/heads/dev" ]
  then
    DEST="/path/to/dev"
    git --work-tree=$DEST checkout -f
  fi
done

当我这样做时,我收到一个错误:

  

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

这是def parse_record(record): key = ["id", "name", "year", "month", "day", "hour", "central pressure", "radius", "speed", "lat", "long"] record = [i if i else 0 for i in record] value = [str(record[1]), str(record[0]), int((record[2])[:4]), int((record[2])[5:7]),int((record[2])[8:10]), int((record[2])[10:13]),float(record[6]), float(record[7]),float(record[8]),float(record[4]), float(record[5])] record_dictionary = dict(zip(key,value)) return record_dictionary record = ["unnamed", "AU190607_01U", "1907-01-17 23:00", "T", "-13", "146.5", "994", "", "10.3"] r1 = ["unnamed", "AU190607_01U", "1907-01-17 23:00", "T", "-13", "146.5", "994", "100", "10.3"] r2 = ["unnamed", "AU190607_01U", "1907-01-17 23:00", "T", "-13", "146.5", "994", "", "10.3"] parse_record(r1) parse_record(r2) 的广播错误吗?任何建议都会有所帮助。提前谢谢!

2 个答案:

答案 0 :(得分:2)

虽然您没有指定,但我假设您使用的是来自scikit learn inverse_transform() StandardScaler。您需要先填入数据。

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

答案 1 :(得分:0)

似乎您正在使用sklearn.preprocessing的预拟合 scaler 对象。 如果是真的,根据我的说法,您用于拟合的数据的维度为(x,25),而数据形状的维度为(x,23),这就是出现此问题的原因。