【python】生成三维图实例

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import csv
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from datetime import datetime
from sklearn.metrics import explained_variance_score
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import explained_variance_score
from sklearn import metrics
from sklearn.metrics import mean_absolute_error # 平方绝对误差
import random
import pandas as pd
from matplotlib import pyplot

#创建一个Axes3d对象
fig=plt.figure()
ax=Axes3D(fig)

#x,y的取值返回是从-5到+5,每隔0.25取一个点
x=np.arange(0.0001,200.0001,2)#惩罚系数
y=np.arange(0.0001,50.0001,1)
# x=np.arange(1, 3, 0.25)
# y=np.arange(1, 2, 0.25)
#[X,Y] = meshgrid(x,y) 将向量x和y定义的区域转换成矩阵X和Y,
#这两个矩阵可以用来表示mesh和surf的三维空间点以及两个变量的赋值。
#其中矩阵X的行向量是向量x的简单复制,而矩阵Y的列向量是向量y的简单复制。
x,y=np.meshgrid(x,y)
# z=x**2+y**2
# print(len(x[0]))#8
# print(len(x))#4
# print(len(y))#4
# [[1.   1.25 1.5  1.75 2.   2.25 2.5  2.75]
#  [1.   1.25 1.5  1.75 2.   2.25 2.5  2.75]
#  [1.   1.25 1.5  1.75 2.   2.25 2.5  2.75]
#  [1.   1.25 1.5  1.75 2.   2.25 2.5  2.75]]
# print(x)
# [[1.   1.   1.   1.   1.   1.   1.   1.  ]
#  [1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25]
#  [1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5 ]
#  [1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.75]]
# print(y)
R = np.random.randint(2,size=(50,100)).astype(float)
for i in range(len(x)):#50#
    for j in range(len(x[0])):#100
        model_svr = SVR(C=x[i][j], epsilon=0.01, gamma=y[i][j])  # pos3
        model_svr.fit(x_train, y_train)
        predict_results = std_y.inverse_transform(model_svr.predict(x_test))  # 预测结果
        R[i][j]=metrics.r2_score(y_test, predict_results)
        # error1 = metrics.r2_score(y_test, predict_results)#决定系数
        # MAPE = metrics.mean_absolute_percentage_error(y_test, predict_results) #平均绝对百分误差
        # RMSE = metrics.mean_squared_error(y_test, predict_results) ** 0.5 #均方根误差

#plot_surface 是绘制一个平面 ax.scatter 是绘制点
surf=ax.plot_surface(x,y,R,cmap=plt.get_cmap('rainbow'))
# 设置图形轴标签
ax.set_xlabel('C')
ax.set_ylabel('gamma')
ax.set_zlabel('R2')

plt.show()

你可能感兴趣的:(工具,算法,python,机器学习,numpy)