Python绘制高斯分布图像

2023-11-05 10:30

本文主要是介绍Python绘制高斯分布图像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Python绘制高斯分布图像

文章目录

    • Python绘制高斯分布图像
      • 一、需求介绍
      • 二、第一个任务
      • 三、第二个任务
      • 四、readme文件

一、需求介绍

我们这里旨在使用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 randomdef 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 randomdef 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绘制高斯分布图像的一个案例啦,希望对大家有一些帮助啦,最后感谢大家的阅读与支持了啦。

这篇关于Python绘制高斯分布图像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/349232

相关文章

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

Python打包成exe常用的四种方法小结

《Python打包成exe常用的四种方法小结》本文主要介绍了Python打包成exe常用的四种方法,包括PyInstaller、cx_Freeze、Py2exe、Nuitka,文中通过示例代码介绍的非... 目录一.PyInstaller11.安装:2. PyInstaller常用参数下面是pyinstal

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数