科学计算库-Numpy

Numpy(科学计算库)

NumPy系统是Python的一种开源的数值计算扩展,这种工具可用来存储和处理大型矩阵

1.数据结构

#1.1 读取文件
test = numpy.genfromtxt("test.txt",delimiter=",",dtype=str)
//delimiter:#以,分隔符读取 
//dtype:一个用于说明数组数据类型的对象。返回的是该数组的数据类型
print(type(test))
print(test)
print(help(numpy.genfromtxt))
numpy.genfromtxt() 可以用来读取各种文件。常用语法大致如下:

  numpy.genfromtxt(fname, dtype=<type 'float'>, delimiter=None, skip_header=0, skip_footer=0)

  fname 要导入的文件路径

  dtype 指定要导入文件的类型,dtype=str / int等等

  delimiter  文件中不同元素间的间隔方式,空格还是逗号什么的,如:delimiter=',';

  skip_header 是否跳过开头行,一般第一行为类别,为0不跳过,为1或者2则跳过前面1或者2行

world_alcohol = numpy.genfromtxt("world_alcohol.txt",delimiter=",", dtype=str, skip_header=1)
print(world_alcohol)

[['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
 ['1986' 'Americas' 'Uruguay' 'Other' '0.5']
 ['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62']
 ...
 ['1987' 'Africa' 'Malawi' 'Other' '0.75']
 ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
 ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
#1.2 创建矩阵
import numpy
vector = numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print (vector)
print (matrix)

[ 5 10 15 20]
[[ 5 10 15]
 [20 25 30]
 [35 40 45]]
#1.3 查看维度
vector = numpy.array([5,10,15,20])
print (vector.shape)
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print (matrix.shape)

(4,)
(3, 3)
#1.4 查看数据类型
#数据类型不一样,无法查看数组的数据类型
vector = numpy.array([1,2,3,4])
print (vector)
print (vector.dtype)

[1 2 3 4]
dtype('int64')
#1.5 打印其中元素
other_1986 = world_alcohol[1,4]
third_country = world_alcohol[2,2]
print (other_1986)
print (third_country)

0.5
Cte d'Ivoire
#1.6 数组切片
vector = numpy.array([5,10,15,20])
print(vector[0:3])

[ 5 10 15]
#1.7 数组切片
matix = numpy.array([
    [5,10,15],
    [10,15,20],
    [15,20,25],
])
print(matix[:,1])
print("---------")
print(matix[:,0:2])
print("---------")
print(matix[0:1,0:2])
print("---------")
print(matix[0:2,0:2])

[10 15 20]
---------
[[ 5 10]
 [10 15]
 [15 20]]
---------
[[ 5 10]
---------
[[ 5 10]
 [10 15]]

2.基本操作

#2.1 查看数组元素
#查看数组是否包含10
import numpy
vector = numpy.array([5,10,15,20])
print(vector == 10)

array([False,  True, False, False])
#2.2 查看数组元素
#查看数组是否包含25
matix = numpy.array([
    [5,10,15],
    [10,15,20],
    [15,20,25],
])
print(matix == 25)

array([[False, False, False],
       [False, False, False],
       [False, False,  True]])
#2.3 查看数组元素
#为False的时候过滤掉
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]
#2.4 查看数组元素
vector = numpy.array([5,10,15,20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
equal_to_ten_or_five = (vector == 10) | (vector == 5)
print (equal_to_ten_and_five)
print (equal_to_ten_or_five)

vector[equal_to_ten_or_five] = 50
print (vector)

[False False False False]
[ True  True False False]
[50 50 15 20]
#2.5 类型转换
vector = numpy.array(["1","2","3"])
print (vector.dtype)
print (vector)
print ("--------")
vector = vector.astype(float)
print (vector.dtype)
print (vector)

<U1
['1' '2' '3']
--------
float64
[1. 2. 3.]
#2.6 找最小值
vector = numpy.array([5,10,15,20])
vector.min()
print (vector.min())

5
#2.7 按行求和
#2.8 按列求和
matrix = numpy.array([
    [5,10,15],
    [20,25,30],
    [35,40,45]
])
#按行求和
print (matrix.sum(axis=1))
#按列求和
print (matrix.sum(axis=0))

[ 30  75 120]
[60 75 90]

3.矩阵属性

import numpy as np
print (np.arange(15))
#生成矩阵
a = np.arange(15).reshape(3,5)
print ("------")
print (a)
print ("------")
#查看几行几列
print (a.shape)
print ("------")
#查看维度
print (a.ndim)
print ("------")
#查看类型
print (a.dtype.name)
print ("------")
#查看共有多少元素
print (a.size)

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

4.矩阵操作

#4.1 初始化矩阵,3行4列,值全为0
np.zeros ((3,4))

array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
#4.2 初始化矩阵,2个3行4列,值全为1
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]]], dtype=int32)
#4.3 起始到结束 每隔一位加定值
# 起始为10,终点为50,差值5
np.arange(10,50,5)

array([10, 15, 20, 25, 30, 35, 40, 45])
#4.4 初始化数组,改变结构
np.arange(20).reshape(4,5)

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
#4.5 从[-1,1]区间,生成2行4列
np.random.random((2,4))

array([[0.96736023, 0.13918327, 0.92569963, 0.86135961],
       [0.6545531 , 0.80578378, 0.44185247, 0.13230089]])
#4.6 从0到2*pi 平均取10个数
from numpy import pi
np.linspace(0,2*pi,10)

array([0.        , 0.6981317 , 1.3962634 , 2.0943951 , 2.7925268 ,
       3.4906585 , 4.1887902 , 4.88692191, 5.58505361, 6.28318531])
