# -*- coding: utf-8 -*-
"""
Created on Mon Dec 2 14:49:59 2018 @author: zhen
""" import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from datetime import datetime def normal(a): #最大值最小值归一化
return (a - np.min(a)) / (np.max(a) - np.min(a)+0.000001) def normalization(x): # np.std:计算矩阵的标准差(方差的算术平方根)
return (x - np.mean(x)) / np.std(x) def corrcoef(a,b):
corrc = np.corrcoef(a,b) # 计算皮尔逊相关系数,用于度量两个变量之间的相关性,其值介于-1到1之间
corrc = corrc[0,1]
return (16 * ((1 - corrc) / (1 + corrc)) ** 1) # ** 表示乘方 startTimeStamp = datetime.now() # 获取当前时间
# 加载数据
filename = 'C:/Users/zhen/.spyder-py3/sh000300_2017.csv'
# 获取第一,二列的数据
all_date = pd.read_csv(filename,usecols=[0, 1, 3], dtype = 'str')
all_date = np.array(all_date)
data = all_date[:, 0]
times = all_date[:, 1] data_points = pd.read_csv(filename,usecols=[3])
data_points = np.array(data_points)
data_points = data_points[:,0] #数据 topk = 10 #只显示top-10
baselen = 100
basebegin = 361
basedata = data[basebegin]+' '+times[basebegin]+'~'+data[basebegin+baselen-1]+' '+times[basebegin+baselen-1]
base = data_points[basebegin:basebegin+baselen]#一天的数据是240个点
length = len(data_points) #数据长度 # 分割片段
subseries = []
dateseries = []
for j in range(0,length):
if (j < (basebegin - baselen) or j > (basebegin + baselen - 1)) and j <length - baselen:
subseries.append(data_points[j:j+baselen])
dateseries.append(j) #开始位置 # 片段搜索
listdistance = []
for i in range(0, len(subseries)):
tt = np.array(subseries[i])
distance = corrcoef(base, tt)
listdistance.append(distance) # 排序
index = np.argsort(listdistance,kind='quicksort') #排序,返回排序后的索引序列 # 显示,要匹配的数据
plt.figure(0)
plt.plot((base),label = basedata, linewidth='')
plt.legend(loc='upper left')
plt.title('Base data') # 原始数据
plt.figure(1)
num = index[0]
length = len(subseries[num])
begin = data[dateseries[num]]+' '+times[dateseries[num]]
end = data[dateseries[num]+length-1]+' '+times[dateseries[num]+length-1]
label = begin+'~'+end
plt.plot((subseries[num]), label=label, linewidth='')
plt.legend(loc='upper left')
plt.title('Similarity data') # 结果集对比
plt.figure(2)
plt.plot(normalization(base),label= basedata,linewidth='')
length = len(subseries[num])
begin = data[dateseries[num]] + ' ' + times[dateseries[num]]
end = data[dateseries[num] + length - 1] + ' ' + times[dateseries[num] + length - 1]
label = begin + '~' + end
plt.plot(normalization(subseries[num]), label=label, linewidth='')
plt.legend(loc='lower right')
plt.title('normal similarity search')
plt.show() endTimeStamp=datetime.now()
print('run time', (endTimeStamp-startTimeStamp).seconds, "s")

结果:

最新文章

  1. Linux下使用Hexo搭建github博客
  2. CLion 2016.1.1 下载 附注册激活码 破解版方法
  3. OC NSString 基本操作(用到补充持续更新)
  4. CSS的clip-path
  5. PHP第三方登录 -- 微博登录
  6. 利用Formdata实现form提交文件上传不跳转页面
  7. 【地图API】收货地址详解2
  8. POJ 1032问题描述
  9. WinForm 实现验证码
  10. 在Linux下写一个简单的驱动程序
  11. SQL中的事物【转】
  12. Google的IP地址一览表,加上代理服务器
  13. hdu 5640 King&#39;s Cake(模拟)
  14. SQL Server 中可以被锁住的 12 种资源
  15. linux修改系统时间date命令加clock -w
  16. 对“demo!demo.Index+HookProc::Invoke”垃圾收集的类型已委托回调。这可能会导致应用程序崩溃、损坏和数据丢失。当传递委托给非托管代码,托管应用程序必须让这些委托保持活着
  17. Java并发基础——Thread
  18. 使用nio对磁盘下的文件进行过滤
  19. 关于vue如何解决数据渲染完成之前,dom树显示问题
  20. mser 最大稳定极值区域(文字区域定位)算法 附完整C代码

热门文章

  1. mesos支持gpu代码分析以及capos支持gpu实现
  2. 关于 Spring Security OAuth2 中 Feign 调用 Token 问题
  3. java.sql.SQLException: The SQL statement must not be null or empty.这个错误
  4. linux 命令 — 文件相关
  5. DateTimeHelper
  6. docker 构建 https 私有仓库 Registry
  7. solr调用lucene底层实现倒排索引源码解析
  8. Mysql的跨表更新
  9. 使用Pyinstaller转换.py文件为.exe可执行程序
  10. JavaScript 二进制转文件