2016-12-24 78 views
0

我剛開始機器學習,花了幾個小時學習線性迴歸。根據我的理解,我在python(下面的代碼)中從頭開始實現它,而沒有正則化。我的邏輯是正確的還是需要改進?使用python驗證線性迴歸實現

import numpy as np 
import matplotlib.pyplot as plt 


# Assigning X and y from the dataset 
data = np.loadtxt('ex1data1.txt', delimiter=',') 
rows=(data.size)/2 
X = np.array((data[:, 0])).reshape(rows, 1) 
y = np.array(data[:, 1]).reshape(rows, 1) 
m = np.size(X) 
X = np.insert(X, 0, values=1, axis=1) 
t = np.ones(shape=[2, 1]) 

def linReg(): 
    h = np.dot(X,t) 
    J = 1/(2*m) * sum((h - y)**2) 
    print('Cost:',J) 
    print("Error:",h-y) 
    for i in range(1,2000): 
     h = np.dot(X,t) 
     t[0] = t[0] - 0.01*(1/m)*(np.dot((X[:,0]),(h-y))) 
     t[1] = t[1] - 0.01*(1/m)*(np.dot((X[:,1]),(h-y))) 
     J = 1/(2*m) * sum((h - y)**2) 
     print(i) 
     print('Cost:', J) 

    plt.scatter(X[:,1],y,color= 'blue') 
    plt.plot(X[:,1],h) 
    return t 

def predict(newval): 
    W = linReg() 
    predValue = np.dot(newval,W[1]) + W[0] 
    print("Predicted Value:-",predValue) 
    plt.plot(newval, predValue) 
    plt.scatter(newval, predValue, color='red') 
    plt.xlim(0, 40) 
    plt.ylim(0, 40) 
    plt.show() 

print("Enter the number to be predicted:-") 
nv = input() 
nv = float(nv) 
predict(nv) 

回答

0

要檢查你的模型,一個簡單的事將是將數據分成訓練組和測試組。該訓練集用於擬合模型,作爲參數傳遞給linReg函數,並且測試集的特徵用於預測(帶有您所謂的predict函數)。

然後,您將需要第三個功能得分模型,通過比較由數據給出的實際值的預測。如果你得到一個不錯的成績,那麼你的實現可能是正確的,如果沒有,一點點調試將是必要的;-)

要開始,我會建議通過如下定義功能重新安排你的代碼:

def train_test_split(X, y): 
    """ 
    Return a splitted version (X_train, y_train) and (X_test, y_test) of the dataset. 
    """ 

def linReg_train(X_train, y_train): 
    """ 
    Fit the model and return the weights. 
    """ 

def linReg_pred(X_test) 
    """ 
    Use the fitted model to predict values for all the points in X_test. 
    """ 

def linReg_score(y_predicted, y_test) 
    """ 
    Compare predicted and true outputs to assess model quality. 
    """ 

有些資源可能對您有用:

祝你好運!

+0

謝謝你的幫助,我會盡力的。 – Raj