编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
import numpy as np

# 数据清洗
data = np.array([1, 2, np.nan, 4, 5])
data = np.where(np.isnan(data), 0, data)

# 数据融合
data1 = np.array([1, 2, 3])
data2 = np.array([4, 5, 6])
data = np.hstack((data1, data2))

# 数据减噪
data = np.medfilt(data, kernel_size=3)

import pandas as pd

# 描述性分析
data = pd.DataFrame({'speed': [1, 2, 3, 4, 5], 'flow': [10, 20, 30, 40, 50]})
print(data.describe())

# 异常检测
data['flow'] = data['flow'].replace(0, np.nan)
data = data.dropna()

# 趋势分析
data['date'] = pd.date_range('2022-01-01', periods=len(data), freq='D')
data['day_of_week'] = data['date'].dt.day_name()
data['week_of_year'] = data['date'].dt.weekofyear
data.groupby('day_of_week').mean()

from sklearn.linear_model import LinearRegression

# 训练数据
X = data[['speed', 'day_of_week', 'week_of_year']]
X = np.array(X)
y = data['flow']
y = np.array(y).reshape(-1, 1)

# 建立模型
model = LinearRegression()
model.fit(X, y)

# 预测结果
pred = model.predict(X)

# 预测未来一周的车流量
future_data = pd.DataFrame({'speed': [1, 2, 3, 4, 5], 'day_of_week': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], 'week_of_year': [52, 1, 2, 3, 4]})
future_data = np.array(future_data)
pred = model.predict(future_data)

# 调整交通信号灯的亮灭时间
light_data = np.array([1, 2, 3, 4, 5])
light_data = np.where(pred > 30, 1, 0)

# 计算出每辆车辆的最佳路径
path_data = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])
path_data = np.where(light_data == 1, [1, 2], path_data)