python 离散序列 样本数伸缩(原创)
解决问题:
有一个固定长度的1维矩阵,将这个矩阵的取样点进行扩充和减少
功能函数:
def discrete_scale(data, num):
import numpy as np
import copy
"""
:param data: 原始一维矩阵数据
:param num: 设定的样本长度
:return d1: 目标矩阵输出
"""
len = data.shape[0] # 原始数据长度 if len < num: # 样本扩展
t = (len - 1) / (num - 1) # 映射差值
d0 = np.array(range(num)) # 序列映射
d0 = d0 * t d0_1 = copy.deepcopy(d0).astype(int) # 整数部分
d0_0 = d0 - d0_1 # 小数部分
dist = data[1:] - data[:-1] # 维度减小一个数据
d1_1 = data[d0_1]
d1_0 = dist[d0_1[:-1]]
d1_0 = d1_0 * d0_0[:-1]
d1 = copy.deepcopy(d1_1[:-1] + d1_0)
d1 = np.hstack((d1, data[-1])) elif len > num: # 样本压缩
t = (len - 1) / num # 映射差值 分成7个给值区域
d0 = np.array(range(num + 1)) # 序列映射
d0 = d0 * t d0_1 = copy.deepcopy(d0).astype(int) # 整数部分
list = []
for i in range(d0_1.shape[0] - 1):
list.append(np.mean(data[d0_1[i]:d0_1[i + 1] + 1]))
d1 = np.array(list) else: # 目标长度与原始长度相同
d1 = data
return d1
实例程序:
import numpy as np
a = np.array(range(0,1000))
print(a)
b = np.sin(a/100)
print(b) num = 100
x1 = np.array(range(num))
y1 = discrete_scale(b, num) import matplotlib.pylab as plt
plt.plot(x1, y1, 'r-')
plt.plot(a, b, 'b-')
plt.show()
print(b)

python 离散序列 样本数伸缩(原创)的更多相关文章
- 第二天:python的函 数、循环和条件、类
https://uqer.io/community/share/54c8af17f9f06c276f651a54 第一天学习了Python的基本操作,以及几种主要的容器类型,今天学习python的函数 ...
- 关于VisualStudio性能分析数据中的独占样本数和非独占样本数的意义
VisualStudio中自带有Profile工具进行性能性能分析,其中用得比较多的数据是函数调用时间,它主要有独占样本数和非独占样本数两个指标,关于这两个指标代表的意义,MSDN的解释比较文艺: 非 ...
- Python学习day15-函数进阶(3)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day14-函数进阶(2)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day13-函数进阶(1)
Python学习day13-函数进阶(1) 闭包函数 闭包函数,从名字理解,闭即是关闭,也就是说把一个函数整个包起来.正规点说就是指函数内部的函数对外部作用域而非全局作用域的引用. 为函数传参的方式有 ...
- Python学习day12-函数基础(2)
<!doctype html>day12博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { pos ...
- Python学习day11-函数基础(1)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- python 特定份数的数据概率统计(原创)
使用numpy模块中的histogram函数模块 Histogram(a,bins=10,range=None,normed=False,weights=None)其中, a是保存待统计数据的数组, ...
- Python 文件行数读取的三种方法
Python三种文件行数读取的方法: #文件比较小 count = len(open(r"d:\lines_test.txt",'rU').readlines()) print c ...
随机推荐
- Linux mail 查看
Linux 下查看mail的命令参数: 一般系统收到邮件都会保存在“/var/spool/mail/[linux username]"文件中,在Linux中输入mail,就进入了收件箱,并显 ...
- Python之ftp服务器
今天把做的ftp服务器过程总结一下,先看看要求 一.需求 1. 用户加密认证 2. 允许同时多用户登录 3. 每个用户有自己的家目录 ,且只能访问自己的家目录 4. 对用户进行磁盘配额,每个用户的可用 ...
- mysql 在update中实现子查询的方式
当使用mysql条件更新时--最先让人想到的写法 UPDATE buyer SET is_seller=1 WHERE uid IN (SELECT uid FROM seller) 此语句是错误的, ...
- 如何解决XMLHttpRequest cannot load file:~~~~~~~~~~~. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-res
原因:Chrome不支持本地Ajax请求. 解决: 右击Chrome浏览器快捷方式,选择“属性”,在“目标”中加上"--allow-file-access-from-files", ...
- 第41章:MongoDB-集群--Sharding(分片)
①Sharding分片概念 分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程.将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载. 分片是每个分 ...
- Cannot set property 'onclick' of null报错
经常几个页面使用公共js文件, 原来遇到也没留意, 原来是本页面执行的时候, 其他页面也在执行并赋予id于onclick. 因为页面是正常情况下是不存在null和undefined if(null){ ...
- 【repost】前端学习总结(二十三)——前端框架天下三分:Angular React 和 Vue的比较
目录(?)[+] 前端这几年的技术发展很快,细分下来,主要可以分成四个方面: 1.开发语言技术,主要是ES6&7,coffeescript,typescript等: 2.开发框架,如Ang ...
- 【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import
Pycharm 2016.2执行单元测试遇到如下问题: RuntimeWarning: Parent module ‘YOUR_MODULE_HERE’ not found while handlin ...
- 【python-HTMLTestRunner】生成HTMLTestRunner报告报错ERROR 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
[python-HTMLTestRunner]生成HTMLTestRunner报告报错:ERROR 'ascii' codec can't decode byte 0xe5 in position 0 ...
- js-function作用域
你能猜出先弹出什么吗? <!DOCTYPE html> <html lang="en"><head> <meta charset=&quo ...