#4.7 矩阵的操作
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]]

5.基本函数

# 5.1 常用操作
# exp:平方  sqrt:根号
import numpy as np
B = np.arange(3)
print(B)
print(np.exp(B))
print(np.sqrt(B))

[0 1 2]
[1.         2.71828183 7.3890561 ]
[0.         1.         1.41421356]
# 5.2 常用操作
#ravel:将多维数组转换为一维数组
a = np.floor(10*np.random.random((3,4)))
print(a)
print("--------")
print(a.ravel())
print("--------")
a.shape = (6,2)
print(a)
print("-------")
#把a转置
print(a.T)

[[2. 7. 8. 5.]
 [1. 0. 1. 2.]
 [0. 6. 6. 6.]]
--------
[2. 7. 8. 5. 1. 0. 1. 2. 0. 6. 6. 6.]
--------
[[2. 7.]
 [8. 5.]
 [1. 0.]
 [1. 2.]
 [0. 6.]
 [6. 6.]]
-------
[[2. 8. 1. 1. 0. 6.]
 [7. 5. 0. 2. 6. 6.]]
#5.3 拼接
#horizontal水平,vertocal垂直。
import numpy as np
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print("-----")
print(b)
print("-----")
#竖向拼接
print(np.vstack((a,b)))
print("-----")
#横向拼接
print(np.hstack((a,b)))

[[6. 0.]
 [5. 6.]]
-----
[[6. 8.]
 [9. 4.]]
-----
[[6. 0.]
 [5. 6.]
 [6. 8.]
 [9. 4.]]
-----
[[6. 0. 6. 8.]
 [5. 6. 9. 4.]]
#5.4 切分
a = np.floor(10*np.random.random((2,12)))
print(a)
print("------")
print(np.hsplit(a,3))
print("------")
#指定切割位置  在3的位置切和在4的位置切
print(np.hsplit(a,(3,4)))

a = np.floor(10*np.random.random((12,2)))
print("------")
print(a)
np.vsplit(a,3)

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

6.函数几种复制方法

6.1 =操作
# a和b只是名字不同,指向同一个区域
a = np.arange(12)
b = a
print (b is a)
b.shape = (3,4)
print(a.shape)
print(id(a))
print(id(b))

True
(3, 4)
4432047680
4432047680
#6.2 view操作
#a和c虽然结构不同,但值一样
c = a.view()
print (c is a)
c.shape = 2,6
print (a.shape)
c[0,4] = 1234
print (a)
print ("------")
print (c)
print (id(a))
print (id(c))

False
(3, 4)
[[   0    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
------
[[   0    1    2    3 1234    5]
 [   6    7    8    9   10   11]]
4432065840
4432066240
#6.3 copy操作
#copy完,a和d没有关系
d = a.copy()
d is a
d[0,0] = 9999
print(d)
print("------")
print(a)

[[9999    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
------
[[   0    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
#6.4 查找操作
import numpy as np
data = np.sin(np.arange(20)).reshape(5,4)
print(data)
#axis = 0按列找,axis = 1按行找
ind = data.argmax(axis = 0)
print(ind)
data_max = data[ind,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]]
[2 0 3 1]
[0.98935825 0.84147098 0.99060736 0.6569866 ]
#6.5 tile扩展操作
a = np.arange(0,40,10)
print(a)
b = np.tile(a,(2,3))
print (b)

[ 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]]
#6.6 sort排序
a = np.array([[4,3,5],[1,3,5]])
print(a)
b = np.sort(a,axis = 1)
print ("------b")
print (b)

a.sort(axis = 1)
print ("------a")
print (a)

a = np.array([4,3,1,2])
#求最小数的索引,把最小值索引排序
j = np.argsort(a)
print ("------")
print(j)
print ("------")
#按索引排序
print(a[j])

[[4 3 5]
 [1 3 5]]
------b
[[3 4 5]
 [1 3 5]]
------a
[[3 4 5]
 [1 3 5]]
------
[2 3 1 0]
------
[1 2 3 4]

总结:
1.读取文件 numpy.genfromtxt(“test.txt”,delimiter=",",dtype=str)
2.创建矩阵 numpy.array
3.查看维度 vector.shape
4.查看数据类型 vector.dtype
5.数组切片 vector[0:3]
6.查看数组是否包含元素10 print(vector == 10)
7.类型转换 vector.astype
8.找最小值 vector.min()
9.按行求和 matrix.sum(axis=1)
10.按列求和 matrix.sum(axis=0)
11.维度转换 vector.reshape(3,5)
12.查看维度 a.ndim
13.初始化矩阵,3行4列,值全为0. np.zeros ((3,4))
14.初始化矩阵,2个3行4列,值全为1. np.ones((2,3,4),dtype=np.int32)
15.起始为10,终点为50,差值5. np.arange(10,50,5)
16.生成随机数,从[-1,1]区间,生成2行4列 np.random.random((2,4))
17.从x到y平均取z个数 linspace(x,y,z)
18.矩阵对应位置相乘 A*B
19.矩阵相乘 A.dot(B) np.dot(A,B)
20.exp:平方 sqrt:根号
21.将多维数组转换为一维数组 a.ravel()
22.把a转置 print(a.T)
23.竖向拼接:np.vstack((a,b)) 横向拼接:np.hstack((a,b))
24.竖向切分:np.hsplit((a,b)) 横向切分:np.hsplit(a,3)
25.复制方法: =、view、copy
26.查找操作:axis = 0按列找,axis = 1按行找
27.扩展操作:np.tile(a,(2,3))
28.排序:np.sort(a,axis = 1) 求最小数的索引:np.argsort(a)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值