Advertisement
mrAnderson33

Код для работы с excel

Feb 7th, 2022
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | None | 0 0
  1. import numpy as np
  2. import openpyxl
  3. import numpy
  4. import pylab
  5.  
  6.  
  7. class RegressionEquation:
  8.  
  9.     def zeroDegreeCoef(y):
  10.         return sum(y) / len(y)
  11.  
  12.     def oneDegreeCoef(x, y):
  13.         mult = sum(x[i] * y[i] for i in range(len(y)))
  14.         norma = sum(x[i] * x[i] for i in range(len(x)))
  15.         return float(mult) / float(norma)
  16.  
  17.     def interactionCoef(x1, x2, y):
  18.         mult = sum(x1[i] * x2[i] * y[i] for i in range(len(x1)))
  19.         norma = sum(x1[i] * x1[i] * x2[i] * x2[i] for i in range(len(x1)))
  20.         return mult / norma
  21.  
  22.     def twoDegreeCoef(x, y):
  23.         a = sum(x[i] * x[i] for i in range(len(x))) / len(x)
  24.         mult = sum(((x[i] * x[i]) - a) * y[i] for i in range(len(y)))
  25.         norma = sum(((x[i] * x[i]) - a) ** 2 for i in range(len(x)))
  26.         return mult / norma
  27.  
  28.     def drawIsoline(regressionEquation, levels):
  29.         x = numpy.arange(-2, 6.5, 0.05)
  30.         y = numpy.arange(0, 15, 0.05)
  31.         xgrid, ygrid = numpy.meshgrid(x, y)
  32.         zgrid = regressionEquation(x1=0, x2=xgrid, x3=ygrid, x4=0, x5=0)
  33.         cs = pylab.contour(xgrid, ygrid, zgrid, levels)
  34.         pylab.xlabel('x2')
  35.         pylab.ylabel('x3')
  36.         pylab.clabel(cs)
  37.         pylab.show()
  38.  
  39.  
  40. path = 'D:/STUDY/Диплом/расчеты/Книга с расчетами.xlsx'
  41. wb = openpyxl.load_workbook(filename=path)
  42.  
  43. sheet = wb['Эксперимент 2']
  44.  
  45. b0 = sheet['B40'].value
  46. b1 = sheet['B41'].value
  47. b2 = sheet['B42'].value
  48. b3 = sheet['B43'].value
  49. b4 = sheet['C40'].value
  50. b5 = sheet['C41'].value
  51. b12 = sheet['C42'].value
  52. b13 = sheet['C43'].value
  53. b14 = sheet['D40'].value
  54. b15 = sheet['D41'].value
  55. b23 = sheet['D42'].value
  56. b24 = sheet['D43'].value
  57. b25 = sheet['E40'].value
  58. b34 = sheet['E41'].value
  59. b35 = sheet['E42'].value
  60. b45 = sheet['E43'].value
  61.  
  62. path = 'D:/STUDY/Диплом/расчеты/Финальные расчеты.xlsx'
  63. wb = openpyxl.load_workbook(filename=path)
  64.  
  65. sheet = wb['Лист1']
  66.  
  67. f = lambda x1, x2, x3, x4,x5: b0 + b1 * x1 + b2 * x2 + b3 * x3 + b4 * x4 + b5 * x5 + b12 * x1 * x2 + b13 * x1 * x3 + b14 * x1 * x4 + b15 * x1 * x5 + b23 * x2 * x3 + b24 * x2 * x4 + b25 * x2 * x5 + b34 * x3 * x4 + b35 * x3 * x5 + b45 * x5 * x4
  68.  
  69. x0 = [sheet.cell(i, 2).value for i in range(2, 48)]
  70. x1 = [sheet.cell(i, 3).value for i in range(2, 48)]
  71. x2 = [sheet.cell(i, 4).value for i in range(2, 48)]
  72. x3 = [sheet.cell(i, 5).value for i in range(2, 48)]
  73. x4 = [sheet.cell(i, 6).value for i in range(2, 48)]
  74. x5 = [sheet.cell(i, 7).value for i in range(2, 48)]
  75.  
  76. y = [sheet.cell(i, 13).value for i in range(2, 48)]
  77.  
  78. print(y)
  79.  
  80. b0 = RegressionEquation.zeroDegreeCoef(y)
  81.  
  82. b1 = RegressionEquation.oneDegreeCoef(x1, y)
  83. b2 = RegressionEquation.oneDegreeCoef(x2, y)
  84. b3 = RegressionEquation.oneDegreeCoef(x3, y)
  85. b4 = RegressionEquation.oneDegreeCoef(x4, y)
  86. b5 = RegressionEquation.oneDegreeCoef(x5, y)
  87.  
  88. b12 = RegressionEquation.interactionCoef(x1, x2, y)
  89. b13 = RegressionEquation.interactionCoef(x1, x3, y)
  90. b14 = RegressionEquation.interactionCoef(x1, x4, y)
  91. b15 = RegressionEquation.interactionCoef(x1, x5, y)
  92.  
  93. b23 = RegressionEquation.interactionCoef(x2, x3, y)
  94. b24 = RegressionEquation.interactionCoef(x2, x4, y)
  95. b25 = RegressionEquation.interactionCoef(x2, x5, y)
  96.  
  97. b34 = RegressionEquation.interactionCoef(x3, x4, y)
  98. b35 = RegressionEquation.interactionCoef(x3, x5, y)
  99.  
  100. b45 = RegressionEquation.interactionCoef(x4, x5, y)
  101.  
  102. b11 = RegressionEquation.twoDegreeCoef(x1, y)
  103. b22 = RegressionEquation.twoDegreeCoef(x2, y)
  104. b33 = RegressionEquation.twoDegreeCoef(x3, y)
  105. b44 = RegressionEquation.twoDegreeCoef(x4, y)
  106. b55 = RegressionEquation.twoDegreeCoef(x5, y)
  107.  
  108. sheet['D49'] = b0
  109.  
  110. sheet['D50'] = b1
  111. sheet['D51'] = b2
  112. sheet['D52'] = b3
  113. sheet['D53'] = b4
  114. sheet['D54'] = b5
  115.  
  116. sheet['E50'] = b12
  117. sheet['E51'] = b13
  118. sheet['E52'] = b14
  119. sheet['E53'] = b15
  120. sheet['E54'] = b23
  121.  
  122. sheet['F50'] = b24
  123. sheet['F51'] = b25
  124. sheet['F52'] = b34
  125. sheet['F53'] = b35
  126. sheet['F54'] = b45
  127.  
  128. sheet['G50'] = b11
  129. sheet['G51'] = b22
  130. sheet['G52'] = b33
  131. sheet['G53'] = b44
  132. sheet['G54'] = b55
  133.  
  134. x11 = [sheet.cell(i, 8).value for i in range(2, 48)]
  135. x22 = [sheet.cell(i, 9).value for i in range(2, 48)]
  136. x33 = [sheet.cell(i, 10).value for i in range(2, 48)]
  137. x44 = [sheet.cell(i, 11).value for i in range(2, 48)]
  138. x55 = [sheet.cell(i, 12).value for i in range(2, 48)]
  139.  
  140. f = lambda x1, x2, x3, x4,x5: b0 + b1 * x1 + b2 * x2 + b3 * x3 + b4 * x4 + b5 * x5 + b12 * x1 * x2 + b13 * x1 * x3 + b14 * x1 * x4 + b15 * x1 * x5 + b23 * x2 * x3 + b24 * x2 * x4 + b25 * x2 * x5 + b34 * x3 * x4 + b35 * x3 * x5 + b45 * x5 * x4 + b11 * (x1 ** 2 - a) + b22 * (x2 ** 2 - a) + b33 * (x3 ** 2 - a) + b44 * (x4 ** 2 - a) + b55 * (x5 ** 2 - a)
  141.  
  142. for i in range(0, 46):
  143.     sheet.cell(i + 2, 14).value = f(x1=x1[i], x2=x2[i], x3=x3[i], x4=x4[i], x5=x5[i])
  144.  
  145. wb.save(path)
  146.  
  147. levels = [x for x in range(0, 70, 10)]
  148.  
  149. RegressionEquation.drawIsoline(f, levels)
  150.  
  151. A = np.array([[2 * b11, b12, b13, b14, b15], [b12, 2 * b22, b23, b24, b25], [b13, b23, 2 * b33, b34, b35],
  152.               [b14, b24, b34, 2 * b44, b45], [b15, b25, b35, b45, 2 * b55]])
  153. b = np.array([-b1, -b2, -b3, -b4, -b5])
  154. x = np.linalg.solve(A, b)
  155.  
  156. res = f(x[0], x[1], x[2], x[3], x[4], x[5])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement