使用 Python 实现
第一步骤:加载样本数据
# 首先,需要计算x和y的均值
meanX = sum(x) / len(x)
meanY = sum(y) / len(y)
第二步骤:根据学习模型,计算 w,b
# 加载样本数据
# x 是商品房面积
x = [137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21]
# y 是房价
y = [145.00, 110.00, 93.00, 116.00, 65.32, 104.00, 118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30]
# 学习模型——计算w, b
# 首先,需要计算x和y的均值
meanX = sum(x) / len(x)
meanY = sum(y) / len(y)
sumXY = 0.0
sumX = 0.0
for i in range(len(x)):
# 得到分子
sumXY += (x[i] - meanX) * (y[i] - meanY)
# 得到分母
sumX += (x[i] - meanX) * (x[i] - meanX)
w = sumXY/sumX
b = meanY-w*meanX
print("w=", w) # w= 0.8945605120044221
print("b=", b) # b= 5.410840339418002
print(type(w)) # <class 'float'>
print(type(b)) # <class 'float'>
第三步骤:得到 w 和 b之后,预测房价
# 将待评估的商品房面积放入列表x_test中
x_test = [128.15, 45.00, 141.43, 106.27, 99.00, 53.84, 85.36, 70.00]
print("面积\t估计房价")
for i in range(len(x_test)):
print(x_test[i], '\t', round(w*x_test[i]+b, 2)) # 精确到小数点后两位
# 输出类型为浮点数类型,Python3中的浮点数类型是64位的双精度浮点数,我们并不需要这么精确的
# 房价, 这里的房价单位是万元, 对于买房子的人来说,精确到小数点后两位就可以了。
"""
面积 估计房价
128.15 120.05
45.0 45.67
141.43 131.93
106.27 100.48
99.0 93.97
53.84 53.57
85.36 81.77
70.0 68.03
"""
使用 NumPy 实现
import numpy as np
# 加载样本数据
# x 是商品房面积
x = np.array([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
# y 是房价
y = np.array([145.00, 110.00, 93.00, 116.00, 65.32, 104.00, 118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
# 学习模型——计算w, b
# 首先,需要计算x和y的均值
# np.mean是NumPy中求均值函数
meanX = np.mean(x)
meanY = np.mean(y)
# NumPy支持数组之间的运算,也支持数组和数字之间的广播运算
# 因此这一步可以直接运算, 无需使用循环语句
sumXY = np.sum((x-meanX)*(y-meanY))
sumX = np.sum((x-meanX)*(x-meanX))
w = sumXY / sumX
b = meanY - w * meanX
# tensorflow中默认的浮点数类型是64位浮点数,这里进行四舍五入,指定输出2位小数。
print("w=", round(w, 2)) # w= 0.89
print("b=", round(b, 2)) # b= 5.41
print(type(w)) # <class 'numpy.float64'>
print(type(b)) # <class 'numpy.float64'>
# 将待评估的商品房面积放入列表x_test中
x_test = np.array([128.15, 45.00, 141.43, 106.27, 99.00, 53.84, 85.36, 70.00])
y_pred = w*x_test+b
# 输出预测的结果
print(y_pred)
"""
[120.04876995 45.66606338 131.92853355 100.47578595 93.97233103
53.57397831 81.77052564 68.03007618]
"""
print("面积\t估计房价")
# 采用更加友好的方式输出预测的结果
for i in range(y_pred.size):
print(x_test[i], '\t', np.round(y_pred[i], 2))
# np.round是NumPy中的四舍五入函数,与Python中的round功能一样
# 精确到小数点后两位
# 房价, 这里的房价单位是万元, 对于买房子的人来说,精确到小数点后两位就可以了。
"""
面积 估计房价
128.15 120.05
45.0 45.67
141.43 131.93
106.27 100.48
99.0 93.97
53.84 53.57
85.36 81.77
70.0 68.03
"""
使用 TensorFlow 实现
import tensorflow as tf
import numpy as np
# 加载样本数据
# x 是商品房面积
x = tf.constant([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
# y 是房价
y = tf.constant([145.00, 110.00, 93.00, 116.00, 65.32, 104.00, 118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
# 学习模型——计算w, b
# 首先,需要计算x和y的均值
# tf.reduce_mean是tensorflow中求均值函数
meanX = tf.reduce_mean(x)
meanY = tf.reduce_mean(y)
# tensorflow也支持数组之间的运算,也支持数组和数字之间的广播运算
# 因此这一步可以直接运算, 无需使用循环语句
sumXY = tf.reduce_sum((x - meanX) * (y - meanY))
sumX = tf.reduce_sum((x - meanX) * (x - meanX))
w = sumXY / sumX
b = meanY - w * meanX
print(type(w)) # <class 'tensorflow.python.framework.ops.EagerTensor'>
print(type(b)) # <class 'tensorflow.python.framework.ops.EagerTensor'>
# tensorflow中默认的浮点数类型是32位浮点数
print("w=", w) # w= tf.Tensor(0.8945604, shape=(), dtype=float32)
print("b=", b) # b= tf.Tensor(5.4108505, shape=(), dtype=float32)
# Tensor对象.numpy()的作用是将张量转换为NumPy数组
print("w=", w.numpy()) # w= 0.8945604
print("b=", b.numpy()) # b= 5.4108505
# 将待评估的商品房面积放入列表x_test中
x_test = tf.constant([128.15, 45.00, 141.43, 106.27, 99.00, 53.84, 85.36, 70.00])
y_pred = w * x_test + b
# 输出预测的结果
print(y_pred)
"""
tf.Tensor(
[120.04876 45.66607 131.92853 100.475784 93.97233 53.573982
81.77052 68.030075], shape=(8,), dtype=float32)
"""
print("面积\t估计房价")
# 如果想要向上面一样采用更加友好的方式输出预测的结果, 需要转换为numpy数组。
# tensorflow中没有 size 这个函数
for i in range(y_pred.numpy().size):
print(x_test[i].numpy(), '\t', np.round(y_pred[i].numpy(), 2))
# np.round是NumPy中的四舍五入函数,与Python中的round功能一样
# tensorflow中也有 round 函数
"""
面积 估计房价
128.15 120.05
45.0 45.67
141.43 131.93
106.27 100.48
99.0 93.97
53.84 53.57
85.36 81.77
70.0 68.03
"""
最后,一般会使用 NumPy 来读取和访问数据集,接收从CPU输出的中间结果或最终结果,完成数据交换和输入、输出的工作。
数据和模型可视化
综合采用 Pyhton、TensorFlow 和 Matplotlib 来演示。
# 数据和模型可视化
plt.figure()
# 绘制散点图,并添加图例
plt.scatter(x, y, color="red", label="销售记录")
plt.scatter(x_test, y_pred, color="blue", label="预测房价")
# 使用plot()函数绘制折线图,并添加图例
plt.plot(x_test, y_pred, color="green", label="拟合直线", linewidth=2)
# 设置坐标轴标签
plt.xlabel("面积(平方米)", fontsize=14)
plt.ylabel("价格(万元)", fontsize=14)
# 设置坐标轴范围
plt.xlim((40, 150))
plt.ylim((40, 150))
plt.suptitle("商品房销售价格评估系统v1.0", fontsize=20)
# 自动确定图例生成的最佳位置
plt.legend(loc="upper left")
plt.show()