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将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专