当前位置:网站首页>NumPy学习挑战第五关-创建数组
NumPy学习挑战第五关-创建数组
2022-06-26 06:44:00 【云曦我女神】
NumPy 创建数组
ndarray 数组除了可以使用底层 ndarray 构造器来创建外,也可以通过以下几种方式来创建。
1、numpy.empty
numpy.empty 方法用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组:
numpy.empty(shape, dtype = float, order = 'C')
shape:数组形状;
dtype:数据类型,可选;
order:C代表行优先,F代表列优先,只代表在计算机内存中的存储元素的顺序,得到的数组形状一样。
a=np.empty([3,2],dtype='i1',order='C')
print(a)
print('\n')
b=np.empty([3,2],dtype='i1',order='F')
print(b)
[[-16 32]
[ 48 41]
[ 10 32]]
[[-16 32]
[ 32 70]
[ 32 111]]
2、numpy.zeros
创建指定大小的数组,数组元素以 0 来填充:
numpy.zeros(shape, dtype = float, order = 'C')
a=np.zeros([3,2],dtype='i1',order='C')
print(a)
[[0 0]
[0 0]
[0 0]]
3、numpy.ones
创建指定形状的数组,数组元素以 1 来填充
numpy.ones(shape, dtype = None, order = 'C')
b=np.ones([3,2],dtype='i1',order='F')
print(b)
[[1 1]
[1 1]
[1 1]]
4、其他方式
import numpy as np
arr1=np.array([1,2,3,4,5,6])#列表创建一维数组
arr2=np.array(((1,2,3),(4,5,6)))#嵌套元组创建二维数组,两个元组要用小括号括起
print('一维数组:\n',arr1)
print('二维数组:\n',arr2)
一维数组:
[1 2 3 4 5 6]
二维数组:
[[1 2 3]
[4 5 6]]
import numpy as np
arr1=np.array([1,2,3,4,5,6])#列表创建一维数组
arr2=np.array(((1,2,3),(4,5,6),(7,8,9),(10,11,12)))#嵌套元组创建二维数组,两个元组要用小括号括起
print('二维数组:\n',arr2)
#获取第2行第1列元素
print(arr2[1,0])
#获取第三列数据
print(arr2[:,2])
#获取前两行
print(arr2[:2])
print('\n')
#获取单独的几行几列:ix_(rows,cols)
#获取第1,最后一行,第2,最后一列元素组成的数组
print(arr2[np.ix_([0,-1],[1,-1])])
二维数组:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
4
[ 3 6 9 12]
[[1 2 3]
[4 5 6]]
[[ 2 3]
[11 12]]
边栏推荐
- Experience the new features of Milvus 2.0 together
- Custom reference formats used by Zotero
- LabVIEW Arduino tcp/ip remote smart home system (project part-5)
- 屏幕共享推荐
- If you meet a female driver who drives didi as an amateur, you can earn 500 yuan a day!
- 一芯实现喷雾|WS2812驱动|按键触摸|LED显示|语音播报芯片等功能,简化加湿器产品设计
- 直播预告丨消防安全讲师培训“云课堂”即将开讲!
- SQL Basics
- 3.pyinstaller module introduction
- How to transfer database data to check box
猜你喜欢
Pytorch uses multi GPU parallel training and its principle and precautions
[digital signal processing] basic sequence (basic sequence lists | unit pulse sequence | unit pulse function | discrete unit pulse function | difference between unit pulse function and discrete unit p
Mysql delete in 不走索引的
MYSQL(三)
MySQL delete in without index
“试用期避免被辞退“ 指南攻略
直播预告丨消防安全讲师培训“云课堂”即将开讲!
Bugku exercise ---misc--- prosperity, strength and democracy
On a classical problem
遇到女司机业余开滴滴,日入500!
随机推荐
[digital signal processing] basic sequence (unit step sequence | relationship between unit step sequence and unit pulse sequence | rectangular sequence | relationship between rectangular sequence and
Kotlin Compose 状态恢复 rememberSaveable 与 remember
Pytorch mixing accuracy principle and how to start this method
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
个人博客系统需求分析
我在腾讯做测试的这几年...
Number of connections server database message: error number 2003can't connect to MySQL server on 'server address' (10061)
Differences, advantages and disadvantages between synchronous communication and asynchronous communication
浅析一道经典题
Guide to "avoid dismissal during probation period"
Vulnerability discovery - API interface service vulnerability probe type utilization and repair
Installation and login of MySQL database
I use flask to write the website "II"
Go学习笔记1.3-变量的数据类型篇
OCA安全联盟(CyberSecurity Mesh)
typescript的type
【golang】time相关
Go语言学习笔记 1.2-变量篇
How can an enterprise successfully complete cloud migration?
API and encapsulation of cookies