线性回归学习笔记

分类:分类(Classification): Y变量为类别型(categorical variable)。如:颜色类别,电脑品牌,有无信誉

回归:回归(regression) Y变量为连续数值型(continuous numerical variable)。如:房价,人数,降雨量

一、简单线性回归模型

1.1简单线性回归模型:

y = β0+β1x +ε  (β0、β1为参数,ε为偏差)>>>>左右求期望值,其中ε偏差为随机变量,均值为0

>>>>得到简单线性回归方程:E(y) = β0+β1x

 

1.2估计的简单线性回归方程

   ŷ=b0+b1x>>>>>b0和b1是β0和β1的估计值

偏差满足:1.随机变量,均值为0 ;2. ε的方差(variance)对于所有的自变量x是一样的; 3. ε的值是独立的 ;4. ε满足正态分布

 

2.1.如何练处适合简单线性回归模型的最佳回归线?

如何练处适合简单线性回归模型的最佳回归线?

使sum of squares最小

2.2.计算

例子:x:1、3、2、1、3       均值为2

          y:14、24、18、17、27    均值为20

 

得到b1 = 20/4  =5   b0 = 20 - 5*2 = 20 - 10 = 10

 

得到估计的简单线性回归方程  ŷ=b0+b1x=10+5x

2.3. python实现:

 

import numpy as np


def fitslr(x, y):

    n = len(x)
    dinominator = 0;
    numerator = 0

    for i in range(0, n):
        numerator += (x[i] - np.mean(x))* (y[i]  - np.mean(y))
        dinominator += (x[i] - np.mean(x))**2

    print("numrator = %.2f" % numerator)
    print("dimominator = %.2f" % dinominator)
    b1 = numerator / (dinominator)
    b0 = np.mean(y) - b1 * np.mean(x)

    return b0, b1

def predict(x, b0, b1):
    return b0 + x * b1


x = [1, 3, 2, 1, 3]
y = [14, 24, 18, 17, 27]

b0, b1 = fitslr(x, y)

print("intercept =:%.2f slope =: %.2f" %(b0, b1))

x_test = 6
y_test = predict(x_test, b0, b1)
print("y_test:%.2f" % y_test)

 

二、多元线性回归模型(MultipleRegression)

 

1. 多元回归模型
     y=β0+β1*x1+β2*x2+ ... +βp*xp+ε
    其中:β0,β1,β2... βp是参数,ε是误差值

2. 多元回归方程
     E(y)=β0+β1x1+β2x2+ ... +βpxp

3. 估计多元回归方程:
     y_hat=b0+b1*x1+b2*x2+ ... +bp*xp
    一个样本被用来计算β0,β1,β2... βp的点估计b0, b1, b2,..., bp

4. 估计方法

    使sum of squares最小   

 

 

Time = b0+ b1*Miles + b2 * Deliveries

5. Python代码:

from numpy import genfromtxt
import numpy as np
from sklearn import datasets, linear_model

dataPath = r"...\Delivery.csv"
deliveryData = genfromtxt(dataPath, delimiter=',')   # 将以逗号为分隔符的文本文件中导入数据,转化成numpyarray的格式

# print("data\n",deliveryData)

X = deliveryData[:, :-1]        # 前两列
Y = deliveryData[:,2 ]          # 第三列

print("X\n", X)
print("Y\n", Y)

regr = linear_model.LinearRegression()  # 调用sklearn的LinearRegression方法

regr.fit(X, Y)          # 用.fit方法建立模型

print("coefficients: ", regr.coef_)         # b1 b2的估计值
print("intercept: ", regr.intercept_)       # 截距b0的估计值

xPredict = [[102, 6]]

yPredict = regr.predict(xPredict)

print("predicted: ", yPredict)


得到:

coefficients:  [0.0611346  0.92342537]
intercept:  -0.868701466781709
predicted:  [10.90757981]

Time = -0.869 + 0.0611 Miles + 0.923 Deliveries

6. 描述参数含义
     b0: 平均每多运送一英里,运输时间延长0.0611 小时
     b1: 平均每多一次运输,运输时间延长 0.923 小时

     Time = -0.869 +0.0611 *102+ 0.923 * 6
              = 10.9 (小时)

 

7. 误差ε

 误差ε是一个随机变量,均值为0,ε的方差对于所有的自变量来说相等,所有ε的值是独立的,ε满足正态分布,并且通过β0+β1x1+β2x2+ ... +βpxp反映y的期望值。

 

 

 

 

 

 

