线性回归 NUMPY

 
 
使用python中的numpy实现线性回归-代码存档2018.4.27# coding:utf-8
import numpy as np
from matplotlib import pyplot as plt
def divDebugWindow():
    print('====================================神秘的分割线================================\n')
def deBugout(nameofVAR,VAR):
    print (nameofVAR,VAR,'Shape',nameofVAR,'is',np.shape(VAR))
def deBugLine(A):
    print('====================',A,'=======================')
def norm(inputtoNum):
    return np.sqrt(np.sum(np.square(inputtoNum)))
#=====================================#=====================================#=====================================#==============================
def gradient_method_quadratic(A,b,x0,epsilon):
    recorder=np.array([]);
    recorder2 = np.array([]);
    x = x0;
    iter = 0;
    grad = 2 * (np.dot(A , x) + b);
    deBugLine('beginiteration:')
    while (norm(grad) > epsilon):
        iter = iter + 1;


        # grad = 2 * (np.dot(A , x) + b);
        t = norm(grad) ** 2 / (2 * np.dot(np.dot(grad.T,A),grad));
        deBugout('t',t)
        x = x - t * grad;
        grad = 2 * (np.dot(A , x) + b);
        fun_val = np.dot(np.dot(x.T,A),x)+2*np.dot(b.T, x);
        # deBugLine(iter)
        recorder=np.append(recorder,fun_val)
        recorder2 = np.append(recorder2, norm(grad))
        print ('iter_number =', iter, ' norm_grad = ', norm(grad), ' fun_val = ', fun_val)
    deBugLine('Finished')
    deBugout('recorder',recorder)
    deBugout('recorder2', recorder2)
    return recorder,recorder2


        # fprintf('iter_number = %3d norm_grad = %2.6f fun_val = %2.6f \n', iter, norm(grad), fun_val);
#=====================================#=====================================#========
divDebugWindow()
dataFrame=datafile()
A = np.array([[1,0],[0,2]])
b = np.array([[0],[0]])
x0 = np.array([[2],[1]])
epsilon = 1*10**(-5)


(recorder,recorder2)= gradient_method_quadratic(A,b,x0,epsilon)
plt.plot(range(1,np.size(recorder)+1),recorder,label='funval')
# plt.legend('funval')


plt.plot(range(1,np.size(recorder)+1),recorder2,label='Norm(grad)')
plt.legend(loc='upper right')




plt.xlabel('ITERATION')
plt.ylabel('VALUE')
plt.show()
# BB = np.array([1,2,3])
# print np.sqrt(np.sum(np.square(BB)))
print(epsilon)






猜你喜欢

转载自blog.csdn.net/weixin_39257042/article/details/80109630