Python绘制高斯分布图像

Python绘制高斯分布图像

一、需求介绍

我们这里旨在使用Python来绘制图像,其他的操作一概先不管,绘制高斯分布的图像。
在这里插入图片描述

二、第一个任务

在这里插入图片描述

代码

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import beta
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import math
import random


def x_gauss(mu=0, sigma=1):
    """
    x_gauss(you can change this method to make it can be input.)
    :param mu: mu_x
    :param sigma: sigma_x
    :return: mu, sigma
    """
    return mu, sigma
    # multiple variables.


def y_gauss(mu=0, sigma=1):
    """
    y_gauss(you can change this method to make it can be input.)
    :param mu: mu_y
    :param sigma: sigma_y
    :return: mu, sigma
    """
    return mu, sigma
    # multiple variables.


def z_beta(alpha0=1, p0=1):
    """
    z_beta(you can change this method to make it can be input.)
    :param alpha0: alpha0
    :param p0: p0
    :return: alpha0, p0
    """
    return alpha0, p0
    # p is a multiple variable, but alpha is not.


if __name__ == '__main__':
    mu_x, sigma_x = x_gauss()
    mu_y, sigma_y = y_gauss()
    # get the mu and sigma parameter of the gauss.

    X = np.arange(mu_x - 5 * sigma_x, mu_x + 5 * sigma_x, 10 * sigma_x / 100)
    # range is related with sigma_x.
    Y = np.arange(mu_y - 5 * sigma_y, mu_y + 5 * sigma_y, 10 * sigma_y / 100)
    # range is related with sigma_y.
    # X and Y are arrays, ranging from mu - 5 * sigma to mu + 5 * sigma.

    X, Y = np.meshgrid(X, Y)
    # make meshgrided.

    alpha, p = z_beta()
    eta = beta.pdf(Y, alpha, p)  # Beta.
    # the equation of the eta.(eta ~ B(1, p))
    # however, as i need a range, so i use the range of Y.

    Z = \
        (1 / (pow(2 * math.pi, 1 / 2))) \
        * np.exp(- ((X - mu_x) ** 2) / (2 * (sigma_x ** 2))) \
        + eta * \
        (1 / (pow(2 * math.pi, 1 / 2))) \
        * np.exp(- ((Y - mu_y) ** 2) / (2 * (sigma_y ** 2)))
    # Z = X + eta * Y.

    list_z = []
    # hist list.
    for line in Z:  # 100 lines.
        appending = random.choices(line, k=10)
        # 100 lines, 10 choices => 100 * 10 = 1000.

        for data in appending:
            list_z.append(data)

    """
    two pictures => two windows.
    one is hist,
    the other is 3D.
    """

    # print(list_z, len(list_z))
    # 1000 points.
    plt.title('N~Z')
    # title.
    plt.xlabel('Z=X+η*Y')
    plt.ylabel('N')
    # labels.
    plt.hist(list_z)
    # draw the hist.

    fig = plt.figure()
    ax = Axes3D(fig)
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.5, cmap=cm.coolwarm)
    # draw the 3D function.
    plt.show()
    # show.



效果:
在这里插入图片描述
以及:
在这里插入图片描述

三、第二个任务

代码

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import beta
import math
import random


def x_gauss(mu=0, sigma=1):
    """
    x_gauss(you can change this method to make it can be input.)
    :param mu: mu_x
    :param sigma: sigma_x
    :return: mu, sigma
    """
    return mu, sigma
    # multiple variables.


def y_gauss(mu=0, sigma=1):
    """
    y_gauss(you can change this method to make it can be input.)
    :param mu: mu_y
    :param sigma: sigma_y
    :return: mu, sigma
    """
    return mu, sigma
    # multiple variables.


def z_beta(alpha0=1, p0=1):
    """
    z_beta(you can change this method to make it can be input.)
    :param alpha0: alpha0
    :param p0: p0
    :return: alpha0, p0
    """
    return alpha0, p0
    # p is a multiple variable, but alpha is not.


if __name__ == '__main__':
    """
    n is a multiple variable.
    """
    n = 100
    # multiple variable.

    mu_x, sigma_x = x_gauss()
    mu_y, sigma_y = y_gauss()
    # get the mu and sigma parameter of the gauss.

    X = np.arange(mu_x - 5 * sigma_x, mu_x + 5 * sigma_x, 10 * sigma_x / 5000)
    # range is related with sigma_x.
    Y = np.arange(mu_y - 5 * sigma_y, mu_y + 5 * sigma_y, 10 * sigma_y / 5000)
    # range is related with sigma_y.
    # X and Y are arrays, ranging from mu - 5 * sigma to mu + 5 * sigma.

    X, Y = np.meshgrid(X, Y)
    # make meshgrided.

    alpha, p = z_beta()
    eta = beta.pdf(Y, alpha, p)  # Beta.
    # the equation of the eta.(eta ~ B(1, p))
    # however, as i need a range, so i use the range of Y.

    Z = \
        (1 / (pow(2 * math.pi, 1 / 2))) \
        * np.exp(- ((X - mu_x) ** 2) / (2 * (sigma_x ** 2))) \
        + eta * \
        (1 / (pow(2 * math.pi, 1 / 2))) \
        * np.exp(- ((Y - mu_y) ** 2) / (2 * (sigma_y ** 2)))
    # Z = X + eta * Y.

    u = []
    # calculate the u.(1000)
    for i in range(1000):  # 1000.
        z_i = random.choices(Z[i], k=n)  # k = n.
        # n z.

        # calculate the u.

        u.append((1 / pow(n * np.var(z_i), 1 / 2)) * (sum(z_i) - n * np.mean(z_i)))
        # Ui = (1 / pow(n * np.var(z_i), 1 / 2)) * (sum(z_i) - n * np.mean(z_i))
        # 1000 u.

    plt.title('N~Ui')
    # title.
    plt.xlabel('Ui')
    plt.ylabel('N')
    # labels.
    plt.hist(u)
    # show the u.
    plt.show()
    # show

效果:
在这里插入图片描述

四、readme文件

This is the homework, there are two packages,
homework1 and homework2, homework1 is related
to work 1, and homework2 is related to work 2.

There may be some modules that you do not have
in your environment, so maybe you should install
those modules first, such as, numpy, scipy,
matplotlib, mpl_toolkits and so on.

After adding all the modules, you can change the
parameters like mu, sigma, p and so on, well,
you can also keep the parameters as you want,
and then,you can run the project and get the
results.

In fact, homework2 is related to homework1,
but, in order to make the question more clear,
i divide the whole question into two small
questions, all in all, they are the same.

以上就是使用Python绘制高斯分布图像的一个案例啦,希望对大家有一些帮助啦,最后感谢大家的阅读与支持了啦。

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhh江月

您的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值