python 数据可视化 -- 生成可控的随机数据集合
生成可控的随机数据集合 使用 numpy.random 模块
numpy.random.random(size=None) 返回 [0.0, 1.0) 区间的随机 floats, 默认返回一个 float
numpy.random.randint(low, high=None, size=None, dtype='l') 按照均匀分布,返回 [low, high) 区间的随机 integers
numpy.random.uniform(low=0.0, high=1.0, size=None) 按照均匀分布,返回 [low, high) 区间的随机 floats
numpy.random.normal(loc=0.0, scale=1.0, size=None) 按照正态分布,返回随机 floats
numpy.random.triangular(left, mode, right, size=None) 按照三角分布,返回随机 floats
numpy.random.beta(a, b, size=None) 按照 beta 分布,返回随机 floats
numpy.random.exponential(scale=1.0, size=None) 按照指数分布,返回随机 floats
numpy.random.gamma(shape, scale=1.0, size=None) 按照 gamma 分布,返回随机 floats
numpy.random.lognormal(mean=0.0, sigma=1.0, size=None) 按照指数正态分布,返回随机 floats
numpy.random.pareto(a, size=None) 按照 pareto 分布,返回随机 floats
更多分布见 numpy.random 官网教程:https://docs.scipy.org/doc/numpy/reference/routines.random.html?highlight=random#module-numpy.random
import matplotlib.pyplot as plt
import numpy as np SAMPLE_SIZE = 100 np.random.seed()
real_rand_vars = [np.random.random() for _ in range(SAMPLE_SIZE)] # 生成 100 个 [0.0, 1.0) 的随机小数 plt.figure()
plt.hist(x = real_rand_vars, bins=10, rwidth=0.9, color='blue')
plt.xlabel('Number range')
plt.ylabel('Count')
plt.show()

使用相似的方法,可以生成虚拟价格增长数据的时序图,并加上随机噪声
import matplotlib.pyplot as plt
import numpy as np duration = 100
mean_inc = 0.2
std_dev_inc = 1.2 x = range(duration)
y = []
price_today = 0 for i in x:
next_delta = np.random.normal(loc=mean_inc, scale=std_dev_inc) # 按照给定的均值和方差的正态分布返回随机floats
price_today += next_delta
y.append(price_today) plt.figure()
plt.plot(x, y, 'b.-')
plt.xlabel('Time')
plt.ylabel('Value')
plt.show()

根据不同的需求,可以选择不同的分布
import matplotlib.pyplot as plt
import numpy as np
import matplotlib SAMPLE_SIZE = 1000
buckets = 100
matplotlib.rcParams.update({'font.size':7}) plt.figure()
# 第一个图是 [0,1) 之间分布的随机变量
plt.subplot(521)
plt.xlabel('random.random')
res = [np.random.random() for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets)
# 第二个图是一个均匀分布的随机变量
plt.subplot(522)
plt.xlabel('random.uniform')
a = 1
b = SAMPLE_SIZE
res = [np.random.uniform(a, b) for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets)
# 第三个图是一个三角形分布
plt.subplot(523)
plt.xlabel('random.triangular')
low = 1
mode = 100.0
high = SAMPLE_SIZE
res = [np.random.triangular(low, mode, high) for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets)
# 第四个图是一个 beta 分布
plt.subplot(524)
plt.xlabel('random.betavariate')
alpha = 1
beta = 10
res = [np.random.beta(alpha, beta) for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets)
# 第五个图是一个指数分布
plt.subplot(525)
plt.xlabel('random.expovariate')
lambd = 1.0 / ((SAMPLE_SIZE + 1) / 2.0)
res = [np.random.exponential(lambd) for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets)
# 第六个图是一个 gamma 分布
plt.subplot(526)
plt.xlabel('random.gammavariate')
alpha = 1
beta = 10
res = [np.random.gamma(alpha, beta) for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets)
# 第七个图是一个 对数正态分布
plt.subplot(527)
plt.xlabel('random.lognormvariate')
mu = 1
sigma = 0.5
res = [np.random.lognormal(mu, sigma) for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets)
# 第八个图是一个正态分布
plt.subplot(528)
plt.xlabel('random.normalvariate')
mu = 1
sigma = 0.5
res = [np.random.normal(mu, sigma) for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets)
# 第九个图是一个帕累托分布
plt.subplot(529)
plt.xlabel('random.paretovariate')
alpha = 1
res = [np.random.pareto(alpha) for _ in range(1, SAMPLE_SIZE)]
plt.hist(x=res, bins=buckets) plt.tight_layout()
plt.show()

