[Python] 纯文本查看 复制代码from sklearn.neural_network import MLPRegressor
import pandas as pd
# 定义线性回归模型
import numpy as np
from pylab import mpl
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
#文本读取与处理
def readfile(path):
X = []
che = []
cai = []
for line in open(path,'r', encoding='UTF-8'):
#print(line)
xx,yy,chee,caii = line.split(',',3)
X.append([float(xx),float(yy)])
che.append(float(chee))
cai.append(float(caii))
#返回数据集列表
return(X,che)
if __name__=='__main__':
print('读取数据...')
X,y = readfile('data.csv')
print('创建神经网络模型')
clf = MLPRegressor(max_iter=400)
print('训练模型...')
clf.fit(X, y)
#向三维图中添加散点
print('生成散点图...')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i in range(len(X)):
ax.scatter(X[i][0], X[i][1],y[i], c='black', marker='o', s=4)
#向三维图中添加影响曲面
print('生成曲面...')
ps = np.arange(1, 7, 0.25)
py = np.arange(1, 7, 0.25)
XX, YY = np.meshgrid(ps, py)
R = np.random.randint(2,size=(24,24)).astype(float)
for i in range(len(XX)):
for j in range(len(XX[0])):
R[i][j]= round(clf.predict([[XX[i][j],YY[i][j]]])[0],1)
ax.plot_surface(XX, YY, R,rstride=1, cstride=1, cmap='Greys')#'rainbow')
# 设置图形坐标范围
ax.set_xlim3d(1, 7)
ax.set_ylim3d(1, 7)
ax.set_zlim3d(1, 7)
# 设置图形轴标签
ax.set_xlabel('探索式创新')
ax.set_ylabel('利用式创新')
ax.set_zlabel('企业成长绩效')
print('展示图表')
plt.show()