当前位置:网站首页>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]]
边栏推荐
- 直播预告丨消防安全讲师培训“云课堂”即将开讲!
- 如何把数据库的数据传给复选框
- SecureCRT运行SparkShell 删除键出现乱码的解法
- Dpdk - tcp/udp protocol stack server implementation (I)
- 【图像检测】基于Itti模型实现图像显著性检测附matlab代码
- Research Report on pallet handling equipment industry - market status analysis and development prospect forecast
- How to make the main thread wait for the sub thread to execute before executing
- Phantom star VR equipment product details II: dark battlefield
- China micronutrient market trend report, technical innovation and market forecast
- DS18B20 details
猜你喜欢
Experience the new features of Milvus 2.0 together

Solution of garbled code in sparkshell deletion key of SecureCRT
![[micro service series] protocol buffer dynamic analysis](/img/86/357d55c77cc67d6413af2de59bf395.png)
[micro service series] protocol buffer dynamic analysis
[alluxio & Dachang] the original boss direct employment was applied in this way

MySQL 数据库的小白安装与登录

【图像检测】基于形态学实现图像目标尺寸测量系统附matlab代码

Past events of Xinhua III

Marketing skills: compared with the advantages of the product, it is more effective to show the use effect to customers
How can an enterprise successfully complete cloud migration?

Differences, advantages and disadvantages between synchronous communication and asynchronous communication
随机推荐
【图像融合】基于耦合特征学习的多模式医学图像融合附matlab代码
Self attention and multi head self attention (MSA) in transformer
New generation engineers teach you how to play with alluxio + ml (Part 1)
遇到女司机业余开滴滴,日入500!
Lightgbm-- parameter adjustment notes
Mysql delete in 不走索引的
Research Report on China's surfactant market demand and future investment opportunities 2022
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
China peek market outlook and future strategic planning proposal report 2022-2027
Research Report on market supply and demand and strategy of natural organic beauty industry in China
自顶向下的变成方法
3.pyinstaller module introduction
Installation and login of MySQL database
Live broadcast Preview - fire safety instructor training "cloud class" is about to start!
Go learning notes 1.3- data types of variables
Go语言学习笔记 1.2-变量篇
MySQL (III)
面试官:测试计划和测试方案有什么区别?
Bugku exercise ---misc--- prosperity, strength and democracy
Decompile Android applications, interview Android