MinMaxScaler无法正确缩放

时间:2018-12-17 16:53:28

标签: python scikit-learn keras

我正在使用从Lynda.com下车的sklearn MinMaxScaler代码来缩放数据集以获取预测代码。特征范围应为(0,1),但我在试用数据中注意到某些列大于1。我相信这导致我的预测代码无法正确显示。有人可以帮忙吗?贝洛是我正在使用的代码...

import pandas as pd
from sklearn.preproMinmaxcessing import MinMaxScaler

# Load training data set from CSV file
training_data_df = pd.read_csv("10596_data_training.csv")

# Load testing data set from CSV file
test_data_df = pd.read_csv("10596_data_test.csv")

# Load the trial data set from CSV file
trial_data_df = pd.read_csv("day05.csv")

# Data needs to be scaled to a small range like 0 to 1 for the neural
# network to work well.
scaler = MinMaxScaler(feature_range=(0, 1))

# Scale both the training inputs and outputs
scaled_training = scaler.fit_transform(training_data_df)
scaled_testing = scaler.transform(test_data_df)
scaled_trial = scaler.transform(trial_data_df)

# Print out the adjustment that the scaler applied to the total_earnings column of data
print("Note: total_hours values were scaled by multiplying by {:.10f} and     adding {:.6f}".format(scaler.scale_[40], scaler.min_[40]))

# Create new pandas DataFrame objects from the scaled data
scaled_training_df = pd.DataFrame(scaled_training,      columns=training_data_df.columns.values)
scaled_testing_df = pd.DataFrame(scaled_testing,     columns=test_data_df.columns.values)
scaled_trial_df = pd.DataFrame(scaled_trial, columns=trial_data_df.columns.values)

# Save scaled data dataframes to new CSV files
scaled_training_df.to_csv("10596_data_training_scaled.csv", index=False)
scaled_testing_df.to_csv("10596_data_test_scaled.csv", index=False)
scaled_trial_df.to_csv("day05_scaled.csv", index=False)

1 个答案:

答案 0 :(得分:1)

您正在对数据的一个子集“训练” MinMaxScaler,然后转换另一个子集。 MinMaxScaler只需减去训练集的最小值,然后除以最大值即可。如果试验集的值大于训练集的最大值或小于训练集的最小值,则您的值将不在[0,1]范围内。这是预期的并且可以接受的。