简体   繁体   English

手动归一化功能执行时间太长

[英]Manual normalization function taking too long to execute

I am trying to implement a normalization function manually rather than using the scikit learn's one. 我正在尝试手动实现标准化功能,而不是使用scikit Learn的功能。 The reason is that, I need to define the maximum and minimum parameters manually and scikit learn doesn't allow that alteration. 原因是,我需要手动定义最大和最小参数,而scikit learning不允许这种更改。

I successfully implemented this to normalize the values between 0 and 1. But it is taking a very long time to run. 我成功实现了该操作,以标准化0到1之间的值。但是,这需要很长时间才能运行。

Question: Is there another efficient way I can do this? 问题: 还有其他有效的方法可以做到这一点吗? How can I make this execute faster. 我怎样才能使它更快地执行。

Shown below is my code: 下面显示的是我的代码:

scaled_train_data = scale(train_data)

def scale(data):
    for index, row in data.iterrows():
        X_std = (data.loc[index, "Close"] - 10) / (2000 - 10)
        data.loc[index, "Close"] = X_std

    return data

2000 and 10 are the attributes that i defined manually rather than taking the minimum and the maximum value of the dataset. 2000和10是我手动定义的属性,而不是采用数据集的最小值和最大值。

Thank you in advance. 先感谢您。

Use numpy's matrix.you can also set your min and max mannually. 使用numpy的矩阵。您还可以手动设置最小值和最大值。

import numpy as np
data = np.array(df)
_min = np.min(data, axis=0)
_max = np.max(data, axis=0)
normed_data = (data - _min) / (_max - _min)

Why loop? 为什么循环? You can just use 你可以用

train_data['close'] = (train_data['close'] - 10)/(2000 - 10) 

to make use of vectorized numpy functions. 利用向量化的numpy函数。 Of course, you could also put this in a function, if you prefer. 当然,如果愿意,也可以将其放在函数中。

Alternatively, if you want to rescale to a linear range, you could use http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html . 另外,如果您想重新缩放到线性范围,则可以使用http://scikit-learn.org/stable/modules/generation/sklearn.preprocessing.MinMaxScaler.html The advantage of this is that you can save it and then rescale the test data in the same manner. 这样做的好处是您可以保存它,然后以相同的方式重新缩放测试数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM