|NO.Z.00026|——————————|BigDataEnd|——|Hadoop&Python.v04|——|Arithmetic.v04|NumPy科学计算库:NumPy数组创建|

一、NumPy科学计算库
### --- Po,功夫熊猫中阿宝。勤加练习,你就所向披靡!

~~~     NumPy(Numerical Python)是Python的⼀种开源的数值计算扩展。
~~~     提供多维数组对象,各种派⽣对象(如掩码数组和矩阵),这种⼯具可⽤来存储和处理⼤型矩阵,
~~~     ⽐Python⾃身的嵌套列表(nested list structure)
~~~     结构要⾼效的多(该结构也可以⽤来表示矩阵(matrix)),
~~~     ⽀持⼤量的维度数组与矩阵运算,此外也针对数组运算提供⼤量的数学函数库,
~~~     包括数学、逻辑、形状操作、排序、选择、输⼊输出、离散傅⽴叶变换、基本线性代数,
~~~     基本统计运算和随机模拟等等。
### --- ⼏乎所有从事Python⼯作的数据分析师都利⽤NumPy的强⼤功能。

~~~     强⼤的N维数组
~~~     成熟的⼴播功能
~~~     ⽤于整合C/C++和Fortran代码的⼯具包
~~~     NumPy提供了全⾯的数学功能、随机数⽣成器和线性代数功能
### --- 安装Python库

~~~     pip install jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple
~~~     pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
~~~     # 启动终端
~~~     Windows----> 快捷键:win + R ----->输⼊:cmd回⻋------>命令⾏出来
~~~     Mac ---->启动终端

~~~     # 启动jupyter
~~~     进⼊终端输⼊指令:jupyter notebook
~~~     在哪⾥启动jupyter启动,浏览器上的⽬录,对应哪⾥

一、数组创建
### --- 创建数组的最简单的⽅法就是使⽤array函数,将Python下的list转换为ndarray。

import numpy as np
l = [1,3,5,7,9]                                         # 列表
arr = np.array(l)                                       # 将列表转换为NumPy数组
arr                                                     # 数据⼀样,NumPy数组的⽅法,功能更加强⼤
~~~ 输出为
# array([1, 3, 5, 7, 9])
### --- 我们可以利⽤np中的⼀些内置函数来创建数组,⽐如我们创建全0的数组,

~~~     也可以创建全1数组,全是其他数字的数组,或者等差数列数组,正态分布数组,随机数。
import numpy as np
arr1 = np.ones(10)                                      # 输出为:array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
arr2 = np.zeros(10)                                     # 输出为: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
arr3 = np.full(shape = [2,3],fill_value=2.718)
~~~ 输出为:
# array([[2.718, 2.718, 2.718],
# [2.718, 2.718, 2.718]])
arr4 = np.arange(start = 0,stop = 20,step = 2)
# 等差数列 输出为:array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
arr5 = np.linspace(start =0,stop = 9,num = 10)
# 等差数列 输出为:array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
arr6 = np.random.randint(0,100,size = 10)
# int随机数 输出为:array([ 4, 8, 79, 62, 34, 35, 2, 65, 47, 18])
arr7 = np.random.randn(5)
# 正态分布 输出为:array([ 0.57807872, 0.37922855, 2.37936837, -0.28688769, 0.2882854 ])
arr8 = np.random.random(size = 5)
# float 随机数 输出为:array([0.59646412, 0.37960586, 0.38077327, 0.76983539, 0.22689201])
二、查看操作
### --- jupyter扩展插件

~~~     # jupyter扩展插件
~~~     # NumPy的数组类称为ndarray,也被称为别名 array。请注意,numpy.array这与标准Python库类不同array.array,后者仅处理⼀维数组且功能较少。ndarray对象的重要属性是
 ~~~ pip install jupyter_contrib_nbextensions -i https://pypi.tuna.tsinghua.edu.cn/simple
 ~~~ pip install jupyter_nbextensions_configurator -i https://pypi.tuna.tsinghua.edu.cn/simple
 ~~~ jupyter contrib nbextension install --user
 ~~~ jupyter nbextensions_configurator enable --user
### --- 查看操作

~~~     # 数组的轴数、维度
import numpy as np
arr = np.random.randint(0,100,size = (3,4,5))
arr.ndim                                                # 输出 3
~~~     # 数组尺⼨形状

import numpy as np
arr = np.random.randint(0,100,size = (3,4,5))
arr.shape                                               # 输出 (3,4,5)
~~~     # 数组元素的总数

import numpy as np
arr = np.random.randint(0,100,size = (3,4,5))
arr.size                                                # 输出 3*4*5 = 60
~~~     # 数据类型

import numpy as np
arr = np.random.randint(0,100,size = (3,4,5))
arr.dtype                                               # 输出 dtype('int64')
~~~     # 数组中每个元素的⼤⼩(以字节为单位)

import numpy as np
arr = np.random.randint(0,100,size = (3,4,5))
arr.itemsize                                            #输出是 8 ,因为数据类型是int64,64位,⼀个字节是8位,所以64/8 = 8
三、文件IO操作
### --- 保存数组

~~~     # save⽅法保存ndarray到⼀个npy⽂件,也可以使⽤savez将多个array保存到⼀个.npz⽂件中
x = np.random.randn(5)
y = np.arange(0,10,1)
~~~     # save⽅法可以存⼀个ndarray
np.save("x_arr",x)
~~~     # 如果要存多个数组,要是⽤savez⽅法,保存时以key-value形式保存,key任意(xarr、yarr)
np.savez("some_array.npz",xarr = x,yarr=y)
### --- 读取

~~~     # load⽅法来读取存储的数组,如果是.npz⽂件的话,读取之后相当于形成了⼀个key-value类型的变量,通过保存时定义的key来获取相应的array
np.load('x_arr.npy')                                        # 直接加载
~~~     # 通过key获取保存的数组数据
np.load('some_array.npz')['yarr']
### --- 读写csv、txt⽂件

~~~     # 读写csv、txt文件
arr = np.random.randint(0,10,size = (3,4))
~~~     # 储存数组到txt⽂件
np.savetxt("arr.csv",arr,delimiter=',')                     # ⽂件后缀是txt也是⼀样的
~~~     # 读取txt⽂件,delimiter为分隔符,dtype为数据类型
np.loadtxt("arr.csv",delimiter=',',dtype=np.int32)

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(27)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示