Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from math import sqrt
- import matplotlib.pyplot as plt
- from scipy.interpolate import interp1d
- s=[1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3]
- Q=[5.21, 4.196, 3.759, 3.672, 4.592, 4.621, 5.758, 7.7173, 9.269]
- s2=[]
- sR=[]
- R2=[]
- for i in range(len(s)):
- s2.append([0])
- s2[i]= s[i] ** 2
- for i in range(len(s)):
- sR.append([0])
- sR[i]= s[i] * Q[i]
- sum_h2=np.sum(s2)
- sum_h=np.sum(s)
- sum_R=np.sum(Q)
- sum_hR=np.sum(sR)
- o=np.array([[sum_h2, sum_h], [sum_h, len(s)]])
- p=np.linalg.det(o) #Дельта
- z=np.array([[sum_hR, sum_h], [sum_R, len(s)]])
- j=np.linalg.det(z) #Дельта a
- a=j/p
- w=np.array([[sum_h2, sum_hR],[sum_h, sum_R]])
- e=np.linalg.det(w) #Дельта b
- b=e/p
- print('Для уравнения линейной регрессионной модели y=ax+b')
- print(f'Коэффициент линейной регрессионной модели b={b}')
- print(f'Коэффициент линейной регрессионной модели a={a}')
- h_medium=sum_h/len(s)
- R_medium=sum_R/len(s)
- hR_medium=sum_hR/len(s)
- Dh= (sum_h2 / len(s)) - h_medium ** 2 #Дисперсия Dh
- for i in range(len(Q)):
- R2.append([0])
- R2[i]= Q[i] ** 2
- sum_R2=np.sum(R2)
- DR= (sum_R2 / len(s)) - R_medium ** 2 #Дисперсия DR
- sigma_h=sqrt(Dh) #Среднеквадратическое отклонение сигма h
- sigma_R=sqrt(DR) #Среднеквадратическое отклонение сигма R
- r=(hR_medium-h_medium*R_medium)/(sigma_h*sigma_R) #Коэффицент корреляции
- Coefficient_Determination_Linear_Regression=r**2
- print('Коэффициент детерминации R2=',Coefficient_Determination_Linear_Regression)
- #Расчёты для линейной регрессионной модели!!! КОНЕЦ
- #Расчёты для полиномиальной регрессионной модели степени 2!!! НАЧАЛО
- print('Для уравнения полиномиальной регрессионной модели степени 2 R=Ch^2+Dh+K')
- h4=[]
- h3=[]
- h2R=[]
- for i in range(len(s)):
- h4.append([0])
- h4[i]= s[i] ** 4
- for i in range(len(s)):
- h3.append([0])
- h3[i]= s[i] ** 3
- for i in range(len(s)):
- h2R.append([0])
- h2R[i]= (s[i] ** 2) * Q[i]
- sum_h4=np.sum(h4)
- sum_h3=np.sum(h3)
- sum_h2R=np.sum(h2R)
- terra1=np.array([[sum_h4, sum_h3, sum_h2], [sum_h3, sum_h2, sum_h], [sum_h2, sum_h, len(s)]])
- delta=np.linalg.det(terra1) #Дельта
- terra2=np.array([[sum_h2R, sum_h3, sum_h2], [sum_hR, sum_h2, sum_h], [sum_R, sum_h, len(s)]])
- delta1=np.linalg.det(terra2) #Дельта C
- terra3=np.array([[sum_h4, sum_h2R, sum_h2], [sum_h3, sum_hR, sum_h], [sum_h2, sum_R, len(s)]])
- delta2=np.linalg.det(terra3) #Дельта D
- terra4=np.array([[sum_h4, sum_h3, sum_h2R],[sum_h3, sum_h2, sum_hR],[sum_h2, sum_h, sum_R]])
- delta3=np.linalg.det(terra4) #Дельта K
- C=delta1/delta
- D=delta2/delta
- K=delta3/delta
- print('Коэффициент полиномиальной регрессионной модели степени 2 C=',C)
- print('Коэффициент полиномиальной регрессионной модели степени 2 D=',D)
- print('Коэффициент полиномиальной регрессионной модели степени 2 K=',K)
- #РАСЧЁТЫ КОЭФФИЦИЕНТА ДЕТЕРМИНАЦИИ ДЛЯ ПОЛИНОМА!!! НАЧАЛО
- T=[]
- GG=[]
- UU=[]
- for i in range(len(s)):
- GG.append([0])
- GG[i]= C * (s[i] ** 2) + D * s[i] + K
- for i in range(len(s)):
- T.append([0])
- T[i]= (Q[i] - (C * (s[i] ** 2) + D * s[i] + K)) ** 2
- sum_T=np.sum(T)
- for i in range(len(s)):
- UU.append([0])
- UU[i]= (Q[i] - R_medium) ** 2
- sum_UU=np.sum(UU)
- Coefficient_Determination_Polinom_Regression=1-(sum_T/sum_UU)
- print('Коэффициент детерминации R2=',Coefficient_Determination_Polinom_Regression)
- Coefficient_Correlation=sqrt(Coefficient_Determination_Polinom_Regression)
- print('Коэффициент корреляции R=',Coefficient_Correlation)
- per1=sum_h*sum_R/len(s)
- per2=((sum_R)**2)/len(s)
- Coefficient_Regression=(sum_hR-per1)/(sum_R2-per2)
- print('Коэффициент регрессии R=',Coefficient_Regression)
- print('Суммарная ошибка =',sum_T)
- J=np.linspace(0,20,21)
- y=a*J+b
- Y=C*(J**2)+D*J+K
- Plot1=plt.subplot(121)
- Plot2=plt.subplot(122)
- Plot1.set_title('Линейная регрессионная модель')
- Plot2.set_title('Полиномиальная регрессионная модель степени 2')
- Plot1.set_xlabel('h')
- Plot1.set_ylabel('R')
- Plot2.set_xlabel('h')
- Plot2.set_ylabel('R')
- Plot1.plot(J, y)
- Plot2.plot(J, Y)
- for i in range(len(s)):
- Plot1.plot(s[i], Q[i], marker='o')
- Plot2.plot(s[i], Q[i], marker='o')
- plt.show()
- '''Задание 5.2 найти приближенное значение функции при заданном аргументе с помощью функции линейной интерполяции'''
- X=[0.68,0.73,0.80,0.88,0.93,0.99]
- Y= [0.80866, 0.89492, 1.02964, 1.20966, 1.34087, 1.52368]
- XN=[0.896,0.774,0.955]
- F=interp1d(X,Y,'linear')
- FN=F(XN)
- AA=np.zeros((2,3))
- uu=(XN,FN)
- for i in range(2):
- for j in range(3):
- AA[i][j]=uu[i][j]
- AA=AA.transpose()
- print(AA)
- M=F(X)
- plt.plot(X,M)
- plt.show()
- '''реализация без встроенных функций'''
- '''
- j=0
- sp=[]
- xx1=np.linspace(0,2,10)
- J=[]
- for i in range(len(X)-1):
- k=X[i+1]-X[i]
- k1=Y[i+1]-Y[i]
- while xx1[j]<X[i+1]:
- J.append(Y[i]+(k1/k)*(xx1[j]-X[i]))
- j=j+1
- J.append(Y[-1]+(k1/k)*(xx1[-1]-X[-1]))
- print(j)
- plt.plot(xx1,J)
- plt.show()'''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement