Home Backend Development Python Tutorial Python NumPy库安装使用笔记

Python NumPy库安装使用笔记

Jun 06, 2016 am 11:16 AM
numpy library python

1. NumPy安装
使用pip包管理工具进行安装

代码如下:


$ sudo pip install numpy


使用pip包管理工具安装ipython(交互式shell工具)

代码如下:


$ sudo pip instlal ipython
$ ipython --pylab  #pylab模式下, 会自动导入SciPy, NumPy, Matplotlib模块


2. NumPy基础

2.1. NumPy数组对象

具体解释可以看每一行代码后的解释和输出

代码如下:


In [1]: a = arange(5)  # 创建数据
In [2]: a.dtype
Out[2]: dtype('int64')  # 创建数组的数据类型
In [3]: a.shape  # 数组的维度, 输出为tuple
Out[3]: (5,)
In [6]: m = array([[1, 2], [3, 4]])  # array将list转换为NumPy数组对象
In [7]: m  # 创建多维数组
Out[7]:
array([[1, 2],
       [3, 4]])
In [10]: m.shape  # 维度为2 * 2
Out[10]: (2, 2)
In [14]: m[0, 0]  # 访问多维数组中特定位置的元素, 下标从0开始
Out[14]: 1
In [15]: m[0, 1]
Out[15]: 2

2.2. 数组的索引和切片

代码如下:


In [16]: a[2: 4]  # 切片操作类似与Python中list的切片操作
Out[16]: array([2, 3])
In [18]: a[2 : 5: 2]  # 切片步长为2
Out[18]: array([2, 4])
In [19]: a[ : : -1]  # 翻转数组
Out[19]: array([4, 3, 2, 1, 0])
In [20]: b = arange(24).reshape(2, 3, 4)  # 修改数组的维度
In [21]: b.shape
Out[21]: (2, 3, 4)
In [22]: b  # 打印数组
Out[22]:
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 [23]: b[1, 2, 3]  # 选取特定元素
Out[23]: 23
In [24]: b[ : , 0, 0]  # 忽略某个下标可以用冒号代替
Out[24]: array([ 0, 12])
In [23]: b[1, 2, 3]
Out[23]: 23
In [24]: b[ : , 0, 0]  # 忽略多个下标可以使用省略号代替
Out[24]: array([ 0, 12])
In [26]: b.ravel()  # 数组的展平操作
Out[26]:
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 [27]: b.flatten()  # 与revel功能相同, 这个函数会请求分配内存来保存结果
Out[27]:
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 [30]: b.shape = (6, 4)  # 可以直接对shape属性赋值元组来设置维度
In [31]: b
Out[31]:
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 [30]: b.shape = (6, 4)  # 矩阵的转置
In [31]: b
Out[31]:
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]])

2.3. 组合数组

代码如下:


In [1]: a = arange(9).reshape(3, 3)  # 生成数组对象并改变维度
In [2]: a
Out[2]:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In [3]: b = a * 2  # 对a数组对象所有元素乘2
In [4]: b
Out[4]:
array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16]])
#######################
In [5]: hstack((a, b))  # 水平组合数组a和数组b
Out[5]:
array([[ 0,  1,  2,  0,  2,  4],
       [ 3,  4,  5,  6,  8, 10],
       [ 6,  7,  8, 12, 14, 16]])
      
In [6]: vstack((a, b))  # 垂直组合数组a和数组b
Out[6]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16]])
In [7]: dstack((a, b))  # 深度组合数组, 沿z轴方向层叠组合数组
Out[7]:
array([[[ 0,  0],
        [ 1,  2],
        [ 2,  4]],
       [[ 3,  6],
        [ 4,  8],
        [ 5, 10]],
       [[ 6, 12],
        [ 7, 14],
        [ 8, 16]]])

2.4. 分割数组

代码如下:


In [8]: a
Out[8]:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In [9]: hsplit(a, 3)  # 将数组沿水平方向分割成三个相同大小的子数组
Out[9]:
[array([[0],
        [3],
        [6]]),
 array([[1],
        [4],
        [7]]),
 array([[2],
        [5],
        [8]])]
In [10]: vsplit(a, 3)  # 将数组沿垂直方向分割成三个子数组
Out[10]: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]

2.5. 数组的属性

代码如下:


In [12]: a.ndim  # 给出数组的尾数或数组的轴数
Out[12]: 2
In [13]: a.size  # 数组中元素的个数
Out[13]: 9
In [14]: a.itemsize  # 数组中元素在内存中所占字节数(int64)
Out[14]: 8
In [15]: a.nbytes  # 数组所占总字节数, size * itemsize
Out[15]: 72
In [18]: a.T  # 和transpose函数一样, 求数组的转置
Out[18]:
array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])

2.6. 数组的转换

代码如下:


In [19]: a.tolist()  # 将NumPy数组转换成python中的list
Out[19]: [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

3. 常用函数

代码如下:


In [22]: c = eye(2)  # 构建2维单位矩阵
In [23]: c
Out[23]:
array([[ 1.,  0.],
       [ 0.,  1.]])
In [25]: savetxt("eye.txt", c)  # 将矩阵保存到文件中
In [5]: c, v = loadtxt("test.csv", delimiter=",", usecols=(0, 1), unpack=True)  # 分隔符为, usecols为元组表示要获取的字段数据(每一行的第零段和第一段), unpack为True表示拆分存储不同列的数据, 分别存入c, v
In [12]: c
Out[12]: array([ 1.,  4.,  7.])
In [13]: mean(c)  # 计算矩阵c的mean均值
Out[13]: 4.0
In [14]: np.max(c)  # 求数组中的最大值
Out[14]: 7.0
In [15]: np.min(c)  # 求数组中的最小值
Out[15]: 1.0
In [16]: np.ptp(c)  # 返回数组最大值和最小值之间的差值
Out[16]: 6.0
In [18]: numpy.median(c)  # 找到数组中的中位数(中间两个数的平均值)
Out[18]: 4.0
In [19]: numpy.var(c)  # 计算数组的方差
Out[19]: 6.0
In [20]: numpy.diff(c)  # 返回相邻数组元素的差值构成的数组
Out[20]: array([ 3.,  3.])
In [21]: numpy.std(c)  # 计算数组的标准差
Out[21]: 2.4494897427831779
In [22]: numpy.where(c > 3)  # 返回满足条件的数组元素的下标组成的数组
Out[22]: (array([1, 2]),)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles