Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matlotlib在中创建曲面打印_Python_Matplotlib_Plot_Surface - Fatal编程技术网

Python 使用matlotlib在中创建曲面打印

Python 使用matlotlib在中创建曲面打印,python,matplotlib,plot,surface,Python,Matplotlib,Plot,Surface,我正在使用Python3和spyder制作曲面图。然而,我的情节似乎没有正确地表现出来。下面是我从spyder终端看到的屏幕截图。等高线图显示数据确实正确。我不确定等高线图为什么不正确。曲面图也无法保存。所有保存的PNG均为空。 我的代码在下面 #Logistic regression data and link function. import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d imp

我正在使用Python3和spyder制作曲面图。然而,我的情节似乎没有正确地表现出来。下面是我从spyder终端看到的屏幕截图。等高线图显示数据确实正确。我不确定等高线图为什么不正确。曲面图也无法保存。所有保存的PNG均为空。

我的代码在下面

#Logistic regression data and link function.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D  
from matplotlib import cm

m= 800

#Define link function here
def g(z):
    g=1/(1+np.exp(-z))
    return g


def loglikelihood(X,y,beta):
    h = g(X@beta)
    b=np.divide(h,(1-h))
    b=np.reshape(np.log(b),(m,1))
    y=np.reshape(y,(1,m))
    c= np.dot(y,b)
    d = np.sum(np.log(1-h))
    l=c+d
    return l

#For producing y data values given true paramters theta and number of covariates
def logit_data(n,p, theta):
    #Define parameters

    #1)Number of covariates
    p_i = p+1  #with intercept
    p_i=np.int(p_i)

    #2) m as correct data type
    n=np.int(n)

    #4)Specify parameter valueas to be estimated
    theta=np.reshape(theta, (p_i,1))

    #5)Define distribution from which covariate values are drawn i.i.d., and initiate data values
    X=np.zeros((n,p_i))
    X[:,0]=1    #intercept
    mean=0
    sigma=1

    X[:,1:]=np.random.normal(mean,sigma,(n,p))


    #6)Produce y values treating y as a Bernoulli variable with p=g(X*theta)
    r=np.random.uniform(0,1,n)
    r=np.reshape(r, (len(r),1))
    htrue=g(X.dot(theta))
    y=htrue-r
    y[y>=0]=1
    y[y<0]=0

    return X, y



p=2
theta = [0,2.5,4]

interval = 100
theta1try = np.linspace(0,5,interval)
theta2try = np.linspace(2,6,interval)

X,y = logit_data(m,p,theta)

A=np.zeros((interval,interval))

beta = [0,0,0]
for i in range(interval):
    for j in range(interval):
        beta[1] = theta1try[i]
        beta[2] = theta2try[j]
        A[i,j] = loglikelihood(X,y,beta)


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

P,Q=np.meshgrid(theta1try,theta2try)
ax.plot_surface(theta1try,theta2try,A, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_title('Value of log likelihood function for varied parameter vectors theta')
ax.set_xlabel('theta 1')
ax.set_ylabel('theta 2')
ax.set_zlabel('log likelihood')
plt.show()
plt.savefig('loglikelihoodmesh1.png')

plt.contour(theta1try,theta2try,A)
plt.show()
#逻辑回归数据和链接函数。
将matplotlib.pyplot作为plt导入
将numpy作为np导入
从mpl_toolkits.mplot3d导入Axes3D
从matplotlib导入cm
m=800
#在这里定义链接函数
def g(z):
g=1/(1+np.exp(-z))
返回g
def对数可能性(X、y、β):
h=g(X@beta)
b=np.除(h,(1-h))
b=np.整形(np.对数(b)、(m,1))
y=np.重塑(y,(1,m))
c=np.点(y,b)
d=np.和(np.对数(1-h))
l=c+d
返回l
#用于生成给定真参数θ和协变量数的y数据值
def logit_数据(n,p,θ):
#定义参数
#1) 协变量数
p_i=p+1,带截距
p_i=np.int(p_i)
#2) m作为正确的数据类型
n=np.int(n)
#4) 指定要估计的参数值
θ=np.重塑(θ,(p_i,1))
#5) 定义从中提取协变量值的分布i.i.d.,并启动数据值
X=np.零((n,p_i))
X[:,0]=1#截距
平均值=0
西格玛=1
X[:,1::]=np.随机.正态(平均值,西格玛,(n,p))
#6) 产生y值,将y视为p=g(X*θ)的伯努利变量
r=np.随机均匀(0,1,n)
r=np.整形(r,(透镜(r),1))
htrue=g(X.dot(θ))
y=htrue-r
y[y>=0]=1

y[yCode不运行:
回溯(最近一次调用):文件“”,第66行,在X中,y=logit_数据(n,p,θ)名称错误:名称“n”未定义
-您能否创建一个?Sth副本,显示您想要的内容,但去掉了所有不需要的内容?对我来说,3D绘图和轮廓看起来彼此一致。您可能想更详细地解释问题的确切位置。@ImportanceOfBeingErnest您能解释一下吗ai它们看起来如何一致?等高线图显示我预期会有一个峰值(这是我在理论基础上预期的)。我希望我能得到一个类似于堆栈交换帖子的图,但我没有看到我的峰值。它只是看起来像一张曲线纸!(我还修复了代码中的一个字母拼写错误)啊,我明白了。你可能想画
ax。画曲面(P,Q,A,…)