我根据列来缩放矩阵,如下所示:
scaler = MinMaxScaler(feature_range=(-1, 1))
data = np.array([[-1, 2], [-0.5, 6], [0, 10], [1, 18]])
scaler = scaler.fit(data)
data_scaled = scaler.transform(data)
data_scaled
给了我以下内容:
array([[-1. , -1. ],
[-0.5, -0.5],
[ 0. , 0. ],
[ 1. , 1. ]])
哪个是所需的输出。但是,我试图反转此矩阵的第一列的缩放,所以我尝试了以下(错误显示在每行代码下面):
scaler.inverse_transform(data_scaled[:,1].reshape(1,-1))
Traceback (most recent call last):
File "c:\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-38-6316f51586e7>", line 1, in <module>
scaler.inverse_transform(data_scaled[:,1].reshape(1,-1))
File "c:\anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 385, in inverse_transform
X -= self.min_
ValueError: operands could not be broadcast together with shapes (1,4) (2,) (1,4)
另外,我试过了:
scaler.inverse_transform(data_scaled[:,1].reshape(-1,1))
Traceback (most recent call last):
File "c:\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-39-397382ddb3fd>", line 1, in <module>
scaler.inverse_transform(data_scaled[:,1].reshape(-1,1))
File "c:\anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 385, in inverse_transform
X -= self.min_
ValueError: non-broadcastable output operand with shape (4,1) doesn't match the broadcast shape (4,2)
那么,如何重新缩放该矩阵的第一列?
答案 0 :(得分:5)
scaler
记得你传递了一个带有两列的2D输入,并且假设传递给它的所有后续数据都具有相同数量的特征/列。
如果它只是您想要的第一列,您仍然需要使用相同数量的列传递inverse_transform
输入。从结果中取出第一列并丢弃其余部分。
scaler.inverse_transform(data_scaled)[:, [0]]
array([[-1. ],
[-0.5],
[ 0. ],
[ 1. ]])
这有点浪费,但是对sklearn API的限制。
答案 1 :(得分:0)
sklearn.preprocessing.MinMaxScaler具有min_和scale _
之类的属性您可以将该特定列的这些属性转移到新的空minmaxscaler 可以解决您的问题。
属性传输