Advertisement
konchin_shih

07/27 16:00

Jul 27th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | None | 0 0
  1. import numpy
  2. import cv2
  3. import pandas
  4. import seaborn
  5. import os
  6. from matplotlib import pyplot
  7.  
  8. # constants
  9. path = "./data/"
  10. maxShrinkRate = 10
  11.  
  12.  
  13. pictureList = []
  14. for filename in os.listdir(path):
  15.     if filename.endswith(".jpg"):
  16.         pictureList.append(cv2.imread(path + filename, cv2.IMREAD_GRAYSCALE))
  17.  
  18.  
  19. # 資料處理
  20.  
  21. def toGraph(originalGraphList, xshrinkRate, yshrinkRate):
  22.  
  23.     xSize = len(originalGraphList[0])
  24.     ySize = len(originalGraphList[0][0])
  25.  
  26.     fixed_xSize = xSize // xshrinkRate
  27.     fixed_ySize = ySize // yshrinkRate
  28.     fixed_graph = numpy.zeros((fixed_xSize, fixed_ySize))
  29.  
  30.     for xi in range(fixed_xSize):
  31.         for yi in range(fixed_ySize):
  32.             for xj in range(xshrinkRate):
  33.                 for yj in range(yshrinkRate):
  34.                     for graph in originalGraphList:
  35.                         fixed_graph[xi][yi] += graph[xi * xshrinkRate + xj][yi * yshrinkRate + yj]
  36.             fixed_graph[xi][yi] //= xshrinkRate * yshrinkRate * len(originalGraphList)
  37.  
  38.     fixed_result = numpy.array(pandas.value_counts(fixed_graph.flatten()))
  39.     returnVar = numpy.arange(1, len(fixed_result) + 1)
  40.  
  41.     pyplot.figure(figsize=(15, 5))
  42.     pyplot.subplot(1, 2, 1)
  43.     pyplot.plot(returnVar, fixed_result)
  44.     pyplot.title(f"{xshrinkRate} * {yshrinkRate}")
  45.     pyplot.ylabel('appear time')
  46.     pyplot.xlabel('rank')
  47.  
  48.     pyplot.subplot(1, 2, 2)
  49.     pyplot.plot(returnVar, fixed_result)
  50.     pyplot.xscale('log')
  51.     pyplot.yscale('log')
  52.     pyplot.title(f"{xshrinkRate} * {yshrinkRate}")
  53.     pyplot.ylabel('appear time(log_10)')
  54.     pyplot.xlabel('rank(log_10)')
  55.  
  56.     return [returnVar, fixed_result]
  57.  
  58.  
  59. trans_pictureList = []
  60.  
  61. for i in pictureList:
  62.     trans_pictureList.append(i.tolist())
  63.  
  64. fixed_resultList = []
  65. for i in range(1, maxShrinkRate + 1):
  66.     fixed_resultList.append(toGraph(trans_pictureList, i, i))  # here
  67. #   fixed_resultList.append(toGraph(trans_pictureList, 1, i))
  68. #   fixed_resultList.append(toGraph(trans_pictureList, i, 1))
  69.  
  70. # 畫總圖
  71.  
  72. pyplot.figure(figsize=(15, 5))
  73. pyplot.subplot(1, 2, 1)
  74. for i in fixed_resultList:
  75.     pyplot.plot(i[0], i[1])
  76. pyplot.title('total')
  77. pyplot.ylabel('appear time')
  78. pyplot.xlabel('rank')
  79.  
  80.  
  81. pyplot.subplot(1, 2, 2)
  82. for i in fixed_resultList:
  83.     pyplot.plot(i[0], i[1])
  84. pyplot.xscale('log')
  85. pyplot.yscale('log')
  86. pyplot.title('total')
  87. pyplot.ylabel('appear time(log_10)')
  88. pyplot.xlabel('rank(log_10)')
  89.  
  90.  
  91. # 分析圖
  92.  
  93. logarithmic_resultList = []  # [x, y]
  94. for i in fixed_resultList:
  95.     logarithmic_resultList.append([numpy.log10(i[0]), numpy.log10(i[1])])
  96.  
  97.  
  98. pyplot.figure(figsize=(15, 5))
  99. pyplot.subplot(1, 2, 1)
  100.  
  101. for i in range(maxShrinkRate):
  102.     u = seaborn.regplot(logarithmic_resultList[i][0],
  103.                         logarithmic_resultList[i][1],
  104.                         label=f"{i + 1} * {i + 1}")
  105. u.set_title('analyze')
  106. u.set_ylabel('appear time(log_10)')
  107. u.set_xlabel('rank(log_10)')
  108. u.legend(loc='upper right')
  109.  
  110. # 線條
  111. pyplot.subplot(1, 2, 2)
  112.  
  113. for i in range(maxShrinkRate):
  114.     v = seaborn.regplot(logarithmic_resultList[i][0],
  115.                         logarithmic_resultList[i][1],
  116.                         label=f"{i + 1} * {i + 1}",
  117.                         scatter=False)
  118. v.set_title('analyze')
  119. v.set_ylabel('appear time(log_10)')
  120. v.set_xlabel('rank(log_10)')
  121. v.legend(loc='upper right')
  122.  
  123. # 取數值
  124. pyplot.figure(figsize=(0.001, 0.001))
  125.  
  126.  
  127. axList = []
  128.  
  129. for i in range(maxShrinkRate):
  130.     pyplot.subplot(1, maxShrinkRate, i + 1)
  131.     axList.append(seaborn.regplot(logarithmic_resultList[i][0],
  132.                                   logarithmic_resultList[i][1],
  133.                                   scatter=False))
  134.  
  135. slopeList = []
  136.  
  137. for i in range(maxShrinkRate):
  138.     slope, intercept = numpy.polyfit(axList[i].get_lines()[0].get_xdata(),
  139.                                      axList[i].get_lines()[0].get_ydata(),
  140.                                      1)
  141.     print(f"slope {i + 1} = {slope}")
  142.     slopeList.append(slope)
  143.  
  144. print('the average of slopes =', sum(slopeList) / maxShrinkRate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement