零基础学习数据分析Day04

编程入门 行业动态 更新时间:2024-10-16 22:14:58

零<a href=https://www.elefans.com/category/jswz/34/1770030.html style=基础学习数据分析Day04"/>

零基础学习数据分析Day04

Numpy的基本数据函数

1.蜡烛图

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing',alpha=0.8)
# 判断涨跌
rise_ = closing_pirce>opening_prices
# 填充色
color = ['white' if x else 'green' for x in rise_]
# 边缘色
ecolor = np.zeros(rise_.size,dtype='U5')
ecolor[:]='green'
ecolor[rise_] = 'red'# print(color)# 绘制k线
# 绘制实体
mp.bar(dates,closing_pirce-opening_prices,0.8,opening_prices,color=color,edgecolor=ecolor,zorder=3)
# 绘制影线
mp.vlines(dates,lowerst_prices,highest_prices,color=ecolor)mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

算术平均数

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing')# 求算术平均数
mean = np.mean(closing_pirce)
mp.hlines(mean,dates[0],dates[-1],color='pink',label='Mean(CP)')mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

加权平均数--成交量

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce,volumes =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6,7),dtype='M8[D],f8,f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing')# VWAP  成交量加权平均价格
vwap = np.average(closing_pirce,weights=volumes)
mp.hlines(vwap,dates[0],dates[-1],color='green',label='VWAP')mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

时间加权平均数

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing')
# TWAP 时间加权平均数
times = np.linspace(1,30,30)
twap = np.average(closing_pirce,weights=times)
mp.hlines(twap,dates[0],dates[-1],color='red',label='TWAP')mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

Numpy的基本函数

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing')
# 30天的交易的最高价格与最低价格
maxval = np.max(highest_prices)
minval = np.max(lowerst_prices)
print(minval,'~',maxval)#获取最高价与最低价出现的日期
max_ind = np.argmax(highest_prices)
min_ind = np.argmin(lowerst_prices)
print('max:',dates[max_ind])
print('min:',dates[min_ind])# maximun minimum
ary = np.arange(1,10).reshape(3,3)
bry = np.arange(1,10)[::-1].reshape(3,3)
print(ary)
print(bry)
print(np.maximum(ary,bry))
print(np.minimum(ary,bry))mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

中位数

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing')
# TWAP 时间加权平均数
times = np.linspace(1,30,30)
twap = np.average(closing_pirce,weights=times)
mp.hlines(twap,dates[0],dates[-1],color='red',label='TWAP')# median
median = np.median(closing_pirce)
# 手动计算
sorted_price = np.msort(closing_pirce)
size = sorted_price.size
median1 = (sorted_price[int((size-1)/2)]+sorted_price[int(size/2)])/2# 查看差值
print(median1)
print(median)mp.hlines(median,dates[0],dates[-1],color='black',label='median(CP)')mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

标准差及相关公式

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})# 标准叉
print(np.std(closing_pirce))                # 总体标准差
print(np.std(closing_pirce,ddof=1))         # 样本标准差

函数轴线汇总

import numpy as np
import matplotlib.pyplot as mp# 语  数 外
data = np.array([[87,89,56],[85,75,99],[78,68,84],])# 统计 每个人的三门成绩的平均分            # 77.33333333333333
for row in range(len(data)):          # 86.33333333333333print(data[row].mean())           # 76.66666666666667# 统计每一门成绩的最高分
for col in range(data.shape[1]):print(np.max(data[:,col]))        # 87 89 99def func(data):return np.max(data),np.min(data),np.mean(data)# numpy提供的轴线汇总的api                  # [ [89.         56.         77.33333333]
res = np.apply_along_axis(func,1,data)   # [99.         75.         86.33333333]
print(res)                               # [84.         68.         76.66666667]]

移动均线

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing')# 绘制5日移动平均线
ma5 = np.zeros(closing_pirce.size -4)
for i in range(ma5.size):ma5[i] = closing_pirce[i:i+5].mean()
mp.plot(dates[4:],ma5,color='orange',linewidth=3,linestyle='--',label='closing')mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

卷积实现5日均线,十日均线,5日加权均线

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing')# 卷积实现5日移动平均线
kernal = np.ones(5)/5
ma52 = np.convolve(closing_pirce,kernal,'valid')
mp.plot(dates[4:],ma52,color='orange',linewidth=7,alpha=0.5)# 卷积实现10日移动均线
kernal = np.ones(10)/10
ma10 = np.convolve(closing_pirce,kernal,'valid')
mp.plot(dates[9:],ma10,color='green',linewidth=3,alpha=0.5)# 加权5日均线
weights = np.exp(np.linspace(-1, 0, 5))
weights /= weights.sum()
sma5 = np.convolve(closing_pirce, weights[::-1], 'valid')
mp.plot(dates[4:], sma5, c='black', alpha=0.5,linewidth=2, label='SMA-5')mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

5日均线的布林带

import matplotlib.pyplot as mp
import numpy as np
import datetime as dt
import matplotlib.dates as md
# 日期转换函数
def dmy2ymd(dmy):dmy = str(dmy, encoding='utf-8')time = dt.datetime.strptime(dmy, '%d-%m-%Y').date()t = time.strftime('%Y-%m-%d')return tdates,opening_prices,highest_prices,lowerst_prices,closing_pirce =np.loadtxt('/home/tarena/aid1907/data/day03/aapl.csv',delimiter=',',usecols=(1,3,4,5,6),dtype='M8[D],f8,f8,f8,f8',unpack=True,converters={1:dmy2ymd})
# print(dates)
# 画图
mp.figure('AAPL',facecolor='lightgray')
mp.title('AAPL',fontsize=18)
mp.grid(linestyle=':')
mp.xlabel('Date',fontsize=14)
mp.ylabel('Closing Price',fontsize=14)
# 设置刻度定位器
ax = mp.gca()
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
# ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y/%m/%d'))dates = dates.astype(md.datetime.datetime)
mp.plot(dates,closing_pirce,color='pink',linewidth=2,linestyle='--',label='closing')# 5日布林带
weights = np.exp(np.linspace(-1, 0, 5))
weights /= weights.sum()
em5 = np.convolve(closing_pirce, weights[::-1], 'valid')
stds = np.zeros(em5.size)
for i in range(stds.size):stds[i] = closing_pirce[i:i + 5].std()
stds *= 2
lowers = em5 - stds
uppers = em5 + stdsmp.plot(dates, closing_pirce, c='lightgray', label='Closing Price')
mp.plot(dates[4:], em5, c='dodgerblue', label='Medio')
mp.plot(dates[4:], lowers, c='limegreen', label='Lower')
mp.plot(dates[4:], uppers, c='orangered', label='Upper')mp.legend()
mp.gcf().autofmt_xdate()   # 刻度为斜线
mp.show()

 

更多推荐

零基础学习数据分析Day04

本文发布于:2024-03-23 18:31:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1741412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:基础   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!