## 多项式回归学习笔记 转

AllenOR灵感

python版本：2.7.5

sklearn版本：0.18.2

tensorflow版本 ：1.2.1

## 多项式的定义及展现形式

本文讨论的是一元多项式相关问题。

y = a0 + a1 * x + a2 * (x**2) + ... + an * (x ** n) + e

y = a0 + a1 * x + a2 * (x**2) + e

``````#! /usr/bin/env python
#-*- coding:utf-8 -*-
import pylab
import pandas as pd

def fun(x):
# y = a0 + a1 * x + a2 * (x**2) + e
a0,a1,a2,e = 10,2,-0.03,0.5
y = a0 + a1 * x + a2 * (x**2) + e
return y

arrX = range(-10000,10000)
arrY = []

for x in arrX :
arrY.append(fun(x))

pylab.plot(arrX,arrY)
pylab.show()``````

y = a0 + a1 * x + a2 * (x**2) + a3 * (x**3) + e

``````#! /usr/bin/env python
#-*- coding:utf-8 -*-
import pylab
import pandas as pd

def fun(x):
# y = a0 + a1 * x + a2 * (x**2) + a3 * (x**3)+ e
a0,a1,a2,a3,e = 10,-0.2,-0.03,-0.04,0.5
y = a0 + a1 * x + a2 * (x**2) + a3 * (x**3)+ e
return y

arrX = range(-10000,10000)
arrY = []

for x in arrX :
arrY.append(fun(x))

pylab.plot(arrX,arrY)
pylab.show()``````

## 多项式回归

### 使用sklearn解决多项式回归问题

``````#! /usr/bin/env python
#-*- coding:utf-8 -*-
# 多项式回归
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

rng = np.random.RandomState(1)

def fun(x):
a0,a1,a2,a3,e = 0.1,-0.02,0.03,-0.04,0.05
y = a0 + a1 * x + a2 * (x**2) + a3 * (x**3)+ e
y += 0.03 * rng.rand(1)
return y

plt.figure()
plt.title('polynomial regression(sklearn)')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)

X = np.linspace(-1, 1, 30)
arrY = [fun(x) for x in X]
X = X.reshape(-1,1)
y = np.array(arrY).reshape(-1,1)

plt.plot(X, y, 'k.')

qf = PolynomialFeatures(degree=3)
qModel = LinearRegression()
qModel.fit(qf.fit_transform(X), y)

X_predict = np.linspace(-1, 2, 100)
X_predict_result = qModel.predict(qf.transform(X_predict.reshape(X_predict.shape[0], 1)))
plt.plot(X_predict,X_predict_result , 'r-')

plt.show()``````

### 使用tensorflow解决多项式回归问题

``````#! /usr/bin/env python
#-*- coding:utf-8 -*-

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

learning_rate = 0.01
training_epochs = 40
rng = np.random.RandomState(1)

def fun(x):
a0,a1,a2,a3,e = 0.1,-0.02,0.03,-0.04,0.05
y = a0 + a1 * x + a2 * (x**2) + a3 * (x**3)+ e
y += 0.03 * rng.rand(1)
return y

trX = np.linspace(-1, 1, 30)
arrY = [fun(x) for x in trX]
num_coeffs = 4
trY = np.array(arrY).reshape(-1,1)

X = tf.placeholder("float")
Y = tf.placeholder("float")

def model(X, w):
terms = []
for i in range(num_coeffs):
term = tf.multiply(w[i], tf.pow(X, i))
terms.append(term)

w = tf.Variable([0.] * num_coeffs, name="parameters")
y_model = model(X, w)

cost = tf.reduce_sum(tf.square(Y-y_model))

with tf.Session() as sess :
init = tf.global_variables_initializer()
sess.run(init)

for epoch in range(training_epochs):
for (x, y) in zip(trX, trY):
sess.run(train_op, feed_dict={X: x, Y: y})

w_val = sess.run(w)
print(w_val)

plt.figure()
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.title('polynomial regression(tensorflow)')
plt.scatter(trX, trY)
trX2 = np.linspace(-1, 2, 100)
trY2 = 0
for i in range(num_coeffs):
trY2 += w_val[i] * np.power(trX2, i)
plt.plot(trX2, trY2, 'r-')
plt.show()``````

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170804_多项式回归学习笔记.rst

### AllenOR灵感

robin_Xu_shuai
2018/01/03
0
0
sklearn学习笔记之简单线性回归

2017/12/30
0
0
Python机器学习笔记——监督学习

2017/08/08
0
0
Machine Learning笔记（三） 多变量线性回归

Machine Learning笔记（三） 多变量线性回归 注：本文内容资源来自 Andrew Ng 在 Coursera上的 Machine Learning 课程，在此向 Andrew Ng 致敬。 一、多特征（Multiple Features） 笔记（二）...

Gaussic
2015/09/26
1K
0

cc12222032
2018/03/29
0
0

EDI 电子数据交换全解指南

EDI（Electronic Data Interchange，电子数据交换)技术使得企业与企业（B2B）实现通信自动化，帮助交易伙伴和组织更快更好地完成更多工作，并消除了人工操作带来的错误。从零售商到制造商、物...

EDI知行软件

3
0
CentOS7的LVM动态扩容

# 问题 CentOS7上面的磁盘空间有点紧张，需要扩容。 解决 查询当前磁盘状态 [root@xxx ~]# lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTfd0 2:0 1 4K ...

5
0
Kafka 0.8 Producer (0.9以前版本适用)

Kafka旧版本producer由scala编写，0.9以后已经废除 示例代码如下： import kafka.producer.KeyedMessage;import kafka.javaapi.producer.Producer;import kafka.producer.ProducerConfig;......

5
0
Giraph源码分析（八）—— 统计每个SuperStep中参与计算的顶点数目

6
0
Navicat 快捷键

13
0