LinearRegression
利用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的更多相关文章
- 代码-Weka的LinearRegression类
package kit.weka; import weka.classifiers.Evaluation; import weka.classifiers.functions.LinearRegres ...
- sklearn库 线性回归库 LinearRegression
import numpy as np import sklearn.datasets #加载原数据 from sklearn.model_selection import train_test_spl ...
- python 10大算法之一 LinearRegression 笔记
简单的线性回归预测房价 #!/usr/bin/env python # encoding: utf-8 """ @version: @author: --*--. @fi ...
- sklearn.linear_model.LinearRegression
官网:http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html class ...
- Python机器学习/LinearRegression(线性回归模型)(附源码)
LinearRegression(线性回归) 2019-02-20 20:25:47 1.线性回归简介 线性回归定义: 百科中解释 我个人的理解就是:线性回归算法就是一个使用线性函数作为模型框架($ ...
- spark LinearRegression 预测缺失字段的值
最近在做金融科技建模的时候,字段里面很多缺少值得时候,模型对于新用户的预测会出现很大的不稳定,即PSI较大的情况. 虽然我们依据字段IV值得大小不断的在调整字段且开发新变量,但是很多IV值很大的字段直 ...
- 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价
python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...
- 机器学习之LinearRegression与Logistic Regression逻辑斯蒂回归(三)
一 评价尺度 sklearn包含四种评价尺度 1 均方差(mean-squared-error) 2 平均绝对值误差(mean_absolute_error) 3 可释方差得分(explained_v ...
- 线性回归模型之LinearRegression和SGDRegressor
用美国波士顿的房价数据来介绍如何使用LR和SGDR模型进行预测 # 从sklearn.datasets导入波士顿房价数据读取器. from sklearn.datasets import load_b ...
随机推荐
- Oracle 启动状态解说
oracle 启动状态由nomount-mount-open 一. nomount状态下操作 08:09:49 idle> startup nomount; ORACLE instance st ...
- 文件的输出与载入之java操作
一.前言 学习java没多久,关键是没怎么系统学过.都是看别人的代码来学习的.今天就把一直以来让我头痛的java IO 的一些基本操作来记录下来,加深记忆. 二.java导入文件到内存中 首先放一个 ...
- python编写接口
- 如何在 Git 里撤销(几乎)任何操作
任何版本控制系统的一个最有的用特性就是“撤销 (undo)”你的错误操作的能力.在 Git 里,“撤销” 蕴含了不少略有差别的功能. 当你进行一次新的提交的时候,Git 会保存你代码库在那个特定时间点 ...
- Centos7 Openstack - (第一节)基本环境配置
Centos7 install Openstack - (第一节)基本环境配置 我的blog地址:http://www.cnblogs.com/caoguo 根据openstack官方文档配置 官方文 ...
- EXT学习之——Extjs 文本框 TextField 添加点击(onclick)事件方法
{ xtype:'textfield', listeners: { render: function(p) { // Append the Panel to the click handler's a ...
- [vsftp]500 OOPS: cannot change directory
这个报错需要检查 1./etc/passwd 用户的主目录 2./etc/vsftpd/vuser_conf 下每个用户的local_root 3.每个用户目录给ftpuser加上rwx权限,一定要有 ...
- Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7d90eb80-29e2-4238-b658-ade407ff9456. Last exception: [u'Traceback (most recent call last):\n', u' File "/usr/lib/py
Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7d90eb80-29e2-42 ...
- AceAdmin In MVC之控件
AceAdmin有很多Html控件,而下载下来之后全部混杂在一起,想用一个控件有时得调整半天,干脆整理出一个版本,而且结合起来MVC的封装.以后就不用一个js css的调了. 在MVC中Html的控件 ...
- SOS 调试扩展 (SOS.dll) 《第五篇》
一.SOS扩展命令 SOS包含几十个命令,要熟练使用SOS,首先要了解SOS有哪些命令.下面给出SOS命令列表. 命令 描述 BPMD [<module name> <method ...