--- tags: AI disqus: HackMD --- 人工智慧 #3 === For the data set in the table below, please implement a multivariate regression program to predict the weights of new samples. Please implement the gradient descent search of the perceptron learning rule by yourself, not calling any library functions. Upload a detail report on how you program the predictor and the running environment of the final program. (Note: Your program should not use any regression library functions from any language environment.) ![](https://i.imgur.com/60B4IQ0.png) --- ```python= import numpy as np import pandas as pd def cost_function(X, Y, theta): '''計算成本函數''' m = len(Y) error = X.dot(theta) - Y cost = np.sum(error ** 2)/(2*m) return cost, error def batch_gradient_descent(X, Y, theta, alpha, iters): '''梯度下降''' cost_history = np.zeros(iters) m = len(Y) for i in range(iters): cost, error = cost_function(X, Y, theta) gradient = X.T.dot(error) / m theta = theta - alpha * gradient cost_history[i] = cost return theta, cost_history # In[0]: 先將資料載入pandas.DataFrame data = list([[5.0, 45, 77], [5.11, 26, 47], [5.6, 30, 55], [5.9, 34, 59], [4.8, 40, 72], [5.8, 36, 60], [5.3, 19, 40], [5.8, 28, 60], [5.5, 23, 45], [5.6, 32, 58]]) df = pd.DataFrame(columns=('Height', 'Age', 'Weight'), data=data) # In[1]: 將資料切分為X、Y,其中X作為Feature、Y作為Label X = df[['Height', 'Age']] Y = df['Weight'] # In[2]: 資料標準化,採用Z分數標準化(Z-Score Standardization) # 取平均值 X_mean = X.mean() # 取標準差 X_std = X.std() # (X-平均值)/標準差 X = (X - X_mean) / X_std # 在向量插入偏差列,校準時會乘以偏差列 X = np.c_[np.ones(X.shape[0]), X] # In[3]: 設定初始係數 theta = np.zeros(X.shape[1]) alpha = 0.005 iterations = 2000 # In[4]: 梯度下降 theta, cost_history = batch_gradient_descent(X, Y, theta, alpha, iterations) # In[5]: 預測結果 test = (5.5, 38) Pred = (theta[2] * (test[0] - X_mean[0]) / X_std[0]) + (theta[1] * (test[1] - X_mean[1]) / X_std[1]) + theta[0] print('Predict:', Pred) ```