numpy均匀分布_Numpy的基本操作
生活随笔
收集整理的这篇文章主要介绍了
numpy均匀分布_Numpy的基本操作
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
一、创建数组
1、0和1的数组
- empty(shape[, dtype, order])
- empty_like(a[, dtype, order, subok])
- eye(N[, M, k, dtype, order])
- identity(n[, dtype])
- ones(shape[, dtype, order])
- ones_like(a[, dtype, order, subok])
- zeros(shape[, dtype, order])
- zeros_like(a[, dtype, order, subok])
- full(shape, fill_value[, dtype, order])
- full_like(a, fill_value[, dtype, order, subok])
2、从现有的数据中创建
- array(object[, dtype, copy, order, subok, ndmin])
- asarray(a[, dtype, order])
- asanyarray(a[, dtype, order])
- ascontiguousarray(a[, dtype])
- asmatrix(data[, dtype])
- copy(a[, order])
2.1、关于array和asarray的不同
3、创建固定范围的数组
- np.linspace (start, stop, num, endpoint, retstep, dtype)
生成等间隔的序列
start 序列的起始值stop 序列的终止值,如果endpoint为true,该值包含于序列中num 要生成的等间隔样例数量,默认为50endpoint 序列中是否包含stop值,默认为tureretstep 如果为true,返回样例,以及连续数字之间的步长dtype 输出ndarray的数据类型# 生成等间隔的数组np.linspace(0, 100, 10)- 其它的还有numpy.arange(start,stop, step, dtype)numpy.logspace(start,stop, num, endpoint, base, dtype)
3、创建随机数组
- np.random模块均匀分布np.random.rand(10)np.random.uniform(0,100)np.random.randint(100)正态分布?给定均值/标准差/维度的正态分布np.random.normal(1.75, 0.2, (3,4))np.random.standard_normal(size=(3,4))
二、正态分布(理解)
1、什么是正态分布
正态分布是一种概率分布。正态分布是具有两个参数μ和σ的连续型随机变量的分布,第一参数μ是服从正态分布的随机变量的均值,第二个参数σ是此随机变量的方差,所以正态分布记作N(μ,σ )。
2、正态分布的应用
生活、生产与科学实验中很多随机变量的概率分布都可以近似地用正态分布来描述。
3、正态分布特点
μ决定了其位置,其标准差σ。决定了分布的幅度。当μ = 0,σ = 1时的正态分布是标准正态分布。
标准差如何来?
3.1方差
是在概率论和统计方差衡量一组数据时离散程度的度量
其中M为平均值,n为数据总个数,S为标准差,S^2可以理解一个整体为方差
标准差与方差的意义
可以理解成数据的一个离散程度的衡量
- 有了方差为什么需要标准差去衡量?
例如:我们可以模拟生成一组股票的涨跌幅的数据
三、案例:随机生成500个股票两年的交易日涨幅数据
500只股票,两年(504天)的涨跌幅数据,如何获取?
- 两年的交易日数量为:2 X 252 = 504
- 随机生成涨跌幅在某个正态分布内,比如均值0,方差1
1、股票涨跌幅数据的创建
# 创建一个符合正太分布的500个股票504天的涨跌幅数据stock_day_rise = np.random.normal(0, 1, (500, 504))stock_day_rise.shape2、数组的索引
- 获取第一个股票的前100个交易日的涨跌幅数据
一维、二维、三维的数组如何索引?
# 三维,一维a1 = np.array([ [[1,2,3],[4,5,6]], [[12,3,34],[5,6,7]]])a1[0, 0, 1]3、数组形状与类型变化
3.1修改形状
让刚才的股票行、日期列反过来,变成日期行,股票列
- ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape.
- ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place.
- ndarray.flatten([order]) Return a copy of the array collapsed into one dimension.
3.2修改类型
- ndarray.astype(type)
3.3修改小数位数
- ndarray.round(arr, out) Return a with each element rounded to the given number of decimals.
4、数组转换
- ndarray.T 数组的转置将数组的行、列进行互换
- ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array.转换成bytes
如果遇到:
IOPub data rate exceeded. The notebook server will temporarily stop sending output to the client in order to avoid crashing it. To change this limit, set the config variable `--NotebookApp.iopub_data_rate_limit`.这个问题是在jupyer当中对输出的字节数有限制,需要去修改配置文件
创建配置文件
jupyter notebook --generate-configvi ~/.jupyter/jupyter_notebook_config.py取消注释,多增加
## (bytes/sec) Maximum rate at which messages can be sent on iopub before they# are limited.c.NotebookApp.iopub_data_rate_limit = 10000000但是不建议这样去修改,jupyter输出太大会崩溃
- ndarray.copy([order]) Return a copy of the array.
当我们不想修改某个股票数据的时候,就可以去进行拷贝操作。在拷贝的数据上进行操作
总结
以上是生活随笔为你收集整理的numpy均匀分布_Numpy的基本操作的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 回溯法解数独游戏
- 下一篇: SAP BADI增强点初学分享