numpy均匀分布_Numpy的基本操作
一、創(chuàng)建數(shù)組
1、0和1的數(shù)組
- 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、從現(xiàn)有的數(shù)據(jù)中創(chuàng)建
- 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、關(guān)于array和asarray的不同
?
3、創(chuàng)建固定范圍的數(shù)組
- np.linspace (start, stop, num, endpoint, retstep, dtype)
生成等間隔的序列
start 序列的起始值stop 序列的終止值,如果endpoint為true,該值包含于序列中num 要生成的等間隔樣例數(shù)量,默認(rèn)為50endpoint 序列中是否包含stop值,默認(rèn)為tureretstep 如果為true,返回樣例,以及連續(xù)數(shù)字之間的步長dtype 輸出ndarray的數(shù)據(jù)類型# 生成等間隔的數(shù)組np.linspace(0, 100, 10)- 其它的還有numpy.arange(start,stop, step, dtype)numpy.logspace(start,stop, num, endpoint, base, dtype)
3、創(chuàng)建隨機(jī)數(shù)組
- np.random模塊均勻分布np.random.rand(10)np.random.uniform(0,100)np.random.randint(100)正態(tài)分布?給定均值/標(biāo)準(zhǔn)差/維度的正態(tài)分布np.random.normal(1.75, 0.2, (3,4))np.random.standard_normal(size=(3,4))
二、正態(tài)分布(理解)
1、什么是正態(tài)分布
正態(tài)分布是一種概率分布。正態(tài)分布是具有兩個(gè)參數(shù)μ和σ的連續(xù)型隨機(jī)變量的分布,第一參數(shù)μ是服從正態(tài)分布的隨機(jī)變量的均值,第二個(gè)參數(shù)σ是此隨機(jī)變量的方差,所以正態(tài)分布記作N(μ,σ )。
?
2、正態(tài)分布的應(yīng)用
生活、生產(chǎn)與科學(xué)實(shí)驗(yàn)中很多隨機(jī)變量的概率分布都可以近似地用正態(tài)分布來描述。
3、正態(tài)分布特點(diǎn)
μ決定了其位置,其標(biāo)準(zhǔn)差σ。決定了分布的幅度。當(dāng)μ = 0,σ = 1時(shí)的正態(tài)分布是標(biāo)準(zhǔn)正態(tài)分布。
?標(biāo)準(zhǔn)差如何來?
3.1方差
是在概率論和統(tǒng)計(jì)方差衡量一組數(shù)據(jù)時(shí)離散程度的度量
?其中M為平均值,n為數(shù)據(jù)總個(gè)數(shù),S為標(biāo)準(zhǔn)差,S^2可以理解一個(gè)整體為方差
?
標(biāo)準(zhǔn)差與方差的意義
可以理解成數(shù)據(jù)的一個(gè)離散程度的衡量
?
- 有了方差為什么需要標(biāo)準(zhǔn)差去衡量?
例如:我們可以模擬生成一組股票的漲跌幅的數(shù)據(jù)
三、案例:隨機(jī)生成500個(gè)股票兩年的交易日漲幅數(shù)據(jù)
500只股票,兩年(504天)的漲跌幅數(shù)據(jù),如何獲取?
- 兩年的交易日數(shù)量為:2 X 252 = 504
- 隨機(jī)生成漲跌幅在某個(gè)正態(tài)分布內(nèi),比如均值0,方差1
1、股票漲跌幅數(shù)據(jù)的創(chuàng)建
# 創(chuàng)建一個(gè)符合正太分布的500個(gè)股票504天的漲跌幅數(shù)據(jù)stock_day_rise = np.random.normal(0, 1, (500, 504))stock_day_rise.shape2、數(shù)組的索引
- 獲取第一個(gè)股票的前100個(gè)交易日的漲跌幅數(shù)據(jù)
一維、二維、三維的數(shù)組如何索引?
# 三維,一維a1 = np.array([ [[1,2,3],[4,5,6]], [[12,3,34],[5,6,7]]])a1[0, 0, 1]3、數(shù)組形狀與類型變化
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修改小數(shù)位數(shù)
- ndarray.round(arr, out) Return a with each element rounded to the given number of decimals.
4、數(shù)組轉(zhuǎn)換
- ndarray.T 數(shù)組的轉(zhuǎn)置將數(shù)組的行、列進(jìn)行互換
- ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array.轉(zhuǎn)換成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`.這個(gè)問題是在jupyer當(dāng)中對(duì)輸出的字節(jié)數(shù)有限制,需要去修改配置文件
創(chuàng)建配置文件
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輸出太大會(huì)崩潰
- ndarray.copy([order]) Return a copy of the array.
當(dāng)我們不想修改某個(gè)股票數(shù)據(jù)的時(shí)候,就可以去進(jìn)行拷貝操作。在拷貝的數(shù)據(jù)上進(jìn)行操作
總結(jié)
以上是生活随笔為你收集整理的numpy均匀分布_Numpy的基本操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 回溯法解数独游戏
- 下一篇: SAP BADI增强点初学分享