比较Octave ML结果比。 R结果

时间:2013-12-08 04:55:56

标签: r statistics machine-learning octave

R代码:

my.data <- mtcars[,c(1,3)] # Which has only two columns mpg, disp

lm(mpg~disp,data=my.data) #R Code for fitting a regression line

R输出:

Call:
  lm(formula = mpg ~ disp, data = my.data)

Coefficients:
  (Intercept)         disp  
     29.59985     -0.04122  

将R数据集写入磁盘文件

write.table(my.data,'~/Downloads/mtcars',sep=",",row.name=F,col.names=F) 

Octave代码:

cd ~/Downloads
data=load('mtcars') # Using R dataset to fit the model
x=data(:,2)
y=data(:,1) 
cd ~/Dropbox/ML/mlclass-ex1-004/mlclass-ex1 %without any errors
xn=featureNormalize(x) # feature Normalizing with mean and std 
x1=[ones(length(x),1),xn]
theta=zeros(size(x1,2),1)
g=gradientDescent(x1,y,theta,alpha=.1,10000)

g的输出是:

g =
20.0906
-5.0277

如果你看一下inter的截距和系数; R输出和八度输出没有近似匹配。

有没有人知道这种差异来自哪里?哪一个是对的?

1 个答案:

答案 0 :(得分:5)

您规范了Octave代码中的功能。在R中做同样的事情会得到类似的结果。

R> mtcars$normalized_disp <- as.numeric(scale(mtcars$disp))
R> lm(mpg ~ normalized_disp, data = mtcars)

Call:
lm(formula = mpg ~ normalized_disp, data = mtcars)

Coefficients:
    (Intercept)  normalized_disp  
         20.091           -5.108