python - After scaling or normalizing values the graph is changed Python Sklearn pandas
问题描述
I am trying to scale or normalize the inputs of my training dataset using the sklearn.preprocessing.MinMaxScaler()
. After normalizing the input, I came to know very weird output graph. It was not like the input before normalization. Please have a look at the issue: reference for normalization of pandas columns: https://stackoverflow.com/a/26415620/4948889
def normalize_data(df):
cols = list(df_stock.columns.values)
x = df.values #returns a numpy array
min_max_scaler = sklearn.preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df = pd.DataFrame(x_scaled)
df.columns = cols
return df
df_stock = df.copy()
cols = list(df_stock.columns.values)
print('df_stock.columns.values = ', cols)
df_stock_norm = df_stock.copy()
df_stock_norm = normalize_data(df_stock_norm)
See the before and after graphs of the input dataframes. Before:
plt.figure(figsize=(15, 5));
plt.subplot(1,2,1);
plt.plot(df.open.values[:20], color='red', label='open')
plt.plot(df.close.values[:20], color='green', label='close')
plt.plot(df.low.values[:20], color='blue', label='low')
plt.plot(df.high.values[:20], color='black', label='high')
plt.title('stock price')
plt.xlabel('time [days]')
plt.ylabel('price')
plt.legend(loc='best')
plt.show()
After:
plt.figure(figsize=(15, 5));
plt.plot(df_stock_norm.open.values[:20], color='red', label='open')
plt.plot(df_stock_norm.close.values[:20], color='green', label='low')
plt.plot(df_stock_norm.low.values[:20], color='blue', label='low')
plt.plot(df_stock_norm.high.values[:20], color='black', label='high')
plt.title('stock')
plt.xlabel('time [days]')
plt.ylabel('normalized price')
plt.legend(loc='best')
plt.show()
Explanation:
Please see the high and low values on both the graphs. They are weird and different, which they should be, even after normalization.
Please let me know what I had mistaken.
EDITED:
Have this as sample dataset for testing the above
解决方案
推荐阅读
- r - 将JSON读入R中的数据框
- aem - 在多个位置更改多个 AEM 页面中的文本
- powershell - 使用 PowerShell 上传多部分/表单数据文件
- performance - BizTalk 延迟形状是否占用任何资源或对性能不利?
- javascript - 在 JavaScript 中获取两个 URL(在点击监听器内)
- reactjs - 使用 React Navigation 导航到深度嵌套的屏幕
- rust - “找到结构 `ThereIsNoIteratorInRepetition`” 尝试使用 `quote!` 重复向量时
- kubernetes - POD删除触发时如何自动拍摄PV快照?
- javascript - 在 chart.js(饼图)标签上添加 \n 或换行符
- autodesk-forge - 基于 Three.js 的 Gizmo 不可见