NumPy 基础

 

 

1. NumPy简介

NumPy 是 Numerical Python的简称,它是Python中的科学计算基本软件包。NumPy 为 Python 提供了大量数学库,使我们能够高效地进行数字计算。

 

参考文档

https://docs.scipy.org/doc/numpy-1.13.0/contents.html

http://www.scipy-lectures.org/intro/numpy/index.html

 

为什么需要使用NumPy?

* NumPy 处理大型数据时, 速度是python处理的几百倍

* NumPy 可以表示向量和矩阵的多维数组数据结构

* NumPy具有大量内置的数学函数,快速进行各种复杂的数学计算

 

例如进行时间比较

使用python 来处理一亿个数据

 

import time
import numpy as np

x = np.random.random(100000000)


start = time.time()

#sum(x)/len(x)
np.mean(x)
print(time.time() - start)

 

运行结果: 需要59s

D:\learn-python>python compare_time.py
59.67717123031616

 

如果使用numpy 处理

D:\learn-python>python compare_time.py
0.36800432205200195

 

 

2. NumPy ndarray

NumPy 的核心是 ndarray,其中 nd 表示 n 维。ndarray 是一个多维数组,其中的所有元素类型都一样。

换句话说,ndarray 是一个形状可以多样,并且可以存储数字或字符串的网格。

 

开始使用NumPy前, 必须导入该包

import numpy as np

 

 

2.1 创建ndarray

有两种方式可以创建ndarray

1. 使用普通的Python列表

2. 使用内置的NumPy函数

 

2.1.1 使用普通的Python列表

import numpy as np

# We create a 1D ndarray that contains only integers
x = np.array([1, 2, 3, 4, 5])

print('x = ', x)

 

 

运行结果

D:\learn-python>python compare_time.py
x = [1 2 3 4 5]

 

将一维数组称之为秩为 1 的数组。通常,N 维数组的秩为 N。因此,二维数组称为秩为 2 的数组。

数组的另一个重要特性是形状。数组的形状是指每个维度的大小。例如,秩为 2 的数组的形状对应于数组的行数和列数。

你将发现,NumPy ndarray 具有特殊的属性,使我们能够非常直观地获取关于 ndarray 的信息。例如,可以通过 .shape 属性获取 ndarray 的形状。

shape 属性返回一个由 n 个正整数(用于指定每个维度的大小)组成的元组。

 

上面示例打印x的维度、形状、类型

# We print information about x
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype)

 

 

D:\learn-python>python compare_time.py
x = [1 2 3 4 5]
x has dimensions: (5,)
x is an object of type: <class 'numpy.ndarray'>
The elements in x are of type: int32

 

shape 属性返回了元组 (5,),即 x 的秩为 1(即 x 只有一个维度),并且有 5 个元素。

type() 函数告诉我们 x 的确是 NumPy ndarray。

最后,.dtype 属性告诉我们 x 的元素作为有符号 64 位整数存储在内存中。

 

 

2.1.2 使用NumPy函数创建

代码如下

import numpy as np

shape = (3, 4)

# We create a 3 x 4 ndarray full of zeros. 
#X = np.zeros((3,4))
#X = np.ones(shape, dtype=np.float32)
#X = np.full(shape, 5)
#X = np.eye(5)
#X = np.diag([10, 20, 30, 50])
#X = np.arange(10)
#X = np.arange(4, 10)
#X = np.arange(1, 14, 3)
#X = np.reshape(1, 14, 3)
#X = np.random.random(shape)
X = np.random.randint(4, 15, shape)

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

 

 

运行结果

D:\learn-python>python compare_time.py

X =
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

X has dimensions: (3, 4)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: float64

 

代码解释:

np.zeros() 函数可以生成指定形状的一个numpy数组,所有元素都为0

np.ones() 函数也可以生成特定形状的numpy数组,但所有元素都是1

np.ones() 运行结果如下

D:\learn-python>python compare_time.py

X =
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]

X has dimensions: (3, 4)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: float32

 

np.full() 函数可以指定生成特定形状的numpy数组, 所有元素的值可以手工指定

D:\learn-python>python compare_time.py

X =
 [[5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]]

X has dimensions: (3, 4)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: int32

 

 

线性代数中有一种矩阵叫作单位矩阵,可以使用np.eye() 函数生成

运行结果如下

D:\learn-python>python compare_time.py

X =
 [[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]

X has dimensions: (5, 5)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: float64

 

 

使用np.diag()函数来生成对角矩阵

D:\learn-python>python compare_time.py

X =
 [[10  0  0  0]
 [ 0 20  0  0]
 [ 0  0 30  0]
 [ 0  0  0 50]]

X has dimensions: (4, 4)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: int32

 

 

使用np.arrange函数生成矩阵

np.arange(10) 只有一个参数时

运行结果

D:\learn-python>python compare_time.py
X =
 [0 1 2 3 4 5 6 7 8 9]

X has dimensions: (10,)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: int32

 

 

np.arange(4, 10) 有两个参数时

运行结果

D:\learn-python>python compare_time.py

X =
 [4 5 6 7 8 9]

X has dimensions: (6,)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: int32

 

 

np.arange(1, 14, 3) 有三个参数时

D:\learn-python>python compare_time.py

X =
 [ 1  4  7 10 13]

X has dimensions: (5,)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: int32

 

 

如果是使用非整数的步长, 则最好使用np.linspace() 函数

 

np.reshape() 函数可以将之前的数字生成任意形状

 

np.random() 函数可以生成随机数组,值在0-1之间

D:\learn-python>python compare_time.py

X =
[[0.59171839 0.55245693 0.12976434 0.12736712]
[0.84688173 0.48640098 0.83468587 0.91336004]
[0.71028495 0.06896051 0.82233413 0.78084142]]

X has dimensions: (3, 4)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: float64

 

np.random()函数输入3个参数

D:\learn-python>python compare_time.py

X =
[[ 5 4 10 7]
[ 7 5 14 13]
[ 4 7 8 7]]

X has dimensions: (3, 4)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: int32

 

 

2.2 操作ndarray

本节介绍如何操纵ndarray中的数据, NumPy ndarray是可变的,意味着ndarray中的元素在ndarray创建之后可以更改。

例如切片操作,在机器学习中,需要用来拆分数据,分为训练集、交叉集、测试集等。

 

2.2.1 访问数组中某个数据

import numpy as np

x = np.array([1, 2, 3, 4, 5])
print(x)

print('list element:', x[0])

 

 

直接使用该下标即可获取值

运行结果如下:

D:\learn-python>python compare_time.py
[1 2 3 4 5]
list element: 1

 

posted @ 2020-01-09 19:22  elewei  阅读(175)  评论(0编辑  收藏  举报