np.array(history_data)
时间: 2024-05-26 16:07:49 浏览: 23
`np.array(history_data)`是将Python列表或元组转换为Numpy数组的方式[^1]。
例如:将一个包含10个整数的列表转化为一个Numpy数组,可以使用以下代码:
```python
import numpy as np
history_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
history_np_array = np.array(history_data)
print(history_np_array)
```
输出结果为:
```
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
```
相关问题
data.fillna(method='ffill', inplace=True) date_history = pd.DataFrame(data.iloc[:, 0]) data_history = pd.DataFrame(data.iloc[:, 1]) date_history = np.array(date_history) data_history = [x for item in np.array(data_history).tolist() for x in item] 代码优化
可以将以上代码进行优化,如下所示:
```python
# 使用ffill方法填充缺失值
data.fillna(method='ffill', inplace=True)
# 将data的第一列作为date_history
date_history = data.iloc[:, 0].values.reshape(-1, 1)
# 将data的第二列作为data_history
data_history = data.iloc[:, 1].values.reshape(-1, 1)
# 将date_history和data_history合并成一个数组
history = np.concatenate([date_history, data_history], axis=1)
```
以上代码首先使用`fillna`方法填充缺失值,然后将`data`的第一列作为`date_history`,第二列作为`data_history`,并将它们合并成一个数组`history`。这样可以简化代码,并让代码更易读。
date_history = pd.DataFrame(data_integrity.iloc[:, 0]) dataset_history = pd.DataFrame(data_integrity.iloc[:, 1]) # 数据划分完,数据格式转换 dataset_history = [each[0] for each in np.array(dataset_history.iloc[:, [0]]).tolist()] # 历史数据 data_history_conversion = [{i: dataset_history[i * 96:96 * (i + 1)]} for i in range(int(len(dataset_history) / 96))] # 历史数据转化 data_pre = [each for each in np.array(dataset_pre).tolist()] # 预测所需要的今日数据 data_pre_conversion = np.array([float(item) for item in data_pre[0:96]]) # 预测所需要的今日数据的格式转化 代码优化
以下是对代码的优化:
```python
date_history = pd.DataFrame(data_integrity.iloc[:, 0])
dataset_history = pd.DataFrame(data_integrity.iloc[:, 1])
# 数据划分完,数据格式转换
dataset_history = dataset_history.iloc[:, 0].tolist() # 历史数据
data_history_conversion = [dict(enumerate(dataset_history[i * 96:96 * (i + 1)])) for i in range(len(dataset_history) // 96)] # 历史数据转化
data_pre = np.array(dataset_pre).flatten().astype(float) # 预测所需要的今日数据
```
主要优化点有:
1. `dataset_history` 可以直接使用 `tolist()` 方法转换为列表,不需要使用 `np.array` 和 `iloc` 索引。
2. `data_history_conversion` 可以使用 `enumerate` 函数代替手动计算索引,可以使代码更加简洁。
3. `data_pre` 可以使用 `flatten()` 方法将多维数组转换为一维数组,并使用 `astype(float)` 方法将字符串转换为浮点数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)