这是我的代码,我无法实现多元线性回归。我正在尝试使用梯度下降来实现它,并且在theta = theta-(learn / len(xtrain))* np.sum(xtrain *(xtrain @ theta.T-ytrain),axis = 0)中遇到错误。有人可以帮忙吗?我是python的新手。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
dataset = pd.read_csv('50_Startups.csv')
x_points = dataset.iloc[:,0:3]
y_points = dataset.iloc[:,4].values
ones = np.ones([x_points.shape[0],1])
x_points = np.concatenate((ones,x_points),axis=1)
x_points = (x_points - x_points.mean())/x_points.std()
y_points = (y_points - y_points.mean())/y_points.std()
from sklearn.cross_validation import train_test_split
xtrain,xtest,ytrain,ytest = train_test_split(x_points,y_points,test_size = 1/3, random_state = 0)
theta = np.zeros(4)
learn = 0.001 # .001, .01, .1, 1
iters = 100
#computecost
def computeCost(xtrain,y,theta):
tobesummed = np.power(((xtrain @ theta.T)-y),2)
return np.sum(tobesummed)/(2 * len(xtrain))
#gradient descent
def gradientDescent(xtrain,ytrain,theta,iters,learn):
cost = np.zeros(iters)
for i in range(iters):
z =(xtrain @ theta.T)
theta = theta - (learn/len(xtrain)) * np.sum(xtrain * (xtrain @ theta.T - ytrain), axis=0)
cost[i] = computeCost(xtrain, ytrain, theta)
return theta,cost
#running the gd and cost function
g,cost = gradientDescent(xtrain,ytrain,theta,iters,learn)
print(g)
finalCost = computeCost(xtrain,ytrain,g)
print(finalCost)