解决问题:

有一个固定长度的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 离散序列 样本数伸缩(原创)的更多相关文章

  1. 第二天:python的函 数、循环和条件、类

    https://uqer.io/community/share/54c8af17f9f06c276f651a54 第一天学习了Python的基本操作,以及几种主要的容器类型,今天学习python的函数 ...

  2. 关于VisualStudio性能分析数据中的独占样本数和非独占样本数的意义

    VisualStudio中自带有Profile工具进行性能性能分析,其中用得比较多的数据是函数调用时间,它主要有独占样本数和非独占样本数两个指标,关于这两个指标代表的意义,MSDN的解释比较文艺: 非 ...

  3. Python学习day15-函数进阶(3)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. Python学习day14-函数进阶(2)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. Python学习day13-函数进阶(1)

    Python学习day13-函数进阶(1) 闭包函数 闭包函数,从名字理解,闭即是关闭,也就是说把一个函数整个包起来.正规点说就是指函数内部的函数对外部作用域而非全局作用域的引用. 为函数传参的方式有 ...

  6. Python学习day12-函数基础(2)

    <!doctype html>day12博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { pos ...

  7. Python学习day11-函数基础(1)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  8. python 特定份数的数据概率统计(原创)

    使用numpy模块中的histogram函数模块 Histogram(a,bins=10,range=None,normed=False,weights=None)其中, a是保存待统计数据的数组, ...

  9. Python 文件行数读取的三种方法

    Python三种文件行数读取的方法: #文件比较小 count = len(open(r"d:\lines_test.txt",'rU').readlines()) print c ...

随机推荐

  1. jdango 使用oss存储

    安装django-aliyun-oss2-storage-0.1.5.tar.gz settings文件添加 MEDIA_ROOT = os.path.join(BASE_DIR,'upload/') ...

  2. 你不知道的JavaScript中,读书笔记

    七种内置类型 null, undefined, boolean, number, string, object, symbol typeof null === 'object' // true nul ...

  3. 【JAVA】杨辉三角

    ソース public Yanghui3jiao() { List<String[]> rowList = new ArrayList<String[]>(); List< ...

  4. [leetcode]364. Nested List Weight Sum II嵌套列表加权和II

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  5. webapi 重复提交问题

    https://izen.live/Blog/info/13.html /// <summary> /// action方法过滤器 /// </summary> public ...

  6. linux就该这么学,第六天了

    今天学了第六天了,主要讲计划任务了,,at,命令,单次有效,一次性的,crontd服务(周期性)计划任务,crontab -e创建,编辑计划任务.crontab -l查看计划任务,crontaab - ...

  7. [SoapUI] 从测试套件,测试用例,测试步骤,测试数据各个级别控制是否执行

    自动保存 # -*- coding: utf-8 -*- import java.awt.Color import org.apache.poi.ss.usermodel.Cell import or ...

  8. 1.5eigen中高级初始化

    1.5 高级初始化 这一节讨论一些初始化矩阵的高级方法. 1.The comma initializer eigen提供一个简单设定所有矩阵系数.向量.数组系数的初始化语法,从左上角开始到右下角结束. ...

  9. http错误代码提示

    1×× 保留 2×× 表示请求成功地接收 3×× 为完成请求客户需进一步细化请求 4×× 客户错误 5×× 服务器错误 200  正常:请求已完成.  201  正常:紧接 POST 命令.  202 ...

  10. windows server防火墙添加例外的步骤

      Windows Server 2012 防火墙如何添加端口例外的方法 在Windows Server 2012系统中,如果用户想在防火墙中开通一个端口,您可以按以下步骤执行: 1. 首先点击桌面左 ...