Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- import cv2
- import pandas
- import seaborn
- import os
- from matplotlib import pyplot
- # constants
- path = "./data/"
- maxShrinkRate = 10
- pictureList = []
- for filename in os.listdir(path):
- if filename.endswith(".jpg"):
- pictureList.append(cv2.imread(path + filename, cv2.IMREAD_GRAYSCALE))
- # 資料處理
- def toGraph(originalGraphList, xshrinkRate, yshrinkRate):
- xSize = len(originalGraphList[0])
- ySize = len(originalGraphList[0][0])
- fixed_xSize = xSize // xshrinkRate
- fixed_ySize = ySize // yshrinkRate
- fixed_graph = numpy.zeros((fixed_xSize, fixed_ySize))
- for xi in range(fixed_xSize):
- for yi in range(fixed_ySize):
- for xj in range(xshrinkRate):
- for yj in range(yshrinkRate):
- for graph in originalGraphList:
- fixed_graph[xi][yi] += graph[xi * xshrinkRate + xj][yi * yshrinkRate + yj]
- fixed_graph[xi][yi] //= xshrinkRate * yshrinkRate * len(originalGraphList)
- fixed_result = numpy.array(pandas.value_counts(fixed_graph.flatten()))
- returnVar = numpy.arange(1, len(fixed_result) + 1)
- pyplot.figure(figsize=(15, 5))
- pyplot.subplot(1, 2, 1)
- pyplot.plot(returnVar, fixed_result)
- pyplot.title(f"{xshrinkRate} * {yshrinkRate}")
- pyplot.ylabel('appear time')
- pyplot.xlabel('rank')
- pyplot.subplot(1, 2, 2)
- pyplot.plot(returnVar, fixed_result)
- pyplot.xscale('log')
- pyplot.yscale('log')
- pyplot.title(f"{xshrinkRate} * {yshrinkRate}")
- pyplot.ylabel('appear time(log_10)')
- pyplot.xlabel('rank(log_10)')
- return [returnVar, fixed_result]
- trans_pictureList = []
- for i in pictureList:
- trans_pictureList.append(i.tolist())
- fixed_resultList = []
- for i in range(1, maxShrinkRate + 1):
- fixed_resultList.append(toGraph(trans_pictureList, i, i)) # here
- # fixed_resultList.append(toGraph(trans_pictureList, 1, i))
- # fixed_resultList.append(toGraph(trans_pictureList, i, 1))
- # 畫總圖
- pyplot.figure(figsize=(15, 5))
- pyplot.subplot(1, 2, 1)
- for i in fixed_resultList:
- pyplot.plot(i[0], i[1])
- pyplot.title('total')
- pyplot.ylabel('appear time')
- pyplot.xlabel('rank')
- pyplot.subplot(1, 2, 2)
- for i in fixed_resultList:
- pyplot.plot(i[0], i[1])
- pyplot.xscale('log')
- pyplot.yscale('log')
- pyplot.title('total')
- pyplot.ylabel('appear time(log_10)')
- pyplot.xlabel('rank(log_10)')
- # 分析圖
- logarithmic_resultList = [] # [x, y]
- for i in fixed_resultList:
- logarithmic_resultList.append([numpy.log10(i[0]), numpy.log10(i[1])])
- pyplot.figure(figsize=(15, 5))
- pyplot.subplot(1, 2, 1)
- for i in range(maxShrinkRate):
- u = seaborn.regplot(logarithmic_resultList[i][0],
- logarithmic_resultList[i][1],
- label=f"{i + 1} * {i + 1}")
- u.set_title('analyze')
- u.set_ylabel('appear time(log_10)')
- u.set_xlabel('rank(log_10)')
- u.legend(loc='upper right')
- # 線條
- pyplot.subplot(1, 2, 2)
- for i in range(maxShrinkRate):
- v = seaborn.regplot(logarithmic_resultList[i][0],
- logarithmic_resultList[i][1],
- label=f"{i + 1} * {i + 1}",
- scatter=False)
- v.set_title('analyze')
- v.set_ylabel('appear time(log_10)')
- v.set_xlabel('rank(log_10)')
- v.legend(loc='upper right')
- # 取數值
- pyplot.figure(figsize=(0.001, 0.001))
- axList = []
- for i in range(maxShrinkRate):
- pyplot.subplot(1, maxShrinkRate, i + 1)
- axList.append(seaborn.regplot(logarithmic_resultList[i][0],
- logarithmic_resultList[i][1],
- scatter=False))
- slopeList = []
- for i in range(maxShrinkRate):
- slope, intercept = numpy.polyfit(axList[i].get_lines()[0].get_xdata(),
- axList[i].get_lines()[0].get_ydata(),
- 1)
- print(f"slope {i + 1} = {slope}")
- slopeList.append(slope)
- print('the average of slopes =', sum(slopeList) / maxShrinkRate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement