# 机器学习常用算法-线性回归算法

## 线性回归

h_\theta(x)=\theta_0+\theta_1x

J(\theta)=J(\theta_0,\theta_1)=\frac 1{2m}\sum_{i=1}^m(h(x^{(i)})-y^{(i)})^2

\theta(j)=\theta(i)- \alpha\frac{\partial}{\partial\theta_i}J(\theta)

## 线性回归实例

import tensorflow as tf
import numpy as np
import mxnet as mx
from mxnet import gluon
from mxnet import ndarray as nd
from matplotlib import pyplot as plt
from numpy import genfromtxt
from sklearn.model_selection import train_test_split
import logging
%matplotlib inline
logging.getLogger().setLevel(logging.DEBUG)

def pre_boston_data():
'''准备boston的数据，构造训练数据集'''
features = np.array(boston.data)
lables = np.array(boston.target)
return features,lables

def normalizer(dataset):
'''对数据进行归一化'''
mean = np.mean(dataset,axis=0)
std = np.std(dataset,axis=0)
return (dataset-mean)/std

def bias_vector(features,lables):
'''规范合理化数据集'''
n_training_samples = features.shape[0]
n_dim = features.shape[1]
ones = np.ones(n_training_samples)
combine = np.c_[ones,features]
f=np.reshape(combine,[n_training_samples,n_dim+1])
l=np.reshape(labels,[n_training_samples,1])
return f,l

#数据预处理和规范化数据集
features,labels = pre_boston_data()
normolized_feature = normalizer(features)
data,label = bias_vector(normolized_feature,labels)
train_x,test_x,train_y,test_y = train_test_split(data,label,test_size = 0.25,random_state = 100)
n_dim = train_x.shape[1]
#开始训练数据集和构造模型算法
learning_rate = 0.01
training_epochs = 1000
log_loss = np.empty(shape=[1],dtype=float)
X = tf.placeholder(tf.float32,[None,n_dim])
Y = tf.placeholder(tf.float32,[None,1])
W = tf.Variable(tf.ones([n_dim,1]))
y_ = tf.matmul(X, W)
cost_op = tf.reduce_mean(tf.square(y_ - Y))
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
sess.run(training_step,feed_dict={X:train_x,Y:train_y})
log_loss = np.append(log_loss,sess.run(cost_op,feed_dict={X: train_x,Y: train_y}))
pred_y = sess.run(y_, feed_dict={X: test_x})
mse = tf.reduce_mean(tf.square(pred_y-test_y))
fig, ax = plt.subplots()
ax.scatter(test_y, pred_y)
ax.plot([test_y.min(), test_y.max()], [test_y.min(), test_y.max()], 'k--', lw=3)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')

75 篇文章22 人订阅

0 条评论

## 相关文章

1581

2622

33410

3203

### 新手入门机器学习十大算法

【磐创AI导读】：对于想要了解机器学习的新手，本文为大家总结了数据科学家最经常使用的十大机器学习算法来帮助大家快速入门。如果喜欢我们的文章，欢迎点赞、评论、转发...

851

39711

3457

3837

4054

3210