Python中的Numpy

來源:互聯網
上載者:User

標籤:最大值   bool   tor   one   UNC   image   篩選   期望   相對   

引用Numpy

import numpy as np

 

產生隨機資料

# 200支股票stock_cnt = 200# 504個交易日view_days = 504# 產生服從常態分佈:均值期望=0,標準差=1的序列stock_day_change = np.random.standard_normal((stock_cnt, view_days))# 使用沙箱資料,目的是和書中一樣的資料環境,不需要注視掉# stock_day_change = np.load(‘../gen/stock_day_change.npy‘)# 列印shape (200, 504) 200行504列print(stock_day_change.shape)# 列印出第一支只股票,頭五個交易日的漲跌幅情況print(stock_day_change[0:1, :5])


3.1.3 索引選取和切片選擇

# 0:2第一,第二支股票,0:5頭五個交易日的漲跌幅資料stock_day_change[0:2, 0:5]


3.1.4 資料轉換與規整

# 2代表保留兩位小數np.around(stock_day_change[0:2, 0:5], 2)


3.1.5 邏輯條件進行資料篩選

mask = stock_day_change[0:2, 0:5] > 0.5print(mask)


3.1.6 通用序列函數

# np.all判斷序列中的所有元素是否全部是true, 即對bool序列進行與操作# 本例實際判斷stock_day_change[0:2, 0:5]中是否全是上漲的np.all(stock_day_change[0:2, 0:5] > 0)
# np.any判斷序列中是否有元素為true, 即對bool序列進行或操作# 本例實際判斷stock_day_change[0:2, 0:5]中是至少有一個是上漲的np.any(stock_day_change[0:2, 0:5] > 0)
# 對兩個序列對應的元素兩兩比較,maximum結果集取大,相對使用minimum為取小的結果集np.maximum(stock_day_change[0:2, 0:5], stock_day_change[-2:, -5:])# array([[ 0.38035486,  0.12259674, -0.2851901 , -0.00889681,  0.45731945],       # [ 0.13380956,  2.03488293,  1.44701057, -0.92392477,  0.96930104]])
change_int = stock_day_change[0:2, 0:5].astype(int)print(change_int)# 序列中數值值唯一且不重複的值組成新的序列np.unique(change_int)
# diff 前後臨近資料進行減法運算# axis=1np.diff(stock_day_change[0:2, 0:5])
# 唯一區別 axis=0np.diff(stock_day_change[0:2, 0:5], axis=0)
#where 資料篩選tmp_test = stock_day_change[-2:, -5:]print(np.where(tmp_test > 0.5, 1, 0))



統計概念與函數使用

stock_day_change_four = stock_day_change[:4, :4]print(‘最大漲幅 {}‘.format(np.max(stock_day_change_four, axis=1)))print(‘最大跌幅 {}‘.format(np.min(stock_day_change_four, axis=1)))print(‘振幅幅度 {}‘.format(np.std(stock_day_change_four, axis=1)))print(‘平均漲跌 {}‘.format(np.mean(stock_day_change_four, axis=1)))



3.2.2 統計基礎概念

a_investor = np.random.normal(loc=100, scale=50, size=(100, 1))b_investor = np.random.normal(loc=100, scale=20, size=(100, 1))# a交易者print(‘交易者期望{0:.2f}元, 標準差{1:.2f}, 方差{2:.2f}‘.format(a_investor.mean(), a_investor.std(), a_investor.var()))# b交易者print(‘交易者期望{0:.2f}元, 標準差{1:.2f}, 方差{2:.2f}‘.format(b_investor.mean(), b_investor.std(), b_investor.var()))



常態分佈

import scipy.stats as scs# 均值期望stock_mean = stock_day_change[0].mean()# 標準差stock_std = stock_day_change[0].std()print(‘股票0 mean均值期望:{:.3f}‘.format(stock_mean))print(‘股票0 std振幅標準差:{:.3f}‘.format(stock_std))# 繪製股票0的長條圖plt.hist(stock_day_change[0], bins=50, normed=True)# linspace從股票0 最小值-> 最大值產生資料fit_linspace = np.linspace(stock_day_change[0].min(),                           stock_day_change[0].max())# 機率密度函數(PDF,probability density function)# 由均值,方差,來描述曲線,使用scipy.stats.norm.pdf產生擬合曲線pdf = scs.norm(stock_mean, stock_std).pdf(fit_linspace)# plot x, yplt.plot(fit_linspace, pdf, lw=2, c=‘r‘)

# 100個賭徒進場開始,勝率0.45,賠率1.04,手續約0.01moneys = [casino(0.45, commission=0.01, win_once=1.02, loss_once=0.98)          for _ in np.arange(0, gamblers)]_ = plt.hist(moneys, bins=30)

 

 


伯努利分布

Python中的Numpy

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.