# 1. 线性回归：根据出生率来预测平均寿命

• 问题描述 下面图片是关于出生率和平均寿命关系的可视化图片，数据来自全世界不同的国家。你会发现一个有趣的结论：对于一个地区，儿童越多，平均寿命就越短。详细请见.

• 数据描述 `Name: Birth rate - life expectancy in 2010 X = birth rate. Type: float. Y = life expectancy. Type: foat. Number of datapoints: 190`
• 方法 首先，我们假设出生率和寿命的关系是线性的，这就意味着我们可以找到类似`Y=wX+b`这种方程。 为了计算出w和b，我们将在一层神经网络使用反向传播算法。对于损失函数，使用均方差，在训练每一轮之后，我们计算出实际值与预测值Y之间的均方差。 `03_linreg_starter.py`
```# -*- coding: utf-8 -*-
# @Author: yanqiang
# @Date:   2018-05-10 22:31:37
import tensorflow as tf
import utils
import matplotlib.pyplot as plt

DATA_FILE = 'data/birth_life_2010.txt'

# Step 1: read in data from the .txt file
# data is a numpy array of shape (190, 2), each row is a datapoint

# Step 2: create placeholders for X (birth rate) and Y (life expectancy)
X = tf.placeholder(tf.float32, name='X')
Y = tf.placeholder(tf.float32, name='Y')

# Step 3: create weight and bias, initialized to 0
w = tf.get_variable('weights', initializer=tf.constant(0.0))
b = tf.get_variable('bias', initializer=tf.constant(0.0))

# Step 4: construct model to predict Y (life expectancy from birth rate)
Y_predicted = w * X + b

# Step 5: use the square error as the loss function
loss = tf.square(Y - Y_predicted, name='loss')

# Step 6: using gradient descent with learning rate of 0.01 to minimize loss
learning_rate=0.001).minimize(loss)

with tf.Session() as sess:
# Step 7: initialize the necessary variables, in this case, w and b
sess.run(tf.global_variables_initializer())

# Step 8: train the model
for i in range(100):  # run 100 epochs
for x, y in data:
# Session runs train_op to minimize loss
sess.run(optimizer, feed_dict={X: x, Y: y})
# Step 9: output the values of w and b
w_out, b_out = sess.run([w, b])

# uncomment the following lines to see the plot
plt.plot(data[:, 0], data[:, 1], 'bo', label='Real data')
plt.plot(data[:, 0], data[:, 0] * w_out + b_out, 'r', label='Predicted data')
plt.legend()
plt.show()```

`[utils.py以及以后其他代码都在github](https://github.com/chiphuyen/stanford-tensorflow-tutorials)` 预测结果：

0 条评论

## 相关文章

1160

4469

1632

1591

9966

### Python：SMOTE算法

17.11.28更新一下：最近把这个算法集成到了数据预处理的python工程代码中了，不想看原理想直接用的，有简易版的python开发：特征工程代码模版 ，进...

1654

4539

7296

80610

3487