当前位置:   article > 正文

NumPy基础(2)--统计分析函数_如何numpy函数进行基本的统计分析

如何numpy函数进行基本的统计分析

文件读写

一般数据都是以文件的形式进行存储的,对文件的读写是较为基本的操作。

写文件(savetxt)

import numpy as np
i2=np.eye(2)# 生成单位数组
print(i2)
np.savetxt('eye.txt',i2) # 指定保存的文件名和数据
  • 1
  • 2
  • 3
  • 4

读文件(loadtxt)

c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True)#csv文件以逗号作分隔符,usecols为一个元组,指的是取6到7列,unpack为Ture时,拆分结果存放到两个变量中
  • 1

简单统计分析

以下面的data.csv中数据为例,第一列为股票代码以标识股票(苹果公司股票代码为
AAPL),第二列为dd-mm-yyyy格式的日期,第三列为空,随后各列依次是开盘价、最高价、最低价和收盘价,最后一列为当日的成交量。

AAPL,28-01-2011, ,344.17,344.4,333.53,336.1,21144800
AAPL,31-01-2011, ,335.8,340.04,334.3,339.32,13473000
AAPL,01-02-2011, ,341.3,345.65,340.98,345.03,15236800
AAPL,02-02-2011, ,344.45,345.25,343.55,344.32,9242600
AAPL,03-02-2011, ,343.8,344.24,338.55,343.44,14064100
AAPL,04-02-2011, ,343.61,346.7,343.51,346.5,11494200
AAPL,07-02-2011, ,347.89,353.25,347.64,351.88,17322100
AAPL,08-02-2011, ,353.68,355.52,352.15,355.2,13608500
AAPL,09-02-2011, ,355.19,359,354.87,358.16,17240800
AAPL,10-02-2011, ,357.39,360,348,354.54,33162400
AAPL,11-02-2011, ,354.75,357.8,353.54,356.85,13127500
AAPL,14-02-2011, ,356.79,359.48,356.71,359.18,11086200
AAPL,15-02-2011, ,359.19,359.97,357.55,359.9,10149000
AAPL,16-02-2011, ,360.8,364.9,360.5,363.13,17184100
AAPL,17-02-2011, ,357.1,360.27,356.52,358.3,18949000
AAPL,18-02-2011, ,358.21,359.5,349.52,350.56,29144500
AAPL,22-02-2011, ,342.05,345.4,337.72,338.61,31162200
AAPL,23-02-2011, ,338.77,344.64,338.61,342.62,23994700
AAPL,24-02-2011, ,344.02,345.15,338.37,342.88,17853500
AAPL,25-02-2011, ,345.29,348.43,344.8,348.16,13572000
AAPL,28-02-2011, ,351.21,355.05,351.12,353.21,14395400
AAPL,01-03-2011, ,355.47,355.72,347.68,349.31,16290300
AAPL,02-03-2011, ,349.96,354.35,348.4,352.12,21521000
AAPL,03-03-2011, ,357.2,359.79,355.92,359.56,17885200
AAPL,04-03-2011, ,360.07,360.29,357.75,360,16188000
AAPL,07-03-2011, ,361.11,361.67,351.31,355.36,19504300
AAPL,08-03-2011, ,354.91,357.4,352.25,355.76,12718000
AAPL,09-03-2011, ,354.69,354.76,350.6,352.47,16192700
AAPL,10-03-2011, ,349.69,349.77,344.9,346.67,18138800
AAPL,11-03-2011, ,345.4,352.32,345,351.99,16824200

平均值

  1. 算术平均函数
import numpy as np
c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True)
print(np.mean(c))
print(np.average(c))
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述
2. 加权平均数函数
average函数可以指定weights参数,指定权重。

import numpy as np
c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True)
print(np.mean(c))
#成交量加权平均价格(VWAP)
print(np.average(c, weights=v))
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

最大值和最小值

import numpy as np
c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True)
print(np.max(c)) #最大值
print(np.min(c)) #最小值
print(np.ptp(c)) #最大值与最小值之间的差值,也就是极差
  • 1
  • 2
  • 3
  • 4
  • 5

中位数

import numpy as np
c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True)
print(np.median(c)) #中位数
#通过排序获取中间值
sorted=np.msort(c)
n=len(c)
print((sorted[n//2]+sorted[(n-1)//2])/2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

方差

import numpy as np
c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True)
print(np.var(c))
  • 1
  • 2
  • 3

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/142276
推荐阅读
相关标签
  

闽ICP备14008679号