numpy 基于现有数据创建ndarray(from existing data)
1 numpy.array
array(object[, dtype=None, copy=True, order='K', subok=False, ndmin=0])
2 numpy.asarray
asarray(a[, dtype=None, order=None])
将(列表、元组及其嵌套结构)数据a转换成ndarray
返回ndarray数据
1 当a为元组、列表array时
返回值值与输入值不相同
import numpy as np a = [1,2] b = np.asarray(a) print(a) #[1, 2] print(b) #[1 2]
2 当a为ndarray且不做任何改变时
原数据a与返回值为同一个地址
import numpy as np a = np.array([1,2]) b = np.asarray(a) a += 2 print(a) #[3 4] print(b) #[3 4] print(a is b) #True
3 当a为ndarray且改变数据结构时
复制一份数据
import numpy as np a = np.array([1,2],dtype=np.float32) b = np.asarray(a,dtype='i4') a += 2 print(a) #[3. 4.] print(b) #[1 2] print(a is b) #False
总之,可理解为不改变元数据时,为同一个数据地址,当改变元数据时,返回值会复制元数据并进行asarray格式化。
3 numpy.frombuffer
frombuffer(buffer[, dtype=float, count=-1, offset=0])
将缓冲区解释为一维数组。
暴露缓冲区接口的任何对象都用作参数来返回ndarray。
buffer - 一个暴露在缓冲取接口的对象
dtype - 返回ndarray的数据类型,默认float
count - 读取数据数量,默认-1,也即读取所有数据 int数据类型
offset - 读取数据的起始位置(偏移量)
返回值ndarray
import numpy as np s = 'hello world' a = np.frombuffer(s,dtype='S1',count=5,offset=6) print(a) # 报错:AttributeError: 'str' object has no attribute '__buffer__'
一直没有弄明白,一直报错,要不就是 itemsize cannot be zero in type
请赐教,谢谢!
4 numpy.frombuffer
fromiter(iterable, dtype[, count=-1])
将一个可迭代对象构建成一个ndarray对象,返回一个新的一维数组
count - 读取数据的量,当为默认值-1时,意味读取所有数据。
import numpy as np iterable = (x*x for x in range(5)) a = np.fromiter(iterable,float) print(a) #[ 0. 1. 4. 9. 16.]
参考:NumPy - 来自数值范围的数组,和官方文档(numpy-ref-14.5)3.1.2 From existing data
numpy 基于现有数据创建ndarray(from existing data)的更多相关文章
- numpy 基于数值范围创建ndarray()
基于数值范围创建函数创建ndarray 1 numpy.arange arange([start=0,] stop[, step=1,][, dtype=None]) >>> np. ...
- NumPy来自现有数据的数组
NumPy - 来自现有数据的数组 这一章中,我们会讨论如何从现有数据创建数组. numpy.asarray 此函数类似于numpy.array,除了它有较少的参数. 这个例程对于将 Python 序 ...
- NumPy 基于数值区间创建数组
来源:Python Numpy 教程 章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基 ...
- (18)ASP.NET Core 基于现有数据库创建EF模型(反向工程)
1.简介 Entity Framework Core可通过数据库提供给应用程序的插件访问许多不同的数据库.我们可以通过使用Entity Framework Core构建执行基本数据访问的ASP.NET ...
- NumPy 基于已有数据创建数组
原文:Python Numpy 教程 章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基 ...
- How to: Generate XPO Business Classes for Existing Data Tables 如何:为现有数据表生成 XPO 业务类
From the Tutorial and other documentation sources, you learned how to create business classes for yo ...
- Sql server中用现有表中的数据创建Sql的Insert插入语句
之前,在Codeproject发表过一篇关于用现有表中数据创建Insert的Sql语句的存储过程,今天将其搬到这里来,注意本存储过程仅适用于SQL SERVER. 介绍 一些时候,你想导出一些现有表中 ...
- Python数据分析与展示[第三周](pandas简介与数据创建)
第三周的课程pandas 分析数据 http://pandas.pydata.org import pandas as pd 常与numpy matplotlib 一块定义 d=pd.Series(r ...
- 用NumPy genfromtxt导入数据
用NumPy genfromtxt导入数据 NumPy provides several functions to create arrays from tabular data. We focus ...
随机推荐
- lvs 隧道模式请求没有回应的解决
众所周知,lvs共有三种转发机制:NAT,IPTUNNELING与DIRECT ROUTING 我在做隧道模式实验时遇到了一个问题,客户端向虚拟ip(即隧道0的ip)发送请求后,lvs服务器接收到了请 ...
- 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)
怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...
- iOS 获取设备型号 ip6更新
//获得设备型号 + (NSString *)getCurrentDeviceModel:(UIViewController *)controller { ]; size_t len; char *m ...
- Jenkins Xcode 证书设置错误 Code Sign error: No matching codesigning identity found: No codesigning identities
Jenkins 集成Xcode 项目的时候在证书上遇到了问题.实际上如果在本地的话.只要Xcode工程里选择了项目就不需要重新设置证书了.jenkins会自动找到这个证书,只要在build setti ...
- LintCode: Flatten Binary Tree to Linked List
C++ Traverse /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, ...
- Android Studio 之 Launch AVD 时" Intel HAXM is required to run this AVD, VT-x is disabled in BIOS; "
问题描述:Launch AVD 时弹窗信息" Intel HAXM is required to run this AVD, VT-x is disabled in BIOS; " ...
- 微信小程序 - tabbar动态更换图标以及文字
大家不喜欢小程序的tabbar原因之一就是它太死板,tabbar一旦在app.json定义以后文字不能改,图标不能改! 我要自制tabbar!!! 其实在微信更新完1.9.0基础调试以后就推出了更新方 ...
- 初识:JMX
来自:http://blog.csdn.net/derekjiang/article/details/4531952 JMX是一份规范,SUN依据这个规范在JDK(1.3.1.4.5.0)提供了JMX ...
- Mysql Left Join Where On
select t1.id,t2.idfrom t1left join t2 on t1.id = t2.id and t1.id>1 and t2.id<>3在mysql的left ...
- Java并发容器——ConcurrentSkipListMap和ConcurrentHashMap
一:ConcurrentSkipListMap TreeMap使用红黑树按照key的顺序(自然顺序.自定义顺序)来使得键值对有序存储,但是只能在单线程下安全使用:多线程下想要使键值对按照key的顺序来 ...