Chapter3 numpy创建数组

这篇博客详细介绍了numpy库在Python中的使用,包括学习numpy的原因、numpy的基础概念、如何创建和操作数组,如数据类型、形状转换、计算及广播原则。还涉及numpy的应用,如读取本地数据、数组索引、转置、数值修改,以及统计函数和数组拼接等。适合初学者了解和掌握numpy的基本操作。
摘要由CSDN通过智能技术生成

numpy学习

学习numpy的原因

快速、方便、科学计算的科学库

什么是numpy

一个在python中做科学计算的基础库,重在数值计算,也是大部分python科学计算库的基础库,多用在大型、多维数组上执行数组运算。

numpy创建数组(矩阵)

一共有三种创建的方法,如下。

#coding=utf-8
import random

import numpy as np
#创建方法一
t1=np.array([1,2,3,])
print(t1)
print(type(t1))
#创建方法二
t2=np.array(range(10))
print(t2)
print(type(t2))
#创建方法三
t3=np.arange(4,10,2)
print(t3)
print(type(t3))

数组

数组的数据类型

(1)利用dtype来判断数组的数据类型。

#coding=utf-8
import numpy as np

t3=np.arange(4,10,2)
print(t3.dtype) 

(2)手动指定数组的数据类型

#coding=utf-8
import numpy as np
t4=np.array(range(1,4),dtype=float)
print(t4)
print(t4.dtype)

t5=np.array([1,1,0,1,0,0],dtype=bool)
print(t5)
print(t5.dtype)

(3)调整数组的数据类型
利用astype调整数组的数据类型。

#coding=utf-8
import numpy as np

t5=np.array([1,1,0,1,0,0],dtype=bool)

t6=t5.astype('int8')
print(t6)
print(t6.dtype)

(4)numpy中的小数

#coding=utf-8
import numpy as np

t7=np.array([random.random() for i in range(10)])
print(t7)
print(t7.dtype)
t8=np.round(t7,3)
print(t8)

数组的形状

进入命令提示符,运用代码pip install ipython安装ipython,然后输入ipython来启动ipython。
(1)分类
按照维数分为:一维数组、二维数组、三维数组……
可以通过shape来查看数组都形状

In [1]: import numpy as np

In [2]: t1=np.arange(12)

In [3]: t1
Out[3]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

In [4]: t2=np.array([[1,2,3],[4,5,6]])

In [5]: t2
Out[5]:
array([[1, 2, 3],
       [4, 5, 6]])

In [6]: t3=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])

In [7]: t3
Out[7]:
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
       
In [8]: t2.shape  #对于二维数组来说,(232——指的是行数  3——指的是列数
Out[8]: (2, 3)  

In [9]: t3.shape  #对于三维数组来说,(2232——指的是块数和行数  3——指的是列数
Out[9]: (2, 2, 3)

(2)改变数组的形状
*1:通过reshape来改变数组的形状

In [1]: import numpy as np

In [2]: t1=np.arange(12).reshape((3,4))

In [3]: t1
Out[3]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

*2:通过t.reshape()会直接返回结果,但是不改变原数组

In [4]: t2=np.arange(24).reshape((2,3,4))

In [5]: t2
Out[5]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

In [6]: t2.reshape((4,6))  #直接返回运行结果
Out[6]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])

In [7]: t2  #不改变原数组
Out[7]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

*3:将多维数组转为一维数组

In [8]: t3=np.arange(24).reshape(4,6)

In [9]: t3
Out[9]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])

In [10]: t3.reshape((24,))  #变一维数组,方式一
Out[10]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值