#python #anaconda #jupyter-notebook #visual-code #html #css #javascript #http

제목최소제곱법 활용한 그래프그리기 질문2020-06-13 18:56
작성자

최소제곱법을 활용해서 데이터에대한 추세선을 그리고 싶습니다. 

mb-file.php?path=2020%2F06%2F13%2FF1299_r3r3r3.png

함수의 데이터는 이렇게 나오는데, 그렇다면 이와 유사한 그래프, 즉 추세선의 기울기는 반드시 양수여야하잖아요

그런데 제가 짠 코드대로 하니 그래프의 기울기가 음수가 나왔습니다... 아무리 봐도 왜 이러한 결과가 나오는지 모르겠네요 ㅠㅠ

KR20190028794A - GPU 기반 TFT-LCD Mura 결함 탐지 방법 - Google Patents


위 식을 그대로 파이썬에 구현하였습니다. 제 코드에 어떤 문제가 있는지 봐주시면 정말 감사하겠습니다 ㅠㅠ
 

------------------------------

import matplotlib.pyplot as plt

import numpy as np

import numpy.linalg as lin

%matplotlib inline


accumulated_answer_count_list = [6473, 2762, 3616, 14879, 2071, 33209, 36111, 7249, 4421, 1666, 10984, 2095, 18362, 5274, 234, 4753, 210, 21246, 1574, 251, 5385, 447, 4144, 3784, 987, 1108, 493, 6996, 742, 3878, 270, 3151, 1221, 1051, 238, 1738, 5618, 1668, 3969, 536, 1801, 1216, 2216, 23529, 1112, 597, 1701, 2134, 787, 5714, 932, 512] #x값

like_count_list = [1774, 1163, 766, 2741, 477, 4997, 6793, 1088, 1242, 701, 4469, 1044, 2763, 1600, 103, 1053, 96, 5990, 196, 75, 1742, 435, 619, 615, 184, 243, 120, 1616, 244, 1127, 19, 944, 514, 364, 130, 551, 1125, 351, 574, 377, 437, 182, 653, 1896, 89, 76, 206, 200, 452, 844, 308, 190] #y값


# 최소제곱법 구현

array_1=np.arange(2*len(accumulated_answer_count_list))

array_a=array_1.reshape(len(accumulated_answer_count_list),2) 

print("------------")

for i in range(len(accumulated_answer_count_list)):

    array_a[i][0]=1

    array_a[i][1]=accumulated_answer_count_list[i]

print("array_a\n",array_a) # 위 식의 행렬 X의 역할

print("------------")


array_2=np.arange(len(like_count_list))

array_b=array_2.reshape(len(like_count_list),1)

for j in range(len(like_count_list)):

    array_b[j][0]=like_count_list[j]

print("--------------------")

print("array_b\n",array_b) #위 식의 행렬 Z의 역할



array_a_T = array_a.transpose()

print(array_a_T)


times1 = np.dot(array_a_T,array_a)

Inverse_times1=lin.inv(times1)


times2 = np.dot(array_a_T,array_b)


results = np.dot(Inverse_times1,times2)

print("results",results)  # results의 2번째값이 양수가 나와야 일차함수가 증가할텐데, 음수가나와요... 뭐가 문제일까요?

#최소제곱법 구현 끝


x=np.array(accumulated_answer_count_list)

y=np.array(like_count_list)


plt.figure()

plt.scatter(x,y)

#y=results[0][0] + results[1][0]*x

#plt.plot(x,y)

plt.show()


---------------------------


그대로 복사하셔서 어떤것이 문제인지 판별해주세요 ㅠㅠ


mb-file.php?path=2020%2F06%2F13%2FF1300_434323.png
 

댓글