import numpy as np
from sklearn.linear_model import LinearRegression

# 输入标准数据
standard_data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 
standard_time = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 

# 进行线性回归分析得到标准方程
regressor = LinearRegression()
regressor.fit(standard_time.reshape(-1, 1), standard_data)

# 输入待测数据
test_data = np.array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20]) 
test_time = np.array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20]) 

# 使用标准方程进行预测
# print(regressor.predict(standard_time.reshape(-1,1)))
predicted_data = regressor.predict(test_time.reshape(-1, 1))

# 计算残差
residuals = predicted_data - test_data
residual_std = np.std(residuals)

# 置信水平
# 0.95与1.96一一对应，如果要修改置信度，同时也需要修改1.96数值。具体可查表
confidence_level = 0.95
margin_of_error = 1.96 * (residual_std / np.sqrt(len(test_data)))  # 1.96是95%置信水平的Z分数


lower_limit = predicted_data - margin_of_error
upper_limit = predicted_data + margin_of_error


# 判断是否误差过大
error_flag = any(predicted_data < lower_limit) or any(predicted_data > upper_limit)

if error_flag:
    print(f"预测结果不具有{confidence_level*100}%的可信度")
else:
    # 结果导出为四位小数
    outdata = predicted_data.round(4)
    print("预测数据：", outdata)