过程: 1.在虚拟机上安装vmware,安装完成后,默认vm tools是没有安装的,启动后在下方提示没有安装虚拟工具,安装过程省略; 2.安装redhat,这个安装过程就省略,没安过的可以也去G一下; 3.安装vm tools: 1)以ROOT身份进入LINUX 2)按下 CTRL ALT组合键,进入主操作系统,点击VMWARE状态栏安装提示,或者点击 虚拟菜单下的安装虚拟机工具子菜单. 3)确认安装VMWARE TOOLS.   这时我们并没有真正的安装上了VMWARE TOOLS软件包,如果您点击菜单:DEVICES,您就会发现光驱的菜单文字变为:ide1:0-> C:\Program Files\VMware\VMware Workstation\Programs\linux.iso,这表示VMWARE将LINUX的ISO映象文件作为了虚拟机的光盘 4)鼠标点击LINUX界面,进入LINUX. 5)运行如下命令,注意大小写. mount -t iso9660 /dev/cdrom /mnt 加载CDROM设备,这时如果进入 /mnt 目录下,你将会 发现多了一个文件:vmware-linux-tools.tar.gz.这就是WMWARE TOOLS的LINUX软件包,也就是我们刚才使用WINISO 打开LINUX.ISO文件所看到的.   cp /mnt/vmware-linux-tools.tar.gz /tmp   将该软件包拷贝到LINUX的 TMP目录下.   umount /dev/cdrom   卸载CDROM.   cd /tmp   进入TMP目录   tar zxf vmware-linux-tools.tar.gz   解压缩该软件包,默认解压到vmware-tools-distrib 目录下(与文件名同名).   cd vmware-linux-tools   进入解压后的目录 这时要进入文本模式下运行下面的命令   ./install.pl 这时install提示你是否需要备份以前的配置文件,建议选择"y".   等待INSTALL运行完成后,这时键入 STARTX 命令,启动LINUX图形界面,vm中不再提示没有安装虚拟机工具了 在Ubuntu下安装VMware-Tools不像在Windows下安装容易,昨晚整了挺长时间终于安上了.方法在下面. 感谢提供方法的大侠! VMware Tools位置:VMware安装路径\VMware\VMware Workstation\linux.iso 启动后按Esc选择recovery mode登陆.然后点击"虚拟机"→"安装VMware Tools". [root@rd01 ~]# mount /cdrom # 有时可能加载不了,这时就要先将系统关闭,再手动指定 ISO 映像: [root@rd01 ~]# cd /cdrom [root@rd01 ~]# ls -a [root@rd01 ~]# cp VMwareTools-5.5.1-19175.tar.gz /tmp [root@rd01 ~]# cd /tmp [root@rd01 ~]# tar zxpf VMwareTools-5.5.1-19175.tar.gz [root@rd01 ~]# cd vmware-tools-distrib [root@rd01 vmware-tools-distrib]# ./vmware-install.pl Creating a new installer database using the tar3 format. Installing the content of the package. # 安装过程的画面,全部使用默认值,一直按 Enter 就对了 In which directory do you want to install the binary files? [/usr/bin] What is the directory that contains the init directories (rc0.d/ to rc6.d/)? [/etc/rc.d] What is the directory that contains the init scripts? [/etc/rc.d/init.d] In which directory do you want to install the daemon files? [/usr/sbin] In which directory do you want to install the library files? [/usr/lib/vmware-tools] Thepath "/usr/lib/vmware-tools" does not exist currently. This programisgoingto create it, including needed parent directories. Is thiswhatyou want? [yes] In which directory do you want to install the documentation files? [/usr/share/doc/vmware-tools] Thepath "/usr/share/doc/vmware-tools" does not exist currently.Thisprogram isgoing to create it, including needed parent directories.Isthis what you want? [yes] The installation of VMware Tools 5.5.1 build-19175 for Linux completed successfully. You can decide to remove this software from your system at any time by invoking the following command: "/usr/bin/vmware-uninstall-tools.pl". Before running VMware Tools for the first time, you need to configure it by invoking the following command: "/usr/bin/vmware-config-tools.pl". Do you want this program to invoke the command for you now? [yes] Stopping VMware Tools services in the virtual machine: Guest operating system daemon: [ 确定 ] Trying to find a suitable vmhgfs module for your running kernel. The module bld-2.6.9-5.EL-i686-RHEL4 loads perfectly in the running kernel. pcnet32 30153 0 Unloading pcnet32 module Trying to find a suitable vmxnet module for your running kernel. The module bld-2.6.9-5.EL-i686-RHEL4 loads perfectly in the running kernel. Detected X.org version 6.8. 关闭控制台鼠标服务: [ 确定 ] 启动控制台鼠标服务: [ 确定 ] Please choose one of the following display sizes (1 - 13): # 显示分辨率,这里是以 1024x768 为例 # VMware Tools 安装的时候,会自动修改 X server 的配置文件 [1] "640x480" [2]< "800x600" [3] "1024x768" [4] "1152x864" [5] "1280x800" [6] "1152x900" [7] "1280x1024" [8] "1376x1032" [9] "1400x1050" [10] "1680x1050" [11] "1600x1200" [12] "1920x1200" [13] "2364x1773" Please enter a number between 1 and 13: [2] 3 X Window System Version 6.8.2 Release Date: 9 February 2005 X Protocol Version 11, Revision 0, Release 6.8.2 Build Operating System: Linux 2.6.9-34.EL i686 [ELF] Current Operating System: Linux rd01.domain 2.6.9-34.EL #1 Wed Mar 8 00:07:35 CST 2006 i686 Build Date: 04 May 2006 Build Host: x8664-build.centos.org Before reporting problems, check http://wiki.X.Org to make sure that you have the latest version. Module Loader present OSKernel: Linux version 2.6.9-34.EL (buildcentos@build-i386) (gccversion3.4.5 20051201 (Red Hat 3.4.5-2)) #1 Wed Mar 8 00:07:35 CST 2006P Markers: (--) probed, (**) from config file, (==) default setting, ( ) from command line, (!!) notice, (II) informational, (WW) warning, (EE) error, (NI) not implemented, (??) unknown. ( ) Log file: "/tmp/vmware-config0/XF86ConfigLog.3131", Time: Mon Jun 12 20:57:34 2006 ( ) Using config file: "/tmp/vmware-config0/XF86Config.3131" (WW) VMWARE(0): Failed to set up write-combining range (0xf0000000,0x1000000) X is running fine with the new config file. Starting VMware Tools services in the virtual machine: Switching to guest configuration: [ 确定 ] Guest filesystem driver: [ 确定 ] DMA setup: [ 确定 ] Guest operating system daemon: [ 确定 ] The configuration of VMware Tools 5.5.1 build-19175 for Linux for this running kernel completed successfully. You must restart your X session before any mouse or graphics changes take effect. You can now run VMware Tools by invoking the following command: "/usr/bin/vmware-toolbox" during an XFree86 session. To use the vmxnet driver, restart networking using the following commands: /etc/init.d/network stop rmmod pcnet32 rmmod vmxnet depmod -a modprobe vmxnet /etc/init.d/network start Enjoy, --the VMware team [root@rd01 vmware-tools-distrib]# shutdown -r now # 修改完成之后,重新启动计算机,让 VMware Tools 生效. == 重启后,你可能会发现还是要按"Ctrl Alt"释放鼠标(VM6.0没这问题了),我用的是5.5.3,要手动执行才有效果. 通过终端运行VMware-Tools: 点击Ubuntu桌面左上角的Applications→附件→终端,会打开一个类似Windows中记事本的程序,在里面输入以下内容($是自带的,不用专门输入): $ /usr/bin/vmware-toolbox 但如果你嫌麻烦,我们就需要让vmware-toolbox实现开机自动运行.点击 Ubuntu菜单:System→首选项→会话→Startup Programs,Add一个Name叫"vmware-toolbox",Command是"/usr/bin/vmware-toolbox"的启动 程序.这样每次开机后就能自动运行VMware Tools了.重启Ubuntu看看效果吧!(注意:vmware-toolbox并不是在后台隐藏运行的,启动后不要关闭). VMware Tools固然是个好东西,但也有副作用,比如说:虚拟机中的鼠标的滚轮不好使了.我们这样解决这个问题,还是打开终端,输入: $ sudo gedit /etc/X11/xorg.conf 这个命令使系统以root权限打开鼠标配置文件/etc/X11/xorg.conf.把文件中的 Option "Protocol" "ps/2" 改成 Option "Protocol" "IMPS/2" 重启Ubuntu搞定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值