• Chapter4:Traing Model


    # The Normal Equation

    1. import numpy as np
    2. X = 2 * np.random.rand(100,1) #np.random.rand,通过本函数可以返回一个或一组服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1),不包括1。
    3. y = 4 + 3 * X + np.random.randn(100,1)
    #np.random.rand通过本函数可以返回一个或一组服从标准正态分布的随机样本值。
    1)当函数括号内没有参数时,则返回一个浮点数; 
    2)当函数括号内有一个参数时,则返回秩为1的数组,不能表示向量和矩阵; 
    3)当函数括号内有两个及以上参数时,则返回对应维度的数组,能表示向量或矩阵; 
    4)np.random.standard_normal()函数与np.random.randn()类似,但是np.random.standard_normal()
    的输入参数为元组(tuple). 
    5)np.random.randn()的输入通常为整数,但是如果为浮点数,则会自动直接截断转换为整数。
    
    
    

    #Compute normalization

    1. X_b = np.c_[np.ones((100,1)),X] # add x0 to each instance
    2. #np.r_是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等。
    3. #np.c_是按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等。
    4. theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
    5. #np.linalg.inv():矩阵求逆
    6. print('theta_best',theta_best)
    7. '''
    8. theta_best [[4.10903353]
    9. [2.9317255 ]]
    10. '''
    11. #make predictions using theta hat
    12. X_new = np.array([[0],[2]])
    13. print(X_new)
    14. '''
    15. [[0]
    16. [2]]
    17. '''
    18. X_new_b = np.c_[np.ones((2,1)),X_new]
    19. print(X_new_b)
    20. '''
    21. [[1. 0.]
    22. [1. 2.]]
    23. '''
    24. y_predict = X_new_b.dot(theta_best)
    25. print('y_predict',y_predict)
    26. '''
    27. [[3.91742296]
    28. [9.94607488]]
    29. '''
    plt the model predictions
    1. plt.plot(X_new,y_predict,'r-')
    2. plt.plot(X,y,'b.')
    3. plt.axis([0,2,0,15])
    4. plt.show()
    #using Scikit-Learn
    1. from sklearn.linear_model import LinearRegression
    2. lin_reg = LinearRegression()
    3. lin_reg.fit(X,y)
    4. print(lin_reg.intercept_,lin_reg.coef_)
    5. #[3.83383173] [[3.16072489]]
    6. print(lin_reg.predict(X_new))
    7. #[[3.95597016]
    8. #[9.85979796]]
    9. theta_best_svd,residuals,rank,s = np.linalg.lstsq(X_b,y, rcond=1e-6)
    10. #lstsq:least squares 是 LeaST SQuare (最小二乘)的意思。我们常用最小二乘法来求解超定线性方程组。https://www.zhihu.com/question/40540185
    11. '''
    12. c=rcond(A)
    13. 返回矩阵A的1-范数可逆的条件数。对于好条件矩阵A,rcond(A)是接近1的数。对于差条件矩阵A,rcond(A)是接近0的数。和cond相比,rcond(A)在对估计矩阵条件数上更有效率,但更不可靠。
    14. '''
    15. print(theta_best_svd)
    16. #[[4.07650022]
    17. #[3.03760046]]
    18. #calculate Pesudoinverse of X(Moore-Penrose inverse)
    19. print('Pesudoinverse',np.linalg.pinv(X_b).dot(y))
    20. #Pesudoinverse [[4.20901209]
    21. # [2.81408334]]
    伪逆矩阵更加常用的定义(基于SVD奇异值分解):
    
    SVD公式:
    
    伪逆矩阵公式:
    
    这个公式要注意的是中间的的求法。因为是一个对角线矩阵,但又不一定是方阵,所以计算它的伪逆矩阵的步骤是特殊又简单的:
    
    将对角线上的元素取倒数
    
    再将整个矩阵转置一次
    #Gradient Descent
    #Batch Gradient Descent
    1. eta = 0.1 #learning rate
    2. n_interations = 1000
    3. m = 100
    4. theta = np.random.randn(2,1) #random initialization
    5. for iteration in range(n_interations):
    6. gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y) #euqation 4-6
    7. theta = theta - eta * gradients
    8. print('theta',theta)
    9. #theta [[3.61861605]
    10. #[3.35291414]]
    #Stochastic Gradient Desent
    #using SGD
    1. from sklearn.linear_model import SGDRegressor
    2. sgd_reg = SGDRegressor(max_iter=1000,tol=1e-3,penalty=None,eta0=0.1)
    3. sgd_reg.fit(X,y.ravel())
    4. print(sgd_reg.intercept_,sgd_reg.coef_)
    5. #[4.03587962] [3.04749379]
    6. #Polynomial Regress
    7. m = 100
    8. X = 6 * np.random.randn(m,1)
    9. y = 0.5 * X**2 + X +2 + np.random.randn(m,1)
    10. #use Sci-kit
    11. from sklearn.preprocessing import PolynomialFeatures
    12. poly_features = PolynomialFeatures(degree=2, include_bias=False)
    13. X_poly = poly_features.fit_transform(X)
    14. print('X[0]',X[0])
    15. #X[0] [-8.77213649]
    16. print(X_poly)
    17. #fit a LinearRegression Model to this extended training data
    18. lin_reg = LinearRegression()
    19. lin_reg.fit(X_poly,y)
    20. print(lin_reg.intercept_,lin_reg.coef_)
    21. #[2.22340435] [[1.03677436 0.49366961]]
    22. #learning curves: plots of the model's performance on the training set and the validation set as a function of the training set size(or training iteraion)
    23. from sklearn.metrics import mean_squared_error
    24. from sklearn.model_selection import train_test_split
    25. def plot_lerarning_curves(model,X,y):
    26. X_train, X_val,y_train,y_val = train_test_split(X,y,test_size = 0.2)
    27. train_errors,val_errors = [],[]
    28. for m in range(1,len(X_train)):
    29. model.fit(X_train[:m],y_train[:m])
    30. y_train_predict = model.predict(X_train[:m])
    31. y_val_prdict = model.predict(X_val)
    32. train_errors.append(mean_squared_error(y_train[:m],y_train_predict))
    33. val_errors.append(mean_squared_error(y_val,y_val_prdict))
    34. plt.plot(np.sqrt(train_errors),'r-+',linewidth=2,label='train')
    35. plt.plot(np.sqrt(val_errors), 'b-', linewidth=2, label='prediction')
    36. plt.show()
    37. lin_reg = LinearRegression()
    38. plot_lerarning_curves(lin_reg,X,y)
    39. #leraning curves of 10th degree
    40. from sklearn.pipeline import Pipeline
    41. polynomial_regression = Pipeline([
    42. ('poly_features',PolynomialFeatures(degree=10,include_bias=False)),
    43. ('lin_reg',LinearRegression()),
    44. ])
    45. #plot_lerarning_curves(polynomial_regression,X,y)

  • 相关阅读:
    什么是网络安全?如何让小白简单的学习网络安全
    osg vs2017编译
    树形表,自关联表查询技巧
    SQLAlchemy 封装的工具类,数据库pgsql(数据库连接池)
    【BOOST C++ 14 消息编程】(2) 异步数据交换
    SveletJs学习——逻辑处理
    拿捏红黑树(C++)
    VARMA模型的原理与实现
    c++if语句(包含else)
    树的相关知识的注意事项
  • 原文地址:https://blog.csdn.net/anelance/article/details/126482510