numpy.array()

功能:创建一个数据

vector = numpy.array([1,2,3,4])
matrix = numpy.array([1,2,3,4],[11,12,13,14])

numpy.shape

功能:查看有多少个对象在数组中

print(vector.shape)

Numpy 基础学习-LMLPHP

print(matrix.shape)

Numpy 基础学习-LMLPHP

numpy.genfromtxt()

功能:Load data from a text file(从txt加载数据)

文件内容示例:

ear,WHO region,Country,Beverage Types,Display Value
1986,Western Pacific,Viet Nam,Wine,0
1986,Americas,Uruguay,Other,0.5
1985,Africa,Cte d'Ivoire,Wine,1.62
...
1986,Americas,Colombia,Beer,4.27
1987,Americas,Saint Kitts and Nevis,Beer,1.98
1987,Americas,Guatemala,Other,0
1987,Africa,Mauritius,Wine,0.13
1985,Africa,Angola,Spirits,0.39
1986,Americas,Antigua and Barbuda,Spirits,1.55

读取代码

world_alcohol = numpy.genfromtxt('world_alcohol.txt',delimiter=',',dtype=str)

打印矩阵

[['Year' 'WHO region' 'Country' 'Beverage Types' 'Display Value']
['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
['1986' 'Americas' 'Uruguay' 'Other' '0.5']
...
['1987' 'Africa' 'Malawi' 'Other' '0.75']
['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]

help(numpy.genfromtxt)

功能:查看该方法如何使用

In>> help(numpy.genfromtxt)
Out>>

Numpy 基础学习-LMLPHP

个人更推荐直接查看该方法的定义的方式来查看方法如何使用
Numpy 基础学习-LMLPHP

切片操作

1.取world_alcohol的所有年份

world_alcohol[:,:1]

:代表所有,:1代表第一列,意义就是取所有行的第一列

2.取world_alcohol的第三列

world_alcohol[:,2:3]

运算 判断

先定一个矩阵

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

判断矩阵中是否存在某个元素

matrix[:,]==2

Numpy 基础学习-LMLPHP
返回一个数组,Flase为不匹配,True为匹配

用匹配信息进行查找
示例:查找第一列为2 的行 所在第三列的值

qe = matrix[:,0] == 2
# array([False, False, True]) matrix[qe,2]
# array([3])

运算 与

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

Numpy 基础学习-LMLPHP

print((vector == 1) & (vector >0))
# out >> [ True False False False]

运算 或

vector = numpy.array([1,2,3,4])
print((vector == 1) | (vector ==2))
# out >> [ True True False False]

类型转换

使用astype来进行类型转换

vector = numpy.array(['1','2','3'])
print(vector.dtype)
## <U1 vector = vector.astype(int)
print(vector.dtype)
## int64

矩阵转换

>>> vector = numpy.arange(15)
>>> print(vector)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
>>> matrix = vector.reshape(3,5)
>>> print(matrix)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
>>> matrix
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])

ndim 维度

>>> matrix.ndim
2

matrix来自上面的矩阵转换

初始化一个矩阵

>>> numpy.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

随机

>>> numpy.random.random((2,3))
array([[0.92741381, 0.60763819, 0.62092669],
[0.0685093 , 0.31973933, 0.59478389]])

linspace 指定范围和数量的初始化

>>> numpy.linspace(1,2*numpy.pi,100)
array([1. , 1.05336551, 1.10673102, 1.16009652, 1.21346203,
1.26682754, 1.32019305, 1.37355856, 1.42692407, 1.48028957,
1.53365508, 1.58702059, 1.6403861 , 1.69375161, 1.74711711,
1.80048262, 1.85384813, 1.90721364, 1.96057915, 2.01394465,
2.06731016, 2.12067567, 2.17404118, 2.22740669, 2.2807722 ,
2.3341377 , 2.38750321, 2.44086872, 2.49423423, 2.54759974,
2.60096524, 2.65433075, 2.70769626, 2.76106177, 2.81442728,
2.86779279, 2.92115829, 2.9745238 , 3.02788931, 3.08125482,
3.13462033, 3.18798583, 3.24135134, 3.29471685, 3.34808236,
3.40144787, 3.45481338, 3.50817888, 3.56154439, 3.6149099 ,
3.66827541, 3.72164092, 3.77500642, 3.82837193, 3.88173744,
3.93510295, 3.98846846, 4.04183396, 4.09519947, 4.14856498,
4.20193049, 4.255296 , 4.30866151, 4.36202701, 4.41539252,
4.46875803, 4.52212354, 4.57548905, 4.62885455, 4.68222006,
4.73558557, 4.78895108, 4.84231659, 4.8956821 , 4.9490476 ,
5.00241311, 5.05577862, 5.10914413, 5.16250964, 5.21587514,
5.26924065, 5.32260616, 5.37597167, 5.42933718, 5.48270268,
5.53606819, 5.5894337 , 5.64279921, 5.69616472, 5.74953023,
5.80289573, 5.85626124, 5.90962675, 5.96299226, 6.01635777,
6.06972327, 6.12308878, 6.17645429, 6.2298198 , 6.28318531])

取整

>>> matrix = numpy.random.random((2,3))
>>> matrix
array([[0.80081883, 0.85121955, 0.13076995],
[0.93531681, 0.63438252, 0.72251243]])
>>> numpy.floor(matrix)
array([[0., 0., 0.],
[0., 0., 0.]])

拼接

hstack

>>> a = numpy.random.random((2,3))
>>> b = numpy.random.random((2,3))
>>> a
array([[0.95498357, 0.18999871, 0.66418543],
[0.57704126, 0.65051646, 0.29100003]])
>>> b
array([[0.1027083 , 0.02873905, 0.91481418],
[0.91912233, 0.24024705, 0.51269805]])
>>> numpy.hstack((a,b))
array([[0.95498357, 0.18999871, 0.66418543, 0.1027083 , 0.02873905,
0.91481418],
[0.57704126, 0.65051646, 0.29100003, 0.91912233, 0.24024705,
0.51269805]])

vstack

>>> a = numpy.random.random((2,3))
>>> b = numpy.random.random((2,3))
>>> a
array([[0.95498357, 0.18999871, 0.66418543],
[0.57704126, 0.65051646, 0.29100003]])
>>> b
array([[0.1027083 , 0.02873905, 0.91481418],
[0.91912233, 0.24024705, 0.51269805]])
>>> numpy.vstack((a,b))
array([[0.95498357, 0.18999871, 0.66418543],
[0.57704126, 0.65051646, 0.29100003],
[0.1027083 , 0.02873905, 0.91481418],
[0.91912233, 0.24024705, 0.51269805]])

切分

hsplit

行切割

>>> c = numpy.floor(10*numpy.random.random((2,12)))
>>> c
array([[3., 7., 9., 1., 4., 3., 3., 9., 5., 4., 9., 9.],
[3., 9., 8., 6., 7., 5., 4., 8., 1., 4., 8., 7.]]) >>> numpy.hsplit(c,3) #平均切割为3份
[array([[3., 7., 9., 1.],
[3., 9., 8., 6.]]),
array([[4., 3., 3., 9.],
[7., 5., 4., 8.]]),
array([[5., 4., 9., 9.],
[1., 4., 8., 7.]])] >>> numpy.hsplit(c,(3,4)) #指定位置切割
[array([[3., 7., 9.],
[3., 9., 8.]]), array([[1.],
[6.]]), array([[4., 3., 3., 9., 5., 4., 9., 9.],
[7., 5., 4., 8., 1., 4., 8., 7.]])]

vsplit

列切割

>>> d = numpy.floor(10*numpy.random.random((3,12)))
array([[1., 3., 4., 6., 6., 1., 6., 7., 3., 2., 8., 0.],
[7., 0., 0., 1., 5., 2., 6., 8., 0., 3., 3., 4.],
[3., 8., 7., 0., 7., 9., 1., 6., 7., 4., 6., 1.]]) >>> numpy.vsplit(d,3) # 平均切割为3份
[array([[1., 3., 4., 6., 6., 1., 6., 7., 3., 2., 8., 0.]]),
array([[7., 0., 0., 1., 5., 2., 6., 8., 0., 3., 3., 4.]]),
array([[3., 8., 7., 0., 7., 9., 1., 6., 7., 4., 6., 1.]])]

深拷贝和浅拷贝

赋值

完全相同的对象

>>> a = numpy.arange(12)
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> b= a
>>> b
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> print(b is a)
True

浅拷贝

对象不同,但引用的值相同

>>> c = a.view()
>>> print(c is a)
False
>>> c.shape = (2,6)
>>> a.shape
(12,)
>>> a[0]=1
>>> a
array([ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> c
array([[ 1, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])

深拷贝

完全不同的两个对象

>>> d = a.copy()
>>> d is a
False
>>> a
array([ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> d
array([ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> d.shape=(2,6)
>>> d
array([[ 1, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
>>> d[0,0]=0
>>> d
array([[ 0, 1, 2, 3, 4, 5],
04-15 02:18