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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
import math import numpy as np import pandas as pd from matplotlib import pyplot as plt data = pd.read_csv('Advertising.csv') datax = data['newspaper'][0:150]
datax_1 = data['newspaper'][150:200]
datay = data['sales'][0:150] datay_1 = data['sales'][150:200] plt.plot(datax, datay, "ob")
def jie_w(datax,datay): suma = 0; num = 0; sum1=0; for i in range(len(datax)): suma = (datay[i]*(datax[i]-sum(datax)/len(datax)))+suma for i in range(len(datax)): sum1 = sum1 + datax[i]*datax[i] num = sum1 - (sum(datax)*sum(datax))/(len(datax)) return suma/num print("w:",jie_w(datax,datay)) def jie_b(datax,datay): suma = 0; tt = jie_w(datax,datay) for i in range(len(datax)): suma = suma + (datay[i]-tt*datax[i]) num = suma / (len(datax)) return num k=jie_w(datax,datay) b=jie_b(datax,datay) print("b",b) y1=[0,datax.max()] y2=[b,datax.max()*k+b] plt.plot(y1,y2,'r') plt.show() datax_2 = 0 for i in range(len(datax_1)): datax_2 = datax_2+math.pow(datax_1[i+150]*k+b-datay_1[i+150],2) wucha = datax_2/len(datax_1) print("均方误差:",wucha)
sddsum = 0 addsum = 0 datay_av = np.mean(datay_1) for i in range(len(datax_1)): sddsum = math.pow(datax_1[i+150]*k+b-datay_av,2) addsum = math.pow(datay_1[i+150] - datay_av,2) R = sddsum/addsum print("R的平方分析:",R)
|