蛙蛙推荐: TensorFlow Hello World 之平面拟合 (2)

把原始的分布在三维空间的点,组成一个个的三元组,分别表示 x, y, z 的坐标值

import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt points = list(zip(x_data[0],x_data[1],y_data)) points[:10] [(0.35073978, 0.33513898, 0.40210177302360534), (0.16348423, 0.07861521, 0.33207146525382997), (0.7059651, 0.58426493, 0.4874494969844818), (0.7696817, 0.87010854, 0.5509898781776428), (0.4036316, 0.24188931, 0.3887410223484039), (0.52306384, 0.64622885, 0.4815521538257599), (0.8748454, 0.39593607, 0.4666717529296875), (0.52280265, 0.4805421, 0.44838868379592894), (0.9512267, 0.6906034, 0.5332433462142945), (0.10213694, 0.41190282, 0.3925942569971085)] w_val = sess.run(W) b_val = sess.run(b) def cross(a, b): return [a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1] - a[1]*b[0]] def show(points, a, b, c): # 定义画布 fig = plt.figure(figsize=(20, 14)) ax = fig.add_subplot(111, projection='3d') # 绘制原始的散点 xs, ys, zs = zip(*points) ax.scatter(xs, ys, zs) # 绘制拟合平面 point = np.array([0.0, 0.0, c]) normal = np.array(cross([1,0,a], [0,1,b])) d = -point.dot(normal) xx, yy = np.meshgrid([0,1], [0,1]) z = (-normal[0] * xx - normal[1] * yy - d) * 1. / normal[2] ax.plot_surface(xx, yy, z, alpha=0.2, color=[0,1,0]) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show() show(points, w_val[0][0],w_val[0][1],b_val[0])

蛙蛙推荐: TensorFlow Hello World 之平面拟合

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwwzxg.html