# Numpy 小结

2

Python 真火来学习一下，先来看一个库 NumPy。NumPy是Python语言的一个扩充程序库。支持高级大量的维度数组与矩阵运算，此外也针对数组运算提供大量的数学函数库。

# 1. 读取文件

numpy.genfromtxt() 用于读取 txt 文件，其中传入的参数依次为：

1. 需要读取的 txt 文件位置，此处文件与程序位于同一目录下
2. 分割的标记
3. 转换类型，如果文件中既有文本类型也有数字类型，就先转成文本类型

help(numpy.genfromtxt)用于查看帮助文档：

``````import numpy

world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str)
print(type(world_alcohol))
print(world_alcohol)
print(help(numpy.genfromtxt))``````

# 2. 构造 ndarray

## numpy.array()构造 ndarray

numpy.array()中传入数组参数，可以是一维的也可以是二维三维的。numpy 会将其转变成 ndarray 的结构。

``````vector = numpy.array([1,2,3,4])
matrix = numpy.array([[1,2,3],[4,5,6]])``````

``````vector = numpy.array([1,2,3,4])

array([1, 2, 3, 4])``````

``````vector = numpy.array([1,2,3,4.0])

array([ 1.,  2.,  3.,  4.])``````

``````vector = numpy.array([1,2,'3',4])

array(['1', '2', '3', '4'],dtype='<U21')``````

## 利用 .shape 查看结构

``````print(vector.shape)
print(matrix.shape)
(4,)
(2, 3)``````

## 利用 dtype 查看类型

``````vector = numpy.array([1,2,3,4])
vector.dtype

dtype('int64')``````

## ndim 查看维度

``````vector = numpy.array([1,2,3,4])
vector.ndim

1``````

``````matrix = numpy.array([[1,2,3],
[4,5,6],
[7,8,9]])
matrix.ndim

2``````

## size 查看元素数量

``````matrix.size
9``````

# 3. 获取与计算

## numpy 能使用切片获取数据

``````matrix = numpy.array([[1,2,3],
[4,5,6],
[7,8,9]])``````

## 根据条件获取

numpy 能够依次比较 vector 和元素之间是否相同

``````vector = numpy.array([5, 10, 15, 20])
vector == 10

array([False,  True, False, False], dtype=bool)``````

``````vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print(equal_to_ten)
print(vector[equal_to_ten])

[False  True False False]
[10]``````

``````vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)``````
``````vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)``````

## 类型转换

``````vector = numpy.array([5, 10, 15, 20])
print(vector.dtype)
vector = vector.astype(str)
print(vector.dtype)

int64
<U21``````

## 求和

sum() 能够对 ndarray 进行各种求和操作，比如分别按行按列进行求和

``````matrix = numpy.array([[1,2,3],
[4,5,6],
[7,8,9]])
print(matrix.sum())
print(matrix.sum(1))
print(matrix.sum(0))

45
[ 6 15 24]
[12 15 18]``````

sum(1) 是 sum(axis=1)) 的缩写，1表示按照 x轴方向求和，0表示按照y轴方向求和

# 4. 常用函数

## reshape

``````import numpy as np
arr = np.arange(15).reshape(3, 5)
arr

array([[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14]])``````

## zeros

``````np.zeros ((3,4))

array([[ 0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.]])``````

## ones

``````np.ones( (2,3,4), dtype=np.int32 )

array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],

[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])``````

## range

``````np.arange(0,10,2)

array([0, 2, 4, 6, 8])``````

## random 随机数

``````np.random.random((2,3))

array([[ 0.86166627,  0.37756207,  0.94265883],
[ 0.9768257 ,  0.96915312,  0.33495431]])``````

# 5. ndarray 运算

``````a = np.array([10,20,30,40])
b = np.array(4)

a - b
array([ 6, 16, 26, 36])``````

``````a**2
array([ 100,  400,  900, 1600])``````

``````np.sqrt(B)

array([[ 1.41421356,  0.        ],
[ 1.73205081,  2.        ]])``````

e 求方

``````np.exp(B)

array([[  7.3890561 ,   1.        ],
[ 20.08553692,  54.59815003]])``````

``````a = np.floor(10*np.random.random((2,2)))
a

array([[ 0.,  0.],
[ 3.,  6.]])``````

``````a.T

array([[ 0.,  3.],
[ 0.,  6.]])``````

``````a.resize(1,4)
a

array([[ 0.,  0.,  3.,  6.]])``````

## 6. 矩阵运算

``````A = np.array( [[1,1],
[0,1]] )
B = np.array( [[2,0],
[3,4]] )``````

``````A*B

array([[2, 0],
[0, 4]])``````

``````print (A.dot(B))
print(np.dot(A,B))

[[5 4]
[3 4]]``````

``````a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))

print(a)
print(b)
print(np.hstack((a,b)))

[[ 2.  3.]
[ 9.  3.]]
[[ 8.  1.]
[ 0.  0.]]
[[ 2.  3.  8.  1.]
[ 9.  3.  0.  0.]]``````

``````print(np.vstack((a,b)))

[[ 2.  3.]
[ 9.  3.]
[ 8.  1.]
[ 0.  0.]]``````

``````#横向分割
print( np.hsplit(a,3))
#纵向风格
print(np.vsplit(a,3))``````

# 7. 复制的区别

## 地址复制

``````a = np.arange(12)
b = a
print(a is b)

print(a.shape)
print(b.shape)
b.shape = (3,4)
print(a.shape)
print(b.shape)

True
(12,)
(12,)
(3, 4)
(3, 4)``````

## 复制值

``````a = np.arange(12)
c = a.view()
print(c is a)

c.shape = 2,6
c[0,0] = 9999

print(a)
print(c)

False
[9999    1    2    3    4    5    6    7    8    9   10   11]
[[9999    1    2    3    4    5]
[   6    7    8    9   10   11]]``````

## 完整拷贝

a.copy() 进行的完整的拷贝，产生一份完全相同的独立的复制

``````a = np.arange(12)
c = a.copy()
print(c is a)

c.shape = 2,6
c[0,0] = 9999

print(a)
print(c)

False
[ 0  1  2  3  4  5  6  7  8  9 10 11]
[[9999    1    2    3    4    5]
[   6    7    8    9   10   11]]``````