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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四