Python中的Numpy
引用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.5 print(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=1 np.diff(stock_day_change[0:2, 0:5])
# 唯一区别 axis=0 np.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, y plt.plot(fit_linspace, pdf, lw=2, c='r')
# 100个赌徒进场开始,胜率0.45,赔率1.04,手续费0.01 moneys = [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的更多相关文章
- Python中的Numpy、SciPy、MatPlotLib安装与配置
Python安装完Numpy,SciPy和MatplotLib后,可以成为非常犀利的科研利器.网上关于这三个库的安装都写得非常不错,但是大部分人遇到的问题并不是如何安装,而是安装好后因为配置不当,在使 ...
- Python中的numpy函数的使用ones,zeros,eye
在看别人写的代码时,看到的不知道的函数,就在这里记下来. 原文是这样用的: weights = ones((numfeatures,1)) 在python中help(): import numpy a ...
- Python中的Numpy入门教程
1.Numpy是什么 很简单,Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy.matplotlib一起使用.其实,list已经提供了类似于矩阵的表示形式,不过nu ...
- Python中的numpy模块解析
numpy 1. 创建对象 维度(dimensions):轴 轴的个数:秩(rank) Numpy最重要的一个特点就是其N维数组对象(即ndarray) 创建数组最简单的函数就是用array函数: ...
- Python中的Numpy包
通过本次学习你可以掌握Numpy Numpy介绍(获取地址)更多Numpy函数 numpy的主要对象是同质多维数组.也就是在一个元素(通常是数字)表中,元素的类型都是相同的. numpy的数组类被成为 ...
- Python中安装numpy,scipy,matplotlib安装方法
这个吧,说简单也简单,说难吧我捣鼓了两天才弄出来,真是头发都急白了.其实只要一个网址就搞定了,嘿嘿 http://www.lfd.uci.edu 这里面有你需要的任何东西,当你运行python imp ...
- python 中range numpy.arange 和 numpy.linspace 的区别
1.返回值不同 range返回一个range对象,numpy.arange和numpy.linspace返回一个数组. 2.np.arange的步长可以为小数,但range的步长只能是整数. 与Pyt ...
- Python中的numpy库介绍!
转自:https://blog.csdn.net/codedz/article/details/82869370 机器学习算法中大部分都是调用Numpy库来完成基础数值计算的.安装方法: pip3 i ...
- Python中 list, numpy.array, torch.Tensor 格式相互转化
1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...
随机推荐
- 常见编码和编码头BOM
ANSI(American National Standards Institute,美国国家标准学会)ANSI编码标准是指所有从基本ASCII码基础上发展起来的编码标准,比如扩展的ASCII码(12 ...
- [shell]C语言调用shell脚本接口
Use popen if you want to run a shell command and want the parent process to be able to talk to the c ...
- IOS设备信息与机型对照表
http://blog.csdn.net/olsQ93038o99S/article/details/78374343 参考别人的文章吧....
- Linux下protobuf的编译与安装
1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 将下载 ...
- 常见的装包的三种宝,包 bao-devel bao-utils bao-agent ,包 开发包 工具包 客户端
常见的装包的三种宝,包 bao-devel bao-utils bao-agent ,包 开发包 工具包 客户端
- c libghttp ghttp 库使用指南
libghttp是一个很好用的 http 库,能够轻松地实现同步和异步的HTTP请求 目录 [隐藏] 1 安装 2 GET示例 3 POST示例 4 相关函数 5 艺搜参考 安装 库文件下载: 在6 ...
- pip和 easy_insall的区别
作为Python爱好者,如果不知道easy_install或者pip中的任何一个的话,那么...... easy_insall的作用和perl中的cpan,ruby中的gem类似,都提供了在线一键 ...
- 用 free 或 delete 释放了内存之后,立即将指针设置为 NULL,防止产 生“野指针”
用 free 或 delete 释放了内存之后,立即将指针设置为 NULL,防止产 生“野指针”. #include <iostream> using namespace std; /* ...
- CPictureEx类
CPictueEx类不仅可以显示GIF(包括GIF动画),还可以显示JPEG.BMP.WMF.ICO.CUR等. 参考:https://www.codeproject.com/Articles/142 ...
- 【C++缺省函数】 空类默认产生的6个类成员函数
1.缺省构造函数. 2.缺省拷贝构造函数. 3. 缺省析构函数. 4.缺省赋值运算符. 5.缺省取址运算符. 6. 缺省取址运算符 const. <span style="font-s ...