利用python实现简单的线性回归对房屋面积进行预测

 # -*-coding:utf-8 -*-
'''
Created on 2016年12月15日 @author: lpworkdstudy
'''
import numpy as np
from numpy.core.multiarray import dtype
import matplotlib.pyplot as plt filename = "ex1data1.txt"
alpha = 0.01 f = open(filename,"r")
data = []
y = []
for item in f:
item = item.rstrip().split(",")
data.append(item[:-1])
y.append(item[-1:])
Data = np.array(data,dtype= "float64")
Y = np.array(y,dtype = "float64")
Y = (Y-Y.mean())/(Y.max()-Y.min())
One = np.ones(Data.shape[0],dtype = "float64")
Data = np.insert(Data, 0, values=One, axis=1)
for i in range(1,Data.shape[1]):
Data[:,i] = (Data[:,i]-Data[:,i].mean())/(Data[:,i].max()-Data[:,i].min())
theta = np.zeros((1,Data.shape[1]),dtype= "float64") def CostFunction(Data,Y,theta):
h = np.dot(Data,theta.T)
cost = 1/float((2*Data.shape[0]))*np.sum((h-Y)**2)
return cost
def GradientDescent(Data,Y,theta,alpha):
costList = []
for i in range(10000):
theta = theta- (alpha/Data.shape[0]*np.dot(Data.T,(np.dot(Data,theta.T)-Y))).T
cost = CostFunction(Data, Y, theta)
costList.append(cost) plt.plot(range(10000),costList)
plt.xlabel("the no. of iterations")
plt.ylabel("cost Error")
plt.title("LinearRegression")
plt.show()
return theta
if __name__ == "__main__":
weight = GradientDescent(Data,Y,theta,alpha)
print weight
cost = CostFunction(Data, Y, weight)
print cost

上图是Loss Error 随 迭代次数变化的曲线,显然,在迭代4000次左右后随着迭代次数增加,loss下降缓慢。

注:在这里只是简单的利用LMS Loss Function 和 GD对Linear Regression进行了编写,并没有预测

LinearRegression的更多相关文章

  1. 代码-Weka的LinearRegression类

    package kit.weka; import weka.classifiers.Evaluation; import weka.classifiers.functions.LinearRegres ...

  2. sklearn库 线性回归库 LinearRegression

    import numpy as np import sklearn.datasets #加载原数据 from sklearn.model_selection import train_test_spl ...

  3. python 10大算法之一 LinearRegression 笔记

    简单的线性回归预测房价 #!/usr/bin/env python # encoding: utf-8 """ @version: @author: --*--. @fi ...

  4. sklearn.linear_model.LinearRegression

    官网:http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html class ...

  5. Python机器学习/LinearRegression(线性回归模型)(附源码)

    LinearRegression(线性回归) 2019-02-20  20:25:47 1.线性回归简介 线性回归定义: 百科中解释 我个人的理解就是:线性回归算法就是一个使用线性函数作为模型框架($ ...

  6. spark LinearRegression 预测缺失字段的值

    最近在做金融科技建模的时候,字段里面很多缺少值得时候,模型对于新用户的预测会出现很大的不稳定,即PSI较大的情况. 虽然我们依据字段IV值得大小不断的在调整字段且开发新变量,但是很多IV值很大的字段直 ...

  7. 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价

    python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...

  8. 机器学习之LinearRegression与Logistic Regression逻辑斯蒂回归(三)

    一 评价尺度 sklearn包含四种评价尺度 1 均方差(mean-squared-error) 2 平均绝对值误差(mean_absolute_error) 3 可释方差得分(explained_v ...

  9. 线性回归模型之LinearRegression和SGDRegressor

    用美国波士顿的房价数据来介绍如何使用LR和SGDR模型进行预测 # 从sklearn.datasets导入波士顿房价数据读取器. from sklearn.datasets import load_b ...

随机推荐

  1. [VB.NET]拖动操作的技术基础

    1.设置需要接受拖动事件的控件的AllowDrop属性为True,在开发界面时配置,不可通过代码配置 2.在DragEnter事件处理程序里,为e.Effet设置枚举值,否则拖动无效 Private ...

  2. 使用Spring的Property文件存储测试数据 - 添加测试数据

    测试数据直接写在Property文件中,如下图: application.properties是系统自动生成,我添加了两个Property文件:HomeTestData.properties和moto ...

  3. Android开发-API指南-<service>

    <service> 英文原文:http://developer.android.com/guide/topics/manifest/service-element.html 采集(更新)日 ...

  4. 慕课网-安卓工程师初养成-4-9 Java循环语句之 for

    来源:http://www.imooc.com/code/1425 Java 的循环结构中除了 while 和 do...while 外,还有 for 循环,三种循环可以相互替换. 语法: 执行过程: ...

  5. .NET MVC4.0与CA对接

    1.改web.confog 2.引用CA提供的 dll 3.在controller层加个方法,记得加上授权认证的特性,获取信息 [Authorize] publi void calogin() { H ...

  6. 洛谷P1827 美国血统 American Heritage

    P1827 美国血统 American Heritage 54通过 90提交 题目提供者JOHNKRAM 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 农夫约翰非 ...

  7. phpexcel 导入导出

    导出excel /** * 以下是使用示例,对于以 //// 开头的行是不同的可选方式,请根据实际需要 * 打开对应行的注释. * 如果使用 Excel5 ,输出的内容应该是GBK编码. */ //r ...

  8. ViewPager撤消左右滑动切换功能

    ViewPager取消左右滑动切换功能 最近做项目要求某种情况下ViewPager不能滑动,那么我们只需要重写这个方法就可以禁止ViewPager滑动 IndexViewPager.java: imp ...

  9. ant风格是什么?

    我们在看java技术书籍的过程中,当加载文件时总会遇到是否支持ant风格路径加载,这里说的ant风格是什么意思呢,今天我查了一下,明白了什么意思,现在总结一下 ANT通配符有三种: 通配符 说明 ? ...

  10. 安装Ecshop首页出现报错:Only variables should be passed by referen

    出现下面这就话: Strict Standards: Only variables should be passed by reference in E:\Tools\ECShop_V2.7.3_UT ...