# 1 加载样本数据
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 = [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]
# 2 学习模型:计算w,b
meanX = sum(x)/len(x)
meanY = sum(y)/len(y)
sumXY = 0.0
sumY = 0.0
for i in range(len(x)):
sumXY += (x[i]-meanX)*(y[i]-meanY)
sumY += (x[i]-meanX)*(x[i]-meanX)
w = sumXY/sumY
b = meanY - w*meanX
print("w=",w)
print("b=",b)
print(type(w),type(b))
输出结果为:
w= 0.8945605120044221
b= 5.410840339418002
<class 'float'> <class 'float'>
# 预测房价
x_test = [128.15,45.00,141.43,106.27,99.00,53.84,85.36,70.00]
for i in range(len(x_test)):
print(x_test[i],"\t",w*x_test[i]+b)
输出结果为:
128.15 120.0487699527847
45.0 45.66606337961699
141.43 131.92853355220342
106.27 100.47578595012793
99.0 93.97233102785579
53.84 53.57397830573609
85.36 81.77052564411547
70.0 68.03007617972756
# 1 加载样本数据
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 = [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]
# 2 学习模型:计算w,b
meanX = sum(x)/len(x)
meanY = sum(y)/len(y)
sumXY = 0.0
sumY = 0.0
for i in range(len(x)):
sumXY += (x[i]-meanX)*(y[i]-meanY)
sumY += (x[i]-meanX)*(x[i]-meanX)
w = sumXY/sumY
b = meanY - w*meanX
print("w=",w)
print("b=",b)
print(type(w),type(b))
# 预测房价
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))
输出结果为:
w= 0.8945605120044221
b= 5.410840339418002
<class 'float'> <class 'float'>
面积 估计房价
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
import numpy as np
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 = 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])
meanX = np.mean(x)
meanY = np.mean(y)
sumXY = np.sum((x-meanX)*(y-meanY))
sumY = np.sum((x-meanX)*(x-meanX))
w = sumXY/sumY
b = meanY - w*meanX
print("w=",w)
print("b=",b)
print(type(w),type(b))
输出结果为:
w= 0.894560512004422
b= 5.410840339418002
<class 'numpy.float64'> <class 'numpy.float64'>
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("面积\t估计房价")
for i in range(len(x_test)):
print(x_test[i],"\t",np.round(y_pred[i],2))
输出结果为:
面积 估计房价
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
import numpy as np
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 = 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])
meanX = np.mean(x)
meanY = np.mean(y)
sumXY = np.sum((x-meanX)*(y-meanY))
sumY = np.sum((x-meanX)*(x-meanX))
w = sumXY/sumY
b = meanY - w*meanX
print("w=",w)
print("b=",b)
print(type(w),type(b))
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("面积\t估计房价")
for i in range(len(x_test)):
print(x_test[i],"\t",np.round(y_pred[i],2))
输出结果为:
w= 0.894560512004422
b= 5.410840339418002
<class 'numpy.float64'> <class 'numpy.float64'>
面积 估计房价
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
import tensorflow as tf
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 = 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])
meanX = tf.reduce_mean(x)
meanY = tf.reduce_mean(y)
sumXY = tf.reduce_sum((x-meanX)*(y-meanY))
sumY = tf.reduce_sum((x-meanX)*(x-meanX))
w = sumXY/sumY
b = meanY - w*meanX
print("w=",w.numpy())
print("b=",b.numpy())
print(type(w),type(b))
输出结果为:
w= 0.8945604
b= 5.4108505
<class 'tensorflow.python.framework.ops.EagerTensor'> <class 'tensorflow.python.framework.ops.EagerTensor'>
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)
import tensorflow as tf
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 = 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])
meanX = tf.reduce_mean(x)
meanY = tf.reduce_mean(y)
sumXY = tf.reduce_sum((x-meanX)*(y-meanY))
sumY = tf.reduce_sum((x-meanX)*(x-meanX))
w = sumXY/sumY
b = meanY - w*meanX
print("w=",w.numpy())
print("b=",b.numpy())
print(type(w),type(b))
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)
输出结果为
w= 0.8945604
b= 5.4108505
<class 'tensorflow.python.framework.ops.EagerTensor'> <class 'tensorflow.python.framework.ops.EagerTensor'>
tf.Tensor(
[120.04876 45.66607 131.92853 100.475784 93.97233 53.573982
81.77052 68.030075], shape=(8,), dtype=float32)
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
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 = 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])
# 3 学习模型-计算w、b
meanX = tf.reduce_mean(x)
meanY = tf.reduce_mean(y)
sumXY = tf.reduce_sum((x-meanX)*(y-meanY))
sumY = tf.reduce_sum((x-meanX)*(x-meanX))
w = sumXY/sumY
b = meanY - w*meanX
print("权值w=",w.numpy(),"\n偏置值b=",b.numpy())
print("线性模型:y=",w.numpy(),"* x + ",b.numpy())
输出结果为:
权值w= 0.8945604
偏置值b= 5.4108505
线性模型:y= 0.8945604 * x + 5.4108505
# 4 预测房价
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).numpy()
print("面积\t估计房价")
n = len(x_test)
for i in range(n):
print(x_test[i],"\t",round(y_pred[i],2))
输出结果为:
面积 估计房价
tf.Tensor(128.15, shape=(), dtype=float32) 120.05
tf.Tensor(45.0, shape=(), dtype=float32) 45.67
tf.Tensor(141.43, shape=(), dtype=float32) 131.93
tf.Tensor(106.27, shape=(), dtype=float32) 100.48
tf.Tensor(99.0, shape=(), dtype=float32) 93.97
tf.Tensor(53.84, shape=(), dtype=float32) 53.57
tf.Tensor(85.36, shape=(), dtype=float32) 81.77
tf.Tensor(70.0, shape=(), dtype=float32) 68.03
# 5 数据和模型可视化
plt.figure()
plt.scatter(x,y,color="red",label="销售记录")
plt.scatter(x_test,y_pred,color="blue",label="预测房价")
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()
# 1 导入库,设置字体
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
# 2 加载样本数据
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 = 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])
# 3 学习模型-计算w、b
meanX = tf.reduce_mean(x)
meanY = tf.reduce_mean(y)
sumXY = tf.reduce_sum((x-meanX)*(y-meanY))
sumY = tf.reduce_sum((x-meanX)*(x-meanX))
w = sumXY/sumY
b = meanY - w*meanX
print("权值w=",w.numpy(),"\n偏置值b=",b.numpy())
print("线性模型:y=",w.numpy(),"* x + ",b.numpy())
# 4 预测房价
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).numpy()
print("面积\t估计房价")
n = len(x_test)
for i in range(n):
print(x_test[i],"\t",round(y_pred[i],2))
# 5 数据和模型可视化
plt.figure()
plt.scatter(x,y,color="red",label="销售记录")
plt.scatter(x_test,y_pred,color="blue",label="预测房价")
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()
输出结果为:
权值w= 0.8945604
偏置值b= 5.4108505
线性模型:y= 0.8945604 * x + 5.4108505
面积 估计房价
tf.Tensor(128.15, shape=(), dtype=float32) 120.05
tf.Tensor(45.0, shape=(), dtype=float32) 45.67
tf.Tensor(141.43, shape=(), dtype=float32) 131.93
tf.Tensor(106.27, shape=(), dtype=float32) 100.48
tf.Tensor(99.0, shape=(), dtype=float32) 93.97
tf.Tensor(53.84, shape=(), dtype=float32) 53.57
tf.Tensor(85.36, shape=(), dtype=float32) 81.77
tf.Tensor(70.0, shape=(), dtype=float32) 68.03
Tensorflow和Numpy中默认的浮点数类型分别为___A___。
A. float32 float64
# 1 加载样本数据
import numpy as np
# 房间面积
x1 = 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])
# 房间数
x2 = np.array([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
# 房价
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])
print(x1.shape,x2.shape,y.shape)
输出结果为:
(16,) (16,) (16,)
# 2 数据处理
x0 = np.ones(len(x1))
X = np.stack((x0,x1,x2),axis=1)
Y = np.array(y).reshape(-1,1)
功能 | 函数 |
---|---|
矩阵相乘 | np.matmul() |
矩阵转置 | np.transpose() |
矩阵求逆 | np.linalg.inv() |
# 3 求解模型参数
Xt = np.transpose(X) # 计算X'
XtX_1 = np.linalg.inv(np.matmul(Xt,X)) # 计算(X'X)-1
XtX_1_Xt = np.matmul(XtX_1,Xt) # 计算(X'X)-1X'
W = np.matmul(XtX_1_Xt,Y) # 计算(X'X)-1X'Y
W = W.reshape(-1) # 为了方便后面的引用
print(W)
print("多元线性回归方程:")
print("Y=",W[1]," * x1 + ",W[2]," * x2 + ",W[0])
输出结果为:
[11.96729093 0.53488599 14.33150378]
多元线性回归方程:
Y= [0.53488599] * x1 + [14.33150378] * x2 + [11.96729093]
print("请输入房屋面积和房间数,预测房屋销售价格:")
x1_test=float(input("商品房面积:"))
x2_test=int(input("房间数:"))
y_pred = W[1]*x1_test+W[2]*x2_test+W[0]
print("预测价格:",round(y_pred,2),"万元")
输出结果为:
请输入房屋面积和房间数,预测房屋销售价格:
商品房面积:120
房间数:4
预测价格: 133.48 万元
# 1 加载样本数据
import numpy as np
# 房间面积
x1 = 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])
# 房间数
x2 = np.array([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
# 房价
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])
print(x1.shape,x2.shape,y.shape)
# 2 数据处理
x0 = np.ones(len(x1))
X = np.stack((x0,x1,x2),axis=1)
Y = np.array(y).reshape(-1,1)
# 3 求解模型参数
Xt = np.transpose(X) # 计算X'
XtX_1 = np.linalg.inv(np.matmul(Xt,X)) # 计算(X'X)-1
XtX_1_Xt = np.matmul(XtX_1,Xt) # 计算(X'X)-1X'
W = np.matmul(XtX_1_Xt,Y) # 计算(X'X)-1X'Y
W = W.reshape(-1) # 为了方便后面的引用
print(W)
print("多元线性回归方程:")
print("Y=",W[1]," * x1 + ",W[2]," * x2 + ",W[0])
print("请输入房屋面积和房间数,预测房屋销售价格:")
x1_test=float(input("商品房面积:"))
x2_test=int(input("房间数:"))
y_pred = W[1]*x1_test+W[2]*x2_test+W[0]
print("预测价格:",round(y_pred,2),"万元")
输出结果为:
(16,) (16,) (16,)
[11.96729093 0.53488599 14.33150378]
多元线性回归方程:
Y= 0.5348859949724712 * x1 + 14.331503777673714 * x2 + 11.96729093053445
请输入房屋面积和房间数,预测房屋销售价格:
商品房面积:120
房间数:4
预测价格: 133.48 万元
功能 | 函数 |
---|---|
数组堆叠 | np.stack() |
改变数组形状 | np.reshape() |
矩阵相乘 | np.matmul() |
矩阵转置 | np.transpose() |
矩阵求逆 | np.linalg.inv() |
# 1 加载样本数据
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 房间面积
x1 = 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])
# 房间数
x2 = np.array([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
# 房价
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 = np.array([11.96729093,0.53488599,14.33150378])
y_pred = W[1]*x1+W[2]*x2+W[0]
fig = plt.figure(figsize=(8,6))
ax3d = Axes3D(fig)
ax3d.scatter(x1,x2,y,color="b",marker="*")
ax3d.set_xlabel('Area',color='r',fontsize=16)
ax3d.set_ylabel('Room',color='r',fontsize=16)
ax3d.set_zlabel('Price',color='r',fontsize=16)
ax3d.set_yticks([1,2,3]) # 设置y轴的坐标轴刻度,设置的是刻度的显示形式,而不是显示范围
ax3d.set_zlim3d(30,160)
plt.show()
# 1 加载样本数据
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 房间面积
x1 = 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])
# 房间数
x2 = np.array([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
# 房价
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 = np.array([11.96729093,0.53488599,14.33150378])
y_pred = W[1]*x1+W[2]*x2+W[0]
fig = plt.figure(figsize=(8,6))
ax3d = Axes3D(fig)
#ax3d.view_init(elev=0,azim=90) # 改变观察视角
ax3d.scatter(x1,x2,y,color="b",marker="*")
ax3d.set_xlabel('Area',color='r',fontsize=16)
ax3d.set_ylabel('Room',color='r',fontsize=16)
ax3d.set_zlabel('Price',color='r',fontsize=16)
ax3d.set_yticks([1,2,3]) # 设置y轴的坐标轴刻度,设置的是刻度的显示形式,而不是显示范围
ax3d.set_zlim3d(30,160)
plt.show()
view_init(elev,azim)
ax3d.view_init(elev=0,azim=90) # 改变观察视角
# 1 加载样本数据
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 房间面积
x1 = 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])
# 房间数
x2 = np.array([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
# 房价
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 = np.array([11.96729093,0.53488599,14.33150378])
X1,X2=np.meshgrid(x1,x2) # 生成网格点的坐标矩阵
Y_PRED = W[1]*X1+W[2]*X2+W[0] # 使用模型计算纵坐标
fig = plt.figure()
ax3d = Axes3D(fig)
ax3d.plot_surface(X1,X2,Y_PRED,cmap="coolwarm") # 颜色方案选择coolwarm
ax3d.set_xlabel('Area',color='r',fontsize=14)
ax3d.set_ylabel('Room',color='r',fontsize=14)
ax3d.set_zlabel('Price',color='r',fontsize=14)
ax3d.set_yticks([1,2,3]) # 设置y轴的坐标轴刻度,设置的是刻度的显示形式,而不是显示范围
plt.show()
# 1 加载样本数据
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 房间面积
x1 = 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])
# 房间数
x2 = np.array([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
# 房价
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 = np.array([11.96729093,0.53488599,14.33150378])
y_pred = W[1]*x1+W[2]*x2+W[0] # 使用模型计算纵坐标
plt.rcParams['font.sans-serif'] = ['SimHei']
X1,X2=np.meshgrid(x1,x2) # 生成网格点的坐标矩阵
Y_PRED = W[1]*X1+W[2]*X2+W[0] # 使用模型计算纵坐标
fig = plt.figure()
ax3d = Axes3D(fig)
ax3d.scatter(x1,x2,y,color="b",marker='*',label="销售记录") #实际房价绘制散点图
ax3d.scatter(x1,x2,y_pred,color='r',label="预测房价") # 估计房价绘制散点图
ax3d.plot_wireframe(X1,X2,Y_PRED,color="c",linewidth=0.5,label="拟合平面")
ax3d.set_xlabel('Area',color='r',fontsize=14)
ax3d.set_ylabel('Room',color='r',fontsize=14)
ax3d.set_zlabel('Price',color='r',fontsize=14)
ax3d.set_yticks([1,2,3]) # 设置y轴的坐标轴刻度,设置的是刻度的显示形式,而不是显示范围
plt.suptitle("商品房销售回归模型",fontsize=20)
plt.legend(loc="upper left")
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax3d = Axes3D(fig)
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.random.uniform(10,40,30)
y = np.random.uniform(100,200,30)
z = np.random.uniform(10,20,30)
fig = plt.figure()
ax3d = Axes3D(fig)
ax3d.scatter(x,y,z,c='b',marker="*")
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.random.uniform(10,40,30)
y = np.random.uniform(100,200,30)
z = 2*x+y
fig = plt.figure()
ax3d = Axes3D(fig)
ax3d.scatter(x,y,z,c='b',marker="*")
ax3d.set_xlabel('X')
ax3d.set_ylabel('Y')
ax3d.set_zlabel('Z=2X+Y')
plt.show()
np.meshgrid():生成网格点坐标矩阵
>>> import numpy as np
>>> x = [1,2,3,4]
>>> y=[4,5,6]
>>> X,Y=np.meshgrid(x,y)
>>> X
array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
>>> Y
array([[4, 4, 4, 4],
[5, 5, 5, 5],
[6, 6, 6, 6]])
Axes3D.plot_surface():绘制平面/曲面图
>>> import numpy as np
>>> x = np.arange(1,5)
>>> y = np.arange(1,5)
>>> X,Y=np.meshgrid(x,y)
>>> X.shape
(4, 4)
>>> Y.shape
(4, 4)
>>> Z=2*X+Y
>>> Z.shape
(4, 4)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.arange(1,5)
y = np.arange(1,5)
X,Y=np.meshgrid(x,y)
Z = 2*X + Y
fig = plt.figure()
ax3d = Axes3D(fig)
ax3d.plot_surface(X,Y,Z,cmap="rainbow")
# 按照彩虹的颜色顺序从高到低排序,Z值大靠近红色,Z值小靠近紫色,颜色相同的色块在同一高度上
# 由于只有4*4个数据,所以划分为3*3的九个格子
ax3d.set_xlabel('X')
ax3d.set_ylabel('Y')
ax3d.set_zlabel('Z=2X+Y')
plt.show()
x = np.arange(1,10)
y = np.arange(1,10)
或
x = np.arange(1,10,0.1)
y = np.arange(1,10,0.1)
试试看
如:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.arange(1,10,0.1)
y = np.arange(1,10,0.1)
X,Y=np.meshgrid(x,y)
Z = 2*X + Y
fig = plt.figure()
ax3d = Axes3D(fig)
surf=ax3d.plot_surface(X,Y,Z,cmap="rainbow")
# 按照彩虹的颜色顺序从高到低排序,Z值大靠近红色,Z值小靠近紫色,颜色相同的色块在同一高度上
# 由于只有4*4个数据,所以划分为3*3的九个格子
fig.colorbar(surf,shrink=0.5,aspect=5) # 在图的旁边显示颜色指示条
ax3d.set_xlabel('X')
ax3d.set_ylabel('Y')
ax3d.set_zlabel('Z=2X+Y')
plt.show()
Axes3D.plot_wireframe()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.arange(1,10,0.1)
y = np.arange(1,10,0.1)
X,Y=np.meshgrid(x,y)
Z = 2*X + Y
fig = plt.figure()
ax3d = Axes3D(fig)
ax3d.plot_wireframe(X,Y,Z,color='r',linewidth =0.5)
ax3d.set_xlabel('X')
ax3d.set_ylabel('Y')
ax3d.set_zlabel('Z=2X+Y')
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.arange(-5,5,0.1)
y = np.arange(-5,5,0.1)
X,Y=np.meshgrid(x,y)
Z = np.sin(np.sqrt(X**2+Y**2))
fig = plt.figure()
ax3d = Axes3D(fig)
surf=ax3d.plot_surface(X,Y,Z,cmap="rainbow")
# 按照彩虹的颜色顺序从高到低排序,Z值大靠近红色,Z值小靠近紫色,颜色相同的色块在同一高度上
# 由于只有4*4个数据,所以划分为3*3的九个格子
fig.colorbar(surf,shrink=0.5,aspect=5) # 在图的旁边显示颜色指示条
ax3d.set_xlabel('X')
ax3d.set_ylabel('Y')
ax3d.set_zlabel('Z=2X+Y')
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.arange(-5,5,0.1)
y = np.arange(-5,5,0.1)
X,Y=np.meshgrid(x,y)
Z = np.sin(np.sqrt(X**2+Y**2))
fig = plt.figure()
ax3d = Axes3D(fig)
ax3d.plot_wireframe(X,Y,Z,color='r',linewidth =0.5)
ax3d.set_xlabel('X')
ax3d.set_ylabel('Y')
ax3d.set_zlabel('Z=2X+Y')
plt.show()
[1] 神经网络与深度学习——TensorFlow实践