二、Numpy

本节内容:

科学计算库Numpy
Numpy基础结构
Numpy矩阵基础
Numpy常用函数
矩阵常用操作
不同复制操作对比
 
1、用numpy打开一个数据
import numpy
data1 = numpy.genfromtxt("data1.txt",delimiter=",",dtype="str")
#文件名:data1.txt,以逗号作为分隔符,字符串类型
print(type(data1))
print(data1)
print(help(numpy.genfromtxt)
#通过帮助文档来看numpy.genfromtxt

2、构造数组:numpy.array()

import numpy
vector = numpy.array([1,3,5])
matrix = numpy.array([[1,2,5],[2,6,8],[3,7,9]])
print(type(vector))
print(vector)
print(type(matrix))
print(matrix)

###
<class 'numpy.ndarray'>
[1 3 5]
<class 'numpy.ndarray'>
[[1 2 5]
 [2 6 8]
 [3 7 9]]
###

3、看一个矩阵的结构.shape

可以看一个矩阵的行和列

import numpy
vector = numpy.array([1,3,5])
matrix = numpy.array([[1,2,5],[2,6,8]])
print(vector.shape)
print(matrix.shape)

###
(3,)一维数组,其中包含3个元素
(2, 3)二维数组,两行三列
###

4、numpy.array的整体变化特性(会保证所有传进来的东西都是同一个类型)

其中一个元素发生变化,所有的元素都会进行同样的类型转换

import numpy
vector = numpy.array([1,3,5])
print(vector)
print(vector.dtype)
vector1 = numpy.array([1,3,5.0])
print(vector1)
print(vector1.dtype)
vector2 = numpy.array([1,3,'5'])
print(vector2)
print(vector2.dtype)#看vector2的当前类型

###

[1 3 5]
int32
[ 1.  3.  5.]
float64
['1' '3' '5']
<U11
###

5、通过索引取数据

 

import numpy
data1 = numpy.genfromtxt("data1.txt",delimiter=",",dtype="str",skip_header=1)#先把数据读进来
print(type(data1))
print(data1)
data1_00=data1[0,0]
data1_01=data1[0,1]
data1_11=data1[1,1]
print(data1_00,data1_01,data1_11)
###
<class 'numpy.ndarray'>
[['5.5277' '9.1302']
['8.5186' '13.662']
['7.0032' '11.854']
['5.8598' '6.8233']
['8.3829' '11.886']
['7.4764' '4.3483']]

5.5277 9.1302 13.662
###

6、切片

import numpy
vector = numpy.array([1,3,5,7])
print(vector[0:3])#取左不取右  [1 3 5]  
matrix = numpy.array([[1,2,5],[2,6,8],[3,7,9]])
print(matrix)
print(matrix[:,1])#取二维数组的第一列 [2 6 7]
print(matrix[:,0:2])#取第0列和第1列
print(matrix[0:2,0:2])
###
[1 3 5]
[[1 2 5]
[2 6 8]
[3 7 9]]
[2 6 7]
[[1 2]
[2 6]
[3 7]]
[[1 2]
[2 6]]
###

*行列都是从0开始的。

7、判断

import numpy
vector = numpy.array([1,3,5,7])
vector == 5  #看vector中存不存在5这个数
matrix = numpy.array([[1,2,5],[2,6,8],[3,7,9]])
matrix == 9 #对矩阵中的每一个元素都进行了判断,看是否等于9,返回一些bool值
###
array([[False, False, False],
       [False, False, False],
       [False, False,  True]], dtype=bool)
###

索引:

import numpy
vector = numpy.array([1,3,5,7])
equal_to_5 = (vector == 5 )#用equal_to_5来接收判断之后返回的bool值
print(equal_to_5)
print(vector[equal_to_5])#用equal_to_5来做索引,找出vector中等于5的值,如果bool的值为ture,则进行返回,否则不进行返回

###
[False False  True False]
[5]
###
import numpy
matrix = numpy.array([[1,2,5],[2,6,8],[3,7,9]])
second_column_6 = (matrix[:,1] == 6 )#判断第一列中是否有等于6的,返回bool值
print(matrix)
print(second_column_6)
print(matrix[second_column_6,:])#用得到的这个与6相对应的ture做索引,打印含有6这个数字的第一行
###
[[1 2 5]
 [2 6 8]
 [3 7 9]]
[False  True False]
[[2 6 8]]
###

 

import numpy
vector = numpy.array([1,3,5,7])
equal_to_5_and_7 = (vector == 5 ) & (vector == 7)#判断是否有值既满足等于5有等于7
print(equal_to_5_and_7)
#[False False False False]

 

import numpy
vector = numpy.array([1,3,5,7])
equal_to_5_and_7 = (vector == 5 ) | (vector == 7)#判断是否有值既满足等于5有等于7
print(equal_to_5_and_7)
 #[False False True True]
 
 

 

import numpy
vector = numpy.array([1,3,5,7])
equal_to_5_and_7 = (vector == 5 ) | (vector == 7)#判断是否有值既满足等于5有等于7
vector[equal_to_5_and_7] = 10
print(vector)

8、转换数据类型

import numpy
vector = numpy.array(['1','3','5'])
print(vector)
print(vector.dtype)
vector = vector.astype(float)#转换数据类型
print(vector)
print(vector.dtype)
###

['1' '3' '5']
<U1
[ 1.  3.  5.]
float64
###

9、找最大最小值

import numpy
vector = numpy.array(['1','3','5'])
vector = vector.astype(int)
print(vector.min())#1找最小值
print(vector.max())#5找最大值
print(help(numpy.array))#更多请找help进行查看

 

 10、对所有行所有列进行求和

import numpy
matrix = numpy.array([[1,2,5],[2,6,8],[3,7,9]])
print(matrix)
print(matrix.sum(axis=1))#[ 8 16 19]对每一行进行求和
print(matrix.sum(axis=0))#[ 6 15 22]对每一列进行求和
print(sum(matrix))#对列进行求和 ### [[1 2 5] [2 6 8] [3 7 9]] [ 8 16 19] [ 6 15 22]
[ 6 15 22]
###

11、构造完矩阵之后对矩阵做变化

import numpy as np
a = np.arange(15)#[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
print(a)
print(a.shape)#(15,)
print(a.ndim)#1
a = a.reshape(3,5)#将上面的一维数组重建为一个3行5列的数组
print(a)
print(a.shape)#(3, 5)
print(a.ndim)#查看维度   2
print(a.dtype.name)#看数据类型  int32
print(a.size)#阵列里所有元素的总数  15 ###
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
(15,)
1
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
(3, 5)
2
int32
15
###
import numpy as np
a = np.arange(12).reshape((3,4))
print(a)

###
import numpy as np
a = np.arange(12).reshape((3,4))
print(a)
###

 

12、初始化矩阵

import numpy as np#给numpy指定一个别名np
a = np.zeros((3,4))#初始化一个3行4列元素全为0的矩阵
print(a)
b= np.ones((3,4))#初始化一个3行4列元素全为1的矩阵
print(b)
c= np.ones((2,3,4),dtype = np.int32)#初始化两个3行4列元素全为1的矩阵,并指定数据类型为整型
print(c)

###
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]
[[[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]

 [[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]]
###

13、构建一个序列

import numpy as np
a = np.arange(10,30,5)#构建一个序列,起始值为10,结束值为30,步长为5  [10 15 20 25]
print(a)
b = np.arange(0,2,0.3)#[ 0.   0.3  0.6  0.9  1.2  1.5  1.8]
print(b)

###
[10 15 20 25]
[ 0.   0.3  0.6  0.9  1.2  1.5  1.8]
###

14、随机模块

import numpy as np
a = np.random.random((2,3))#在numpy下的random模块中调用random函数,随机生成一个2*3的矩阵,其元素值在-1~+1之间
print(a)

###
[[ 0.08854379  0.85398     0.66946974]
 [ 0.81927271  0.1475651   0.03371494]]
###

15、linspace函数

import numpy as np
from numpy import pi
a = np.linspace(0,2*pi,100)#从0到6.28等分量生成100个数
print(a)
print(np.sin(a))
###
[ 0.          0.06346652  0.12693304  0.19039955  0.25386607  0.31733259
  0.38079911  0.44426563  0.50773215  0.57119866  0.63466518  0.6981317
  0.76159822  0.82506474  0.88853126  0.95199777  1.01546429  1.07893081
  1.14239733  1.20586385  1.26933037  1.33279688  1.3962634   1.45972992
  1.52319644  1.58666296  1.65012947  1.71359599  1.77706251  1.84052903
  1.90399555  1.96746207  2.03092858  2.0943951   2.15786162  2.22132814
  2.28479466  2.34826118  2.41172769  2.47519421  2.53866073  2.60212725
  2.66559377  2.72906028  2.7925268   2.85599332  2.91945984  2.98292636
  3.04639288  3.10985939  3.17332591  3.23679243  3.30025895  3.36372547
  3.42719199  3.4906585   3.55412502  3.61759154  3.68105806  3.74452458
  3.8079911   3.87145761  3.93492413  3.99839065  4.06185717  4.12532369
  4.1887902   4.25225672  4.31572324  4.37918976  4.44265628  4.5061228
  4.56958931  4.63305583  4.69652235  4.75998887  4.82345539  4.88692191
  4.95038842  5.01385494  5.07732146  5.14078798  5.2042545   5.26772102
  5.33118753  5.39465405  5.45812057  5.52158709  5.58505361  5.64852012
  5.71198664  5.77545316  5.83891968  5.9023862   5.96585272  6.02931923
  6.09278575  6.15625227  6.21971879  6.28318531]
[  0.00000000e+00   6.34239197e-02   1.26592454e-01   1.89251244e-01
   2.51147987e-01   3.12033446e-01   3.71662456e-01   4.29794912e-01
   4.86196736e-01   5.40640817e-01   5.92907929e-01   6.42787610e-01
   6.90079011e-01   7.34591709e-01   7.76146464e-01   8.14575952e-01
   8.49725430e-01   8.81453363e-01   9.09631995e-01   9.34147860e-01
   9.54902241e-01   9.71811568e-01   9.84807753e-01   9.93838464e-01
   9.98867339e-01   9.99874128e-01   9.96854776e-01   9.89821442e-01
   9.78802446e-01   9.63842159e-01   9.45000819e-01   9.22354294e-01
   8.95993774e-01   8.66025404e-01   8.32569855e-01   7.95761841e-01
   7.55749574e-01   7.12694171e-01   6.66769001e-01   6.18158986e-01
   5.67059864e-01   5.13677392e-01   4.58226522e-01   4.00930535e-01
   3.42020143e-01   2.81732557e-01   2.20310533e-01   1.58001396e-01
   9.50560433e-02   3.17279335e-02  -3.17279335e-02  -9.50560433e-02
  -1.58001396e-01  -2.20310533e-01  -2.81732557e-01  -3.42020143e-01
  -4.00930535e-01  -4.58226522e-01  -5.13677392e-01  -5.67059864e-01
  -6.18158986e-01  -6.66769001e-01  -7.12694171e-01  -7.55749574e-01
  -7.95761841e-01  -8.32569855e-01  -8.66025404e-01  -8.95993774e-01
  -9.22354294e-01  -9.45000819e-01  -9.63842159e-01  -9.78802446e-01
  -9.89821442e-01  -9.96854776e-01  -9.99874128e-01  -9.98867339e-01
  -9.93838464e-01  -9.84807753e-01  -9.71811568e-01  -9.54902241e-01
  -9.34147860e-01  -9.09631995e-01  -8.81453363e-01  -8.49725430e-01
  -8.14575952e-01  -7.76146464e-01  -7.34591709e-01  -6.90079011e-01
  -6.42787610e-01  -5.92907929e-01  -5.40640817e-01  -4.86196736e-01
  -4.29794912e-01  -3.71662456e-01  -3.12033446e-01  -2.51147987e-01
  -1.89251244e-01  -1.26592454e-01  -6.34239197e-02  -2.44929360e-16]
###

16、矩阵的基本运算

import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
print(a)#[10 20 30 40]
print(b)#[0 1 2 3]
print(b**2)#[0 1 4 9]
c = a-b
print(c)#[10 19 28 37]
c =c-1
print(c)#[ 9 18 27 36]
print(a>35)#[False False False  True]

###
[10 20 30 40]
[0 1 2 3]
[0 1 4 9]
[10 19 28 37]
[ 9 18 27 36]
[False False False  True]
###
import numpy as np
a = np.array([[1,1],[0,1]])
b = np.array([[2,0],[3,4]])
print(a)
print('--------')
print(b)
print('--------')
print(a*b)#相对应位置上的数进行相乘
print('--------')
print(a.dot(b))#矩阵与矩阵之间进行相乘,行列相乘并相加
print('--------')
print(np.dot(a,b))#矩阵与矩阵之间相乘

###
[[1 1]
 [0 1]]
--------
[[2 0]
 [3 4]]
--------
[[2 0]
 [0 4]]
--------
[[5 4]
 [3 4]]
--------
[[5 4]
 [3 4]]
###
import numpy as np
a = np.arange(3)
print(a)#[0 1 2]
print(np.exp(a))#对a的每个元素求指数   [ 1.          2.71828183  7.3890561 ]
print(np.sqrt(a))#对a的每个元素开平方根  [ 0.          1.          1.41421356]

17、关于矩阵的一些操作

import numpy as np
a = np.random.random((3,4))
print(a)
print("----------------------------------------------------")
b = 10*a
print(b)
print("----------------------------------------------------")
print(np.floor(b))#向下取整
print("----------------------------------------------------")
print(np.ceil(b))#向上取整
print("----------------------------------------------------")
print(b.ravel())#将矩阵转为一个一维向量
print("----------------------------------------------------")
b.shape=(6,2)#将一维向量转为6*2的矩阵
print(b)
print("----------------------------------------------------")
print(b.T)
print("----------------------------------------------------")
print(b.reshape(3,-1))#矩阵变维,给出行数,-1表示默认为4列
###
[[ 0.63541947  0.96585385  0.72111004  0.22907506]
 [ 0.29056626  0.82302183  0.30211646  0.79812054]
 [ 0.50067806  0.65067756  0.12233978  0.13923017]]
----------------------------------------------------
[[ 6.35419473  9.65853851  7.21110043  2.29075061]
 [ 2.9056626   8.23021831  3.02116464  7.98120542]
 [ 5.00678057  6.50677562  1.22339782  1.39230172]]
----------------------------------------------------
[[ 6.  9.  7.  2.]
 [ 2.  8.  3.  7.]
 [ 5.  6.  1.  1.]]
----------------------------------------------------
[[  7.  10.   8.   3.]
 [  3.   9.   4.   8.]
 [  6.   7.   2.   2.]]
----------------------------------------------------
[ 6.35419473  9.65853851  7.21110043  2.29075061  2.9056626   8.23021831
  3.02116464  7.98120542  5.00678057  6.50677562  1.22339782  1.39230172]
----------------------------------------------------
[[ 6.35419473  9.65853851]
 [ 7.21110043  2.29075061]
 [ 2.9056626   8.23021831]
 [ 3.02116464  7.98120542]
 [ 5.00678057  6.50677562]
 [ 1.22339782  1.39230172]]
----------------------------------------------------
[[ 6.35419473  7.21110043  2.9056626   3.02116464  5.00678057  1.22339782]
 [ 9.65853851  2.29075061  8.23021831  7.98120542  6.50677562  1.39230172]]
----------------------------------------------------
[[ 6.35419473  9.65853851  7.21110043  2.29075061]
 [ 2.9056626   8.23021831  3.02116464  7.98120542]
 [ 5.00678057  6.50677562  1.22339782  1.39230172]]
###

18、矩阵的拼接

import numpy as np
a = np.floor(10*np.random.random((3,4)))
b = np.floor(10*np.random.random((3,4)))
print(a)
print("----------------------------------------------------")
print(b)
print("----------------------------------------------------")
print(np.hstack((a,b)))#两矩阵横拼接
print("----------------------------------------------------")
print(np.vstack((a,b)))#两矩阵竖着拼接 ### [[ 4. 2. 0. 3.] [ 2. 5. 5. 3.] [ 2. 3. 8. 8.]] ---------------------------------------------------- [[ 6. 7. 0. 5.] [ 6. 5. 9. 6.] [ 7. 0. 0. 6.]] ---------------------------------------------------- [[ 4. 2. 0. 3. 6. 7. 0. 5.] [ 2. 5. 5. 3. 6. 5. 9. 6.] [ 2. 3. 8. 8. 7. 0. 0. 6.]]
---------------------------------------------------- [[ 8.  1.  1.  6.]
[ 5.  4.  4.  6.]
[ 2.  1.  9.  4.]
[ 7.  6.  2.  0.]
[ 9.  9.  4.  4.]
[ 3.  3.  5.  1.]]
###

19、矩阵的分割

import numpy as np
a = np.floor(10*np.random.random((2,12)))
print(a)
print("----------------------------------------------------")
print(np.hsplit(a,3))#横切分割,将其分割成的3个2*4的矩阵
print("----------------------------------------------------")
print(np.hsplit(a,(3,4)))#指定位置进行分割,在3和4的位置进行分割
b = np.floor(10*np.random.random((12,2)))
print(b)
print(np.vsplit(b,3))#竖切,将其分割成的3个4*2的矩阵

###
[[ 7.  2.  1.  2.  1.  8.  4.  7.  0.  8.  1.  8.]
 [ 9.  4.  4.  5.  0.  2.  7.  9.  1.  5.  3.  5.]]
----------------------------------------------------
[array([[ 7.,  2.,  1.,  2.],
       [ 9.,  4.,  4.,  5.]]), array([[ 1.,  8.,  4.,  7.],
       [ 0.,  2.,  7.,  9.]]), array([[ 0.,  8.,  1.,  8.],
       [ 1.,  5.,  3.,  5.]])]
----------------------------------------------------
[array([[ 7.,  2.,  1.],
       [ 9.,  4.,  4.]]), array([[ 2.],
       [ 5.]]), array([[ 1.,  8.,  4.,  7.,  0.,  8.,  1.,  8.],
       [ 0.,  2.,  7.,  9.,  1.,  5.,  3.,  5.]])]
[[ 2.  1.]
 [ 0.  2.]
 [ 9.  5.]
 [ 7.  3.]
 [ 9.  8.]
 [ 9.  7.]
 [ 5.  5.]
 [ 1.  3.]
 [ 1.  2.]
 [ 0.  8.]
 [ 3.  7.]
 [ 8.  1.]]
[array([[ 2.,  1.],
       [ 0.,  2.],
       [ 9.,  5.],
       [ 7.,  3.]]), array([[ 9.,  8.],
       [ 9.,  7.],
       [ 5.,  5.],
       [ 1.,  3.]]), array([[ 1.,  2.],
       [ 0.,  8.],
       [ 3.,  7.],
       [ 8.,  1.]])]
###

20、不同复制操作对比

import numpy as np
a = np.arange(12)
b = a
print(a)
print(b)
b.shape =(3,4)#将b矩阵改成3*4的矩阵
print(a)
print(b)
print(a.shape)#看a的结构,结果发现也变成了和b一样
print(id(a))
print(id(b))
#指向同一个地址
###
[ 0  1  2  3  4  5  6  7  8  9 10 11]
[ 0  1  2  3  4  5  6  7  8  9 10 11]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
(3, 4)
2021402549664
2021402549664
###
import numpy as np
a = np.array([[ 8,3,0,4],
 [ 9,9,1,9],
 [ 7, 8, 9,5]])
print(a)
c = a.view()#浅复制
print(c is a)
c.shape = (2,6)
c[0,4] = 1234
print(c)
print(a)
print(a.shape)
print(id(a))
print(id(c))
#指向不同的地址,但共用了一套值

###
[[8 3 0 4]
 [9 9 1 9]
 [7 8 9 5]]
False
[[   8    3    0    4 1234    9]
 [   1    9    7    8    9    5]]
[[   8    3    0    4]
 [1234    9    1    9]
 [   7    8    9    5]]
(3, 4)
2021402549424
2021402755920
In [ ]:



###
import numpy as np
a = np.array([[ 8,3,0,4],
 [ 9,9,1,9],
 [ 7, 8, 9,5]])
print(a)
d = a.copy()
print(d is a)
print(d)
d[0,0] =666
print(a)
print(d)

###
[[8 3 0 4]
 [9 9 1 9]
 [7 8 9 5]]
False
[[8 3 0 4]
 [9 9 1 9]
 [7 8 9 5]]
[[8 3 0 4]
 [9 9 1 9]
 [7 8 9 5]]
[[666   3   0   4]
 [  9   9   1   9]
 [  7   8   9   5]]
###

21、通过索引取每列的最大值

import numpy as np
data = np.sin(np.arange(20).reshape(4,5))#随机生成一个4行5列的矩阵
print(data)
index = data.argmax(axis=0)#取出每列中最大值的(行)位置
print(index)
data_max = data[index,range(data.shape[1])]#取出每列中最大值
print(data_max)

###
[[ 0.          0.84147098  0.90929743  0.14112001 -0.7568025 ]
 [-0.95892427 -0.2794155   0.6569866   0.98935825  0.41211849]
 [-0.54402111 -0.99999021 -0.53657292  0.42016704  0.99060736]
 [ 0.65028784 -0.28790332 -0.96139749 -0.75098725  0.14987721]]
[3 0 0 1 2]
[ 0.65028784  0.84147098  0.90929743  0.98935825  0.99060736]
###

22、对矩阵进行拓展

import numpy as np
a = np.arange(0,40,10)
print(a)
b = np.tile(a,(2,2))#对a的行扩2倍,列扩2倍
print(b)
c = np.tile(a,(3,2))#对a的行扩3倍,列扩2倍
print(c)

###
[ 0 10 20 30]

[[ 0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30]]

[[ 0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30]]
###

23、排序

import numpy as np
a = np.array([[4,3,5],[1,2,1]])
print(a)
b = np.sort(a,axis=1)#对a按行进行从小到大的排序
print(b)
print("----------------------------------------------------")
a.sort(axis=1)#对a按行进行从小到大的排序
print(a)
print("----------------------------------------------------")
a = np.array([4,3,2,1])
i = np.argsort(a)#取a按从小到大的排序的索引
print(i)
print("----------------------------------------------------")
print(a[i])#通过索引对i进行排序

###
[[4 3 5]
 [1 2 1]]
[[3 4 5]
 [1 1 2]]
----------------------------------------------------
[[3 4 5]
 [1 1 2]]
----------------------------------------------------
[3 2 1 0]
----------------------------------------------------
[1 2 3 4]




###

 

 

 

 
 
 
 
 
posted @ 2018-11-20 10:58  大头swag  阅读(287)  评论(0)    收藏  举报