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 ...
随机推荐
- Block学习总结
最近网上浏览了一些关于Block的文章,自己进行一下消化吸收. Void (^blockName)(parma)-> Block声明 Void (^) (parma){}; ->Bloc ...
- FortiGate外网IPSec链路及运维专线链路到个别网段不通
1.现状: 如图,用户网段有192.168.50.0/24.192.168.51.0/24和192.168.52.0/24.192.168.53.0/24.在防火墙上有静态路由到运维专线的10.160 ...
- ABP框架提示框
abp.message.info('some info message', 'some optional title');abp.message.success('some success messa ...
- dfs | Security Badges
Description You are in charge of the security for a large building, with n rooms and m doors between ...
- huawei FPGA方案
https://indico.cern.ch/event/669648/contributions/2838181/attachments/1581893/2500031/Huawei_Cloud_F ...
- linux命令大全(1)
当用户使用linux系统时,其实在和Shell在打交道,当用户发出指令,其实先将这些指令发送给Shell, 然后由Shell将用户的指令翻译后传送给内核,再由内核来控制硬件的工作. 然后内核将硬件的工 ...
- NCUAP 利用java自带方法实现导入excel取数据
final JComponent parent = getModel().getContext().getEntranceUI(); JFileChooser chooser = new JFileC ...
- spring mvc mybatis shiro构建cms系统ios android
开发语言: java.ios.android 部署平台: linux.window jdk版本:JDK1.7以上版本 开发工具: eclipse.idea等 服务器中间件:Tomcat 6.7.Jbo ...
- python04 列表 元祖 字典
1.list 有序,元素可以被修改 li=[1,2,2,"am","123"] 列表中的元素可以是数字,字符串,列表等等 支持切片,切片结果为列表 li[3] ...
- kylin 系列(一)安装部署
kylin 系列(一)安装部署 一.环境说明 1.1 版本选择 cdh 版本下载地址:http://archive.cloudera.com/cdh5/cdh/5/ 软件名称 版本 JDK 1.8 H ...