栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

1、Numpy数组(一)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1、Numpy数组(一)

Numpy数组
一、numpy介绍:numpy实际上是Python的一个工具库,专为进行严格的数字处理而产生。numpy中的核心元素是ndarray,ndarray可以看成数组也可以看成向量或者矩阵。
二、numpy中数组的创建
1、用array建立数组:

import numpy as np

#建立一维数组
#(1)参数为列表
a1=np.array([1,2,3,4,5,6])
print(a1)#[1 2 3 4 5 6]

#(2)参数为元组
a2=np.array((1,2,3,4,5,6))
print(a2)#[1 2 3 4 5 6]

#建立二维数组
#(1)参数为列表
b1=np.array([[1,2,3],[4,5,6]])
print(b1)
'''
[[1 2 3]
[4 5 6]]
'''
#(2)参数为列表嵌套元组
b2=np.array([(1,2,3),(4,5,6)])
print(b2)
'''
[[1 2 3]
[4 5 6]]
'''

#建立三维数组
#(1)参数为列表
c1=np.array([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
print(c1)
'''
[[[1 2 3]
  [4 5 6]]

 [[1 2 3]
  [4 5 6]]]
  
#建立字符串数组
d1=np.array(['b','aaaa','aaaaadddd'])#这是一个二维数组
print(r1[0])

#布尔数组
d2=np.array([True,False,0,1])
print(d2)

#浮点型数组
d3=np.array([10.2,2.4])
print(d3)

#复数数组
d4=np.array([1+3j,4+5j,8j])
print(d4)#[1.+3.j 4.+5.j 0.+8.j]

'''
注意:numpy的数组元素要求同一类型
'''
'''

2、arange(start,stop,step,dtype=None):以指定步长累加产生指定范围有序元素的数组,默认start=0,step=1

import numpy as np
h1=np.arange(5)
print(h1)#[0 1 2 3 4]
h2=np.arange(2,9,2)
print(h2)#[2 4 6 8]
h3=np.arange(5,0,-1)
print(h3)#[5 4 3 2 1]
h4=np.arange(5,2,-0.5)
print(h4)#[5.  4.5 4.  3.5 3.  2.5]

3、linspace():在指定范围内返回均匀步长的样本数组,这个样本数量由第三个参数确定

linspace(start,stop,num=num_points,endpoint=True,retstep=False,dtype=None,axis=0)
start:返回样本数据开始点
stop:返回样本数据结束点
num:生成的样本数据量,默认为50
endpoint:True则包含stop;False则不包含stop
retstep:If True, return (samples, step), where step is the spacing between samples.(即如果为True则结果会给出数据间隔)
dtype:数组类型
axis:0(默认)或-1
import numpy as np
h1=np.linspace(0,1,2)
print(h1)#[0. 1.]
h2=np.linspace(0,4,7)
print(h2)#[0.         0.66666667 1.33333333 2.         2.66666667 3.33333333   4.        ]
h3=np.linspace(0,1,2,0,1)
print(h3)#(array([0. , 0.5]), 0.5)0.5是步长
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/303927.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号