Advertisement
lenyaplay

Untitled

Nov 23rd, 2021
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1.  
  2. import numpy as np
  3. from math import sqrt
  4. import matplotlib.pyplot as plt
  5. from scipy.interpolate import interp1d
  6.  
  7. s=[1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3]
  8. Q=[5.21, 4.196, 3.759, 3.672, 4.592, 4.621, 5.758, 7.7173, 9.269]
  9.  
  10.  
  11. s2=[]
  12. sR=[]
  13. R2=[]
  14. for i in range(len(s)):
  15. s2.append([0])
  16. s2[i]= s[i] ** 2
  17. for i in range(len(s)):
  18. sR.append([0])
  19. sR[i]= s[i] * Q[i]
  20. sum_h2=np.sum(s2)
  21. sum_h=np.sum(s)
  22. sum_R=np.sum(Q)
  23. sum_hR=np.sum(sR)
  24. o=np.array([[sum_h2, sum_h], [sum_h, len(s)]])
  25. p=np.linalg.det(o) #Дельта
  26. z=np.array([[sum_hR, sum_h], [sum_R, len(s)]])
  27. j=np.linalg.det(z) #Дельта a
  28. a=j/p
  29. w=np.array([[sum_h2, sum_hR],[sum_h, sum_R]])
  30. e=np.linalg.det(w) #Дельта b
  31. b=e/p
  32. print('Для уравнения линейной регрессионной модели y=ax+b')
  33. print(f'Коэффициент линейной регрессионной модели b={b}')
  34. print(f'Коэффициент линейной регрессионной модели a={a}')
  35. h_medium=sum_h/len(s)
  36. R_medium=sum_R/len(s)
  37. hR_medium=sum_hR/len(s)
  38. Dh= (sum_h2 / len(s)) - h_medium ** 2 #Дисперсия Dh
  39. for i in range(len(Q)):
  40. R2.append([0])
  41. R2[i]= Q[i] ** 2
  42. sum_R2=np.sum(R2)
  43. DR= (sum_R2 / len(s)) - R_medium ** 2 #Дисперсия DR
  44. sigma_h=sqrt(Dh) #Среднеквадратическое отклонение сигма h
  45. sigma_R=sqrt(DR) #Среднеквадратическое отклонение сигма R
  46. r=(hR_medium-h_medium*R_medium)/(sigma_h*sigma_R) #Коэффицент корреляции
  47. Coefficient_Determination_Linear_Regression=r**2
  48. print('Коэффициент детерминации R2=',Coefficient_Determination_Linear_Regression)
  49. #Расчёты для линейной регрессионной модели!!! КОНЕЦ
  50. #Расчёты для полиномиальной регрессионной модели степени 2!!! НАЧАЛО
  51. print('Для уравнения полиномиальной регрессионной модели степени 2 R=Ch^2+Dh+K')
  52. h4=[]
  53. h3=[]
  54. h2R=[]
  55. for i in range(len(s)):
  56. h4.append([0])
  57. h4[i]= s[i] ** 4
  58. for i in range(len(s)):
  59. h3.append([0])
  60. h3[i]= s[i] ** 3
  61. for i in range(len(s)):
  62. h2R.append([0])
  63. h2R[i]= (s[i] ** 2) * Q[i]
  64. sum_h4=np.sum(h4)
  65. sum_h3=np.sum(h3)
  66. sum_h2R=np.sum(h2R)
  67. terra1=np.array([[sum_h4, sum_h3, sum_h2], [sum_h3, sum_h2, sum_h], [sum_h2, sum_h, len(s)]])
  68. delta=np.linalg.det(terra1) #Дельта
  69. terra2=np.array([[sum_h2R, sum_h3, sum_h2], [sum_hR, sum_h2, sum_h], [sum_R, sum_h, len(s)]])
  70. delta1=np.linalg.det(terra2) #Дельта C
  71. terra3=np.array([[sum_h4, sum_h2R, sum_h2], [sum_h3, sum_hR, sum_h], [sum_h2, sum_R, len(s)]])
  72. delta2=np.linalg.det(terra3) #Дельта D
  73. terra4=np.array([[sum_h4, sum_h3, sum_h2R],[sum_h3, sum_h2, sum_hR],[sum_h2, sum_h, sum_R]])
  74. delta3=np.linalg.det(terra4) #Дельта K
  75. C=delta1/delta
  76. D=delta2/delta
  77. K=delta3/delta
  78. print('Коэффициент полиномиальной регрессионной модели степени 2 C=',C)
  79. print('Коэффициент полиномиальной регрессионной модели степени 2 D=',D)
  80. print('Коэффициент полиномиальной регрессионной модели степени 2 K=',K)
  81. #РАСЧЁТЫ КОЭФФИЦИЕНТА ДЕТЕРМИНАЦИИ ДЛЯ ПОЛИНОМА!!! НАЧАЛО
  82. T=[]
  83. GG=[]
  84. UU=[]
  85. for i in range(len(s)):
  86. GG.append([0])
  87. GG[i]= C * (s[i] ** 2) + D * s[i] + K
  88. for i in range(len(s)):
  89. T.append([0])
  90. T[i]= (Q[i] - (C * (s[i] ** 2) + D * s[i] + K)) ** 2
  91. sum_T=np.sum(T)
  92. for i in range(len(s)):
  93. UU.append([0])
  94. UU[i]= (Q[i] - R_medium) ** 2
  95. sum_UU=np.sum(UU)
  96. Coefficient_Determination_Polinom_Regression=1-(sum_T/sum_UU)
  97. print('Коэффициент детерминации R2=',Coefficient_Determination_Polinom_Regression)
  98.  
  99. Coefficient_Correlation=sqrt(Coefficient_Determination_Polinom_Regression)
  100. print('Коэффициент корреляции R=',Coefficient_Correlation)
  101. per1=sum_h*sum_R/len(s)
  102. per2=((sum_R)**2)/len(s)
  103. Coefficient_Regression=(sum_hR-per1)/(sum_R2-per2)
  104. print('Коэффициент регрессии R=',Coefficient_Regression)
  105. print('Суммарная ошибка =',sum_T)
  106.  
  107.  
  108. J=np.linspace(0,20,21)
  109. y=a*J+b
  110. Y=C*(J**2)+D*J+K
  111. Plot1=plt.subplot(121)
  112. Plot2=plt.subplot(122)
  113. Plot1.set_title('Линейная регрессионная модель')
  114. Plot2.set_title('Полиномиальная регрессионная модель степени 2')
  115. Plot1.set_xlabel('h')
  116. Plot1.set_ylabel('R')
  117. Plot2.set_xlabel('h')
  118. Plot2.set_ylabel('R')
  119. Plot1.plot(J, y)
  120. Plot2.plot(J, Y)
  121. for i in range(len(s)):
  122. Plot1.plot(s[i], Q[i], marker='o')
  123. Plot2.plot(s[i], Q[i], marker='o')
  124. plt.show()
  125.  
  126. '''Задание 5.2 найти приближенное значение функции при заданном аргументе с помощью функции линейной интерполяции'''
  127. X=[0.68,0.73,0.80,0.88,0.93,0.99]
  128. Y= [0.80866, 0.89492, 1.02964, 1.20966, 1.34087, 1.52368]
  129. XN=[0.896,0.774,0.955]
  130. F=interp1d(X,Y,'linear')
  131. FN=F(XN)
  132. AA=np.zeros((2,3))
  133. uu=(XN,FN)
  134. for i in range(2):
  135. for j in range(3):
  136. AA[i][j]=uu[i][j]
  137. AA=AA.transpose()
  138.  
  139. print(AA)
  140. M=F(X)
  141. plt.plot(X,M)
  142. plt.show()
  143. '''реализация без встроенных функций'''
  144. '''
  145. j=0
  146. sp=[]
  147. xx1=np.linspace(0,2,10)
  148. J=[]
  149. for i in range(len(X)-1):
  150. k=X[i+1]-X[i]
  151. k1=Y[i+1]-Y[i]
  152. while xx1[j]<X[i+1]:
  153. J.append(Y[i]+(k1/k)*(xx1[j]-X[i]))
  154. j=j+1
  155. J.append(Y[-1]+(k1/k)*(xx1[-1]-X[-1]))
  156. print(j)
  157. plt.plot(xx1,J)
  158. plt.show()'''
  159.  
  160.  
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement