在lstm时间序列预测python_Keras中的多变量时间序列预测当中进行天气预测资料,LabelEncoder 是对不连续的数字或者文本进行编号时,发现处理导入的csv文件会出错,而自己编写的x却能处理,有遇到同样问题的么?
下面是自己随便写的代码:
from sklearn.preprocessing import LabelEncoder from pandas import read_csv import numpy as np dataset = read_csv('data_set/air_pollution_new.csv', encoding='ISO-8859-1', header=0, index_col=0) values = dataset.values print('原始数据') print(values[0:5]) print(dataset[0:5]) # 由于4列的风向是标签,编码成整数 encoder = LabelEncoder() # 简单来说 LabelEncoder 是对不连续的数字或者文本进行编号 x = np.array(['SE', 'SE', 'SE', 'SA', 'NW']) y = np.array(values[:5, 4]) z = x + y print(z) print(x) print(y) print(type(x)) print(type(y)) print('标签编码') x = encoder.fit_transform(x) print(x) z = encoder.fit_transform(z) y = encoder.fit_transform(y)
这是输出的结果:
原始数据
[[129.0 -16 -4.0 1020.0 'SE' 1.79 0 0]
[148.0 -15 -4.0 1020.0 'SE' 2.68 0 0]
[159.0 -11 -5.0 1021.0 'SE' 3.57 0 0]
[181.0 -7 -5.0 1022.0 'SE' 5.36 1 0]
[138.0 -7 -5.0 1022.0 'SE' 6.25 2 0]]
pollution dew temp press wnd_dir wnd_spd snow rain
date
2010-01-02 00:00:00 129.0 -16 -4.0 1020.0 SE 1.79 0 0
2010-01-02 01:00:00 148.0 -15 -4.0 1020.0 SE 2.68 0 0
2010-01-02 02:00:00 159.0 -11 -5.0 1021.0 SE 3.57 0 0
2010-01-02 03:00:00 181.0 -7 -5.0 1022.0 SE 5.36 1 0
2010-01-02 04:00:00 138.0 -7 -5.0 1022.0 SE 6.25 2 0
['SESE' 'SESE' 'SESE' 'SASE' 'NWSE']
['SE' 'SE' 'SE' 'SA' 'NW']
['SE' 'SE' 'SE' 'SE' 'SE']
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
标签编码
[2 2 2 1 0]
Traceback (most recent call last):
File "H:/LSTM-master/LSTM系列/LSTM多变量2/1.py", line 25, in <module>
z = encoder.fit_transform(z)
File "C:\Users\PycharmProjects\pythonProject1\venv\lib\site-packages\sklearn\preprocessing\_label.py", line 117, in fit_transform
self.classes_, y = _unique(y, return_inverse=True)
File "C:\Users\PycharmProjects\pythonProject1\venv\lib\site-packages\sklearn\utils\_encode.py", line 31, in _unique
return _unique_python(values, return_inverse=return_inverse)
File "C:\Users\PycharmProjects\pythonProject1\venv\lib\site-packages\sklearn\utils\_encode.py", line 133, in _unique_python
uniques.extend(missing_values.to_list())
AttributeError: 'MissingValues' object has no attribute 'to_list'