linear_regression
来源:互联网 发布:北大图书馆知乎 编辑:程序博客网 时间:2024/06/06 10:44
Linear Regression
The objective of linear regression is to minimise the cost fucntion:
The parameters of your model are the
There are two kinds of functions to solve this problem:
Least Square method
Batch Gradient Descent
Derivative of
#!/usr/bin/env python# -*- coding: utf-8 -*-##################################Author: Vincent.Y################################import numpy as npclass LinearRegression(object): """ solver: ls for least square method, sgd for gridient descent """ def __init__(self,solver="ls",lr=0.2,max_iter=200,bias=False): self.solver=solver self.coef_=None self.bias=bias if self.solver=='sgd': self.lr=lr self.max_iter=max_iter def gradient_descent(self,X,y): m=len(y) for i in xrange(0,self.max_iter): pred=X.dot(self.coef_) for j in xrange(0,X.shape[1]): tmp=X[:,j] errors = (pred - y) * tmp#element-wise multi self.coef_[j]=self.coef_[j] - self.lr * np.mean(errors) return self.coef_ def fit(self,X,y): if self.bias: X = np.hstack([X,np.ones((X.shape[0],1))]) if self.solver=="ls": self.coef_=np.linalg.lstsq(X,y)[0] else: self.coef_=np.zeros(X.shape[1]) self.coef_=self.gradient_descent(X,y) def predict(self,X): if self.bias: X = np.hstack([X,np.ones((X.shape[0],1))]) return X.dot(self.coef_)if __name__=="__main__": x=np.array([1,2,3]) x=x.reshape(-1,1) y=np.array([3,5,7]) model=LinearRegression(bias=True) model.fit(x,y) print model.coef_ print model.predict(x) model=LinearRegression(bias=True,solver='sgd',max_iter=1000) model.fit(x,y) print model.coef_ print model.predict(x)
https://github.com/muyinanhai/ml-learn/
0 0
- linear_regression
- Linear_regression
- sklearn-linear_regression
- tensorflow linear_regression 实例解析
- TensorFlow简单实例(一):linear_regression
- Linear_regression与 Logistic_regression简单比较与python实现
- 线性回归(linear_regression),多项式回归(polynomial regression)(Tensorflow实现)
- Mars-HTML5(三)常用文本标签
- UGUI 滚动优化之 隐藏视口外的GameObject
- 动态规划算法总结
- 11、python标准库概览
- tar命令基本用法
- linear_regression
- 二叉树的创建与前序遍历(递归方式)
- window 下mysql5.7.12安装报错记录
- java数据结构之(顺序栈+链式栈)
- <LeetCode OJ> 109 / 108 Convert Sorted ( List / Array ) to Binary Search Tree
- NodeJs——(1)封装,调用,执行,访问路径,http,函数编程,等待函数,事件监听
- sql技巧-构造分组条件
- 【黑马Android】(13)Linux操作系统/cron计划任务
- android socket 通信(2)--传文件