根据问题一步一步求就好了,非常简单的一道题。
代码如下:
x=[0, 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
y=[20, 21, 23, 28, 36, 45, 57, 69, 85, 102, 120, 138, 161, 190, 216, 246, 276, 310, 343, 381, 418, 459, 505, 549, 596, 645, 697, 750, 805, 862]
#简约求y_new;
y_new=[20+i*i for i in x]
print(y_new)
#初始化delta_y
delta_y=[]
for j in range(30):
delta_y.append(y_new[j]-y[j])
print(delta_y)
#可视化图
import matplotlib.pyplot as plt #导入可视化函数
# plot curve 1
plt.plot(x,delta_y)
# show the picture
plt.show()
#计算方差
import numpy as np
y=[20, 21, 23, 28, 36, 45, 57, 69, 85, 102, 120, 138, 161, 190, 216, 246, 276, 310, 343, 381, 418, 459, 505, 549, 596, 645, 697, 750, 805, 862]
#求方差
y_var = np.var(y)
print("方差为:%f" %y_var)
结果如下: