使用python

时间:2016-12-24 10:37:59

标签: python machine-learning

我刚从机器学习开始,花了几个小时学习线性回归。根据我的理解,我在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)

1 个答案:

答案 0 :(得分: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.
    """

您可能会发现一些有用的资源:

祝你好运!