python 数据可视化 -- 生成可控的随机数据集合的更多相关文章
- 开源 Web 相册程序: Photoview 和数据可视化生成工具:Datawrapper
Photoview Photoview是一个开源 Web 相册程序,Go 语言写的,使用 Docker 安装,可以用来快速架设个人相册. github地址:https://github.com/pho ...
- Python图表数据可视化Seaborn:3. 线性关系数据| 时间线图表| 热图
1. 线性关系数据可视化 lmplot( ) import numpy as np import pandas as pd import matplotlib.pyplot as plt import ...
- 使用vs2010生成SQL Server 随机数据
前几天做测试数据,偶然发现vs2010中有一个生成随机数据的功能,记录下来,方便以后使用,确实非常的好用灵活快捷. 为了简单扼要的说明,下面我用一个实例来说明如何快捷使用: 在VS2010创建数据库项 ...
- Python图表数据可视化Seaborn:2. 分类数据可视化-分类散点图|分布图(箱型图|小提琴图|LV图表)|统计图(柱状图|折线图)
1. 分类数据可视化 - 分类散点图 stripplot( ) / swarmplot( ) sns.stripplot(x="day",y="total_bill&qu ...
- python中faker模块:产生随机数据的模块
#pip install faker #产生各种随机数据的模块 想要运用更多的随机数据,可以百度查找下
- 学会这一招,小白也能使用数据可视化BI软件创建医院数据实时展示大屏
灯果数据可视化BI软件是新一代人工智能数据可视化大屏软件,内置丰富的大屏模板,可视化编辑操作,无需任何经验就可以创建属于你自己的大屏.大家可以在他们的官网下载软件. 本文以医院数据实时展示大屏为例 ...
- python脚本批量生成50000条插入数据的sql语句
f = open("xx.txt",'w') for i in range(1,50001): str_i = str(i) realname = "lxs"+ ...
- 数据科学速查手册(包括机器学习,概率,微积分,线性代数,python,pandas,numpy,数据可视化,SQL,大数据等方向)
介绍:https://redstonewill.com/2372/ 项目网址:https://github.com/FavioVazquez/ds-cheatsheets
- python 操作txt 生成新的文本数据
name: Jack ; salary: 12000 name :Mike ; salary: 12300 name: Luk ; salary: 10030 name :Tim ; salary: ...
随机推荐
- Exp1 PC平台逆向破解 20164311
实验目标: 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getSh ...
- python汉诺塔问题的递归理解
一.问题背景 汉诺塔问题是源于印度一个古老传说. 源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下 ...
- Django整理1
基本结构 │ db.sqlite3 ----------sqlie3数据库 │ manage.py │ ├─logres │ │ admin.py 后台,可以用很少量的代码就拥有一个强大的后台. │ ...
- JConsole监控Linux上的Tomcat
JConsole监控Linux上的Tomcat 从Java 5开始引入了 JConsole,来监控 Java 应用程序性能和跟踪 Java 中的代码.jconsole是JDK自带监控工具,只需要找到 ...
- 如何系统的学习Java
初学者记住一点,学习Java一定是连续性的且循序渐进的“系统化”学习,首先我给你提供一个优秀Java工程师的学习路线. web前端方面:html.css,Java.jQuery.xml解析.Boots ...
- c++多态及实现原理
https://www.cnblogs.com/cxq0017/p/6074247.html 通过虚函数实现,普通函数在编译过程中即确定了函数的地址,虚函数的函数地址是在程序运行的时候确定的,有虚函数 ...
- jquery中的 parseJSON() 源码分析
parseJSON: function( data ) { // Attempt to parse using the native JSON parser first if ( window.JSO ...
- dubbo 概述和使用
dubbo核心概念 apache是一款高性能.轻量级的开源java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现 官网:http://dubbo ...
- springBoot、SpringCloud 常用注解
1,@SpringBootApplication是springboot启动类的入口注解,标注在主启动类上:2,@EnableEurekaServer 是eureka服务端启动,接受其他服务注册进来,标 ...
- c语言:第二次作业,循环结构
1.本章学习总结(2分) 1.1 思维导图 1.2 本章学习体会及代码量学习体会 1.2.1 学习体会 循环相比分支和顺序结构难了许多,相对的来说我的c语言的基础比之前有提高,但是还是很多题想了很久也 ...