meshgrid
1 import numpy as np 2 from matplotlib import pyplot as plt 3 from mpl_toolkits.mplot3d import Axes3D 4 x = np.array([0,1,2]) 5 y = np.array([0,1]) 6 X,Y = np.meshgrid(x,y)#X,Y扩展成了矩阵, 7 print(X) 8 print(Y) 9 theta0, theta1, theta2 = 2, 3, 4 10 ax = Axes3D(plt.figure())#用来画三维图 11 Z = theta0 + theta1*X + theta2*Y#求z值 12 plt.plot(X,Y,'r.')#此时你会发现绘画出的是3*2个点,这些点组成一个网格,切每个点的坐标是X*Y的笛卡尔积 13 ax.plot_surface(X,Y,Z)#用来画三维图 14 plt.show()
mpl_toolkits.mplot3d
绘图时用到划分面板的方法
使用add_subplot
import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 100) fig = plt.figure() ax1 = fig.add_subplot(221) ax1.plot(x, x) ax2 = fig.add_subplot(222) ax2.plot(x, -x) ax3 = fig.add_subplot(223) ax3.plot(x, x ** 2) ax4 = fig.add_subplot(224) ax4.plot(x, np.log(x)) plt.show()
使用subplot方法
import numpy as np from matplotlib import pyplot as plt x = np.arange(10) plt.subplot(221) plt.plot(x,x) plt.subplot(223) plt.plot(x,-x) plt.show()
PolynomialFeatures
1 import numpy as np 2 import matplotlib.pyplot as plt 3 from sklearn.preprocessing import PolynomialFeatures#多项式 4 from sklearn.linear_model import LinearRegression 5 6 # 载入数据 7 data = np.genfromtxt("job.csv", delimiter=",") 8 x_data = data[1:,1] 9 y_data = data[1:,2] 10 plt.scatter(x_data,y_data) 11 plt.show() 12 #维度必须是二维 13 x_data = x_data[:,np.newaxis] 14 y_data = y_data[:,np.newaxis] 15 # 定义多项式回归,degree的值可以调节多项式的特征 16 poly = PolynomialFeatures(degree=4) 17 # 特征处理 18 x_poly = poly.fit_transform(x_data) 19 # 定义回归模型 20 model = LinearRegression() 21 # 训练模型 22 model.fit(x_poly,y_data) 23 plt.plot(x_data,y_data,'b.') 24 plt.plot(x_data,model.predict(poly.fit_transform(x_data)),'r') 25 plt.show()