使用python验证线性回归实现

使用python验证线性回归实现,python,machine-learning,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((da

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

要检查您的模型,一件简单的事情就是将您的数据拆分为一个训练集和一个测试集。训练集作为参数传递给
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.
    """
您可能会发现一些有用的资源:

  • :一系列详细介绍如何实现线性回归的视频
  • :scikit learn是Python中最有用的机器学习库之一,具有非常好的书面文档。如果您计划编写自己的ML算法实现,您可能希望通过scikit学习实现运行数据来检查它们,并将输出与您自己的工作进行比较

祝你好运

谢谢你的帮助,我试试看。