Python numpy 的用法

使用 numpy 打开 .txt 文件 numpy.genfromtxt


import numpy

world_alcohol = numpy.genfromtxt("test.txt", delimiter=",", dtype=str)
print(type(world_alcohol))
print(world_alcohol)
#print(help(numpy.genfromtxt))    # 查看函数说明文档

运行结果

<class 'numpy.ndarray'>
[['qwertyu' 'asdfgh']
 ['asd' 'tyy']]

numpy 矩阵操作    numpy.array 创建的数据必须是统一结构的

# 创建向量
vector = numpy.array([5, 10, 15, 20])
# 创建矩阵
matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ])
print(vector)
print(matrix)


print("-----------------------------------")
# 查看向量或矩阵的维度
print(vector.shape)
print(matrix.shape)


print("-----------------------------------")
# 查看当前是什么格式
vector = numpy.array([5, 10, 15, 20])
print(vector)
# vector.dtype # dtype('int32')
vector = numpy.array([5, 10, 15, 20.0])
print(vector)
vector.dtype
运行结果
 
[ 5 10 15 20]
[[ 5 10 15]
 [20 25 30]
 [35 40 45]]
-----------------------------------
(4,)
(3, 3)
-----------------------------------
[ 5 10 15 20]
[ 5. 10. 15. 20.]
dtype('float64')

读取矩阵样本

import numpy

# 读取矩阵文本(样本),并解析,skip_header=1 跳过首行
test = numpy.genfromtxt("test.txt", delimiter=",", dtype=str, skip_header=1)
print(test)

print("-------------------------------")
# 找出第二个样本的 0.5
a = test[1,3]
print(a)

print("-------------------------------")
print(test[0,2]) # 打印第一个样本的第三个列
运行结果
[['2018' ' zhang' ' china' ' 0']
 ['2017' ' wang' ' english' ' 0.5']]
-------------------------------
 0.5
-------------------------------
 china

读取矩阵的行列

# 读取向量的前三个元素
vector = numpy.array([5,10,15,20])
print(vector[0:3])

print("----------------------------")
# 读取矩阵的第二列
matrix = numpy.array([
    [1, 2, 3],
    [20, 21, 22],
    [30, 31, 32]
])
print(matrix[:,1])

print("----------------------------")
# 读取矩阵的后两列
print(matrix[:,1:])

print("----------------------------")
# 读取矩阵的第二行和第三行的前两列元素
print(matrix[1:3,0:2])
运行结果
[ 5 10 15]
----------------------------
[ 2 21 31]
----------------------------
[[ 2  3]
 [21 22]
 [31 32]]
----------------------------
[[20 21]
 [30 31]]

取值

import numpy

# 判断向量中的元素是否与10相等
vertor = numpy.array([5, 10, 15, 20])
vertor == 10
运行结果
array([False,  True, False, False])
import numpy

# 判断矩阵中的元素是否等于25
matrix = numpy.array([
    [5, 10, 15],
    [20, 25, 30],
    [35, 40, 45]
])
matrix == 25
运行结果
array([[False, False, False],
       [False,  True, False],
       [False, False, False]])
import numpy

# 把向量中等于 10 的值取出来
vertor = numpy.array([5, 10, 10, 20])
tmp = (vertor == 10) 
print(tmp)
print(vertor[tmp]) # bool 值当做索引,TRUE返回, FALSE 过滤掉了
运行结果

[False  True  True False]
[10 10]
import numpy

# 把矩阵中等于 10 的值取出来
matrix = numpy.array([
    [5, 10, 15],
    [20, 25, 30],
    [35, 25, 45]
])
# 判断第二列中等于25的值
tmp = (matrix[:,1] == 25)
print(tmp)
# 打印 等于 25 值得行
print(matrix[tmp, :])

运行结果

[False  True  True]
[[20 25 30]
 [35 25 45]]
import numpy

# 与或操作
vector = numpy.array([5, 10, 15, 20])
tmp = (vector == 10) & (vector == 5)
print(tmp)
tmp = (vector == 10) | (vector == 5)
print(tmp)

# 修改值
vector[tmp] = 60
print(vector)

运行结果

[False False False False]
[ True  True False False]
[60 60 15 20]

类型转换

import numpy

# 把string 值 转换为 float
vector = numpy.array(["1", "2", "3"])
print(vector.dtype)
print(vector)
vector = vector.astype(float)
print(vector.dtype)
print(vector)
运行结果
<U1
['1' '2' '3']
float64
[1. 2. 3.]

求值

import numpy

vector = numpy.array([5, 10, 15, 20])
# 求最小值
print(min(vector))
print(max(vector))

运行结果

5
20
import numpy

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

运行结果

[ 30  75 105]
[60 60 90]

矩阵属性

import numpy as np  # np 是 numpy 的一个别名

print(np.arange(15)) # 从0开始造15个数,比如造一个索引

print("----------------------------------------")
# 把向量转换成一个矩阵, 如转换成三行五列
a = np.arange(15).reshape(3,5)
print(a)

print("----------------------------------------")
# 打印行列数,常用于 debug 操作
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
----------------------------------------
int32
----------------------------------------
15

矩阵创建

import numpy as np  # np 是 numpy 的一个别名

print(np.arange(15)) # 从0开始造15个数,比如造一个索引

print("----------------------------------------")
# 把向量转换成一个矩阵, 如转换成三行五列
a = np.arange(15).reshape(3,5)
print(a)

print("----------------------------------------")
# 打印行列数,常用于 debug 操作
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
----------------------------------------
int32
----------------------------------------
15
import numpy as np 

# 创建空矩阵,默认数据类型为浮点型
np.zeros((3,4))

运行结果

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

import numpy as np 

# 创建元素都为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]]])

import numpy as np 

# 产生随机数
np.random.random((2,3))

运行结果

array([10, 15, 20, 25])
import numpy as np 

# 产生随机数
np.random.random((2,3))
运行结果
array([[0.03113358, 0.10459384, 0.56249966],
       [0.79602781, 0.13410629, 0.06759049]])
import numpy as np 
from numpy import pi

# 在一个区间上平均找出多少个元素,第一个元素起始值,第二个元素终止值,第三个元素是元素个数
np.linspace(0, 2*pi, 100)
运行结果
array([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])

矩阵运算

import numpy as np 

# 加减乘除
a = np.array([20, 30, 40, 50])
b = np.arange(4)
print(a)
print(b)

print("---------------------------")
# 对应位置上的元素相减
c = a - b
print(c)

print("---------------------------")
c = c - 1
print(c)

print("---------------------------")
# 平方
print(b**2)
print(a < 35)
运行结果
[20 30 40 50]
[0 1 2 3]
---------------------------
[20 29 38 47]
---------------------------
[19 28 37 46]
---------------------------
[0 1 4 9]
[ True  True False False]

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("-------------------------")
# 矩阵相乘,行乘上列,A的行乘上B的列,再相加
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

B = np.arange(3)
print(B)

# e 的 B 次幂
print(np.exp(B))

# 开方
print(np.sqrt(B))
运算结果
[0 1 2]
[1.         2.71828183 7.3890561 ]
[0.         1.         1.41421356]

import numpy as np

# 产生随机数
A = 10*np.random.random((3,4))
print(A)
print("-----------------------------------")

# 向下取整
a = np.floor(A)
print(a)

print("-----------------------------------")
# 把矩阵转换成向量
print(a.ravel())

print("-----------------------------------")
# 将矩阵转换为 6 行 2 列 
a.shape = (6, 2)
print(a)

print("-----------------------------------")
# 转置 - 列转化为行
print(a.T)
运算结果
[[1.36542801 7.02119261 0.0334998  6.68124413]
 [2.34966133 5.12600052 5.19708676 8.80336892]
 [2.03745319 7.88588516 8.31964932 6.64296087]]
-----------------------------------
[[1. 7. 0. 6.]
 [2. 5. 5. 8.]
 [2. 7. 8. 6.]]
-----------------------------------
[1. 7. 0. 6. 2. 5. 5. 8. 2. 7. 8. 6.]
-----------------------------------
[[1. 7.]
 [0. 6.]
 [2. 5.]
 [5. 8.]
 [2. 7.]
 [8. 6.]]
-----------------------------------
[[1. 0. 2. 5. 2. 8.]
 [7. 6. 5. 8. 7. 6.]]

矩阵拼接

import numpy as np

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

print("----------------------------------")
b = np.floor(10*np.random.random((2,2))) 
print(b)

print("----------------------------------")
# 矩阵纵拼接
print(np.vstack((a,b)))

print("----------------------------------")
# 矩阵横拼接
print(np.hstack((a,b)))

运算结果

[[0. 8.]
 [1. 0.]]
----------------------------------
[[9. 1.]
 [1. 1.]]
----------------------------------
[[0. 8.]
 [1. 0.]
 [9. 1.]
 [1. 1.]]
----------------------------------
[[0. 8. 9. 1.]
 [1. 0. 1. 1.]]
矩阵切分
import numpy as np

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

print("----------------------------------")
# 把矩阵a 横向平均切成3个矩阵 (按列切分)
print(np.hsplit(a, 3))

print("----------------------------------")
# 在某个位置切分,在第3列之后 和 第4列之后 切分
print(np.hsplit(a, (3,4)))

print("----------------------------------")
a = np.floor(10*np.random.random((12, 2))) 
print(a)
print("----------------------------------")
# 把矩阵a 纵向平均切成3个矩阵 (按列行分)
print(np.vsplit(a, 3))
运算结果
[[1. 5. 4. 3. 6. 8. 2. 6. 3. 3. 8. 7.]
 [8. 5. 5. 8. 9. 8. 3. 3. 3. 1. 1. 0.]]
----------------------------------
[array([[1., 5., 4., 3.],
       [8., 5., 5., 8.]]), array([[6., 8., 2., 6.],
       [9., 8., 3., 3.]]), array([[3., 3., 8., 7.],
       [3., 1., 1., 0.]])]
----------------------------------
[array([[1., 5., 4.],
       [8., 5., 5.]]), array([[3.],
       [8.]]), array([[6., 8., 2., 6., 3., 3., 8., 7.],
       [9., 8., 3., 3., 3., 1., 1., 0.]])]
----------------------------------
[[5. 6.]
 [9. 8.]
 [0. 3.]
 [0. 3.]
 [7. 3.]
 [4. 9.]
 [0. 8.]
 [4. 8.]
 [5. 6.]
 [7. 0.]
 [4. 5.]
 [2. 2.]]
----------------------------------
[array([[5., 6.],
       [9., 8.],
       [0., 3.],
       [0., 3.]]), array([[7., 3.],
       [4., 9.],
       [0., 8.],
       [4., 8.]]), array([[5., 6.],
       [7., 0.],
       [4., 5.],
       [2., 2.]])]

矩阵赋值

import numpy as np
a = np.arange(12)
# 变量赋值, a 和 b 指向同一对象
b = a

# 判断 b 和 a 是不是相等的
print( b is a)

# 打印 b 的行列数
print(b.shape)

# 把 b 的行列数修改成3,4
b.shape = 3,4

# 打印a的行列数
print(a.shape)

# 查看 a, b 的id
print(id(a))
print(id(b))

运算结果

True
(12,)
(3, 4)
154867712
154867712

矩阵拷贝

import numpy as np

a = np.arange(12)
print(a)

print("--------------------------------------------")
# view 浅拷贝,创建一个新的对象,指向不一样, 但是共用一组数据
c = a.view()
print(c)
print(c is a)
print("--------------------------------------------")
c.shape = 2,6
print(c)
print(a.shape)
print("--------------------------------------------")
c[0,4] = 1234
print(a)
print("--------------------------------------------")
print(c)
print("--------------------------------------------")
print(id(a))
print(id(c))
运算结果

[ 0  1  2  3  4  5  6  7  8  9 10 11]
--------------------------------------------
[ 0  1  2  3  4  5  6  7  8  9 10 11]
False
--------------------------------------------
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]]
(12,)
--------------------------------------------
[   0    1    2    3 1234    5    6    7    8    9   10   11]
--------------------------------------------
[[   0    1    2    3 1234    5]
 [   6    7    8    9   10   11]]
--------------------------------------------
154881760
154881920
import numpy as np

a = np.arange(12)
a.shape = 3,4
print(a)

print("--------------------------------------------")
# 完全拷贝,不共用数据
d = a.copy()
print(d is a)
d[0,0] = 99
print(d)
print("--------------------------------------------")
print(a)

运算结果

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
--------------------------------------------
False
[[99  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
--------------------------------------------
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
import numpy as np

a = np.arange(0, 40, 10)
print(a)

print("-----------------------------------------------------------")
# 将矩阵扩大 
b = np.tile(a, (2,2))
print(b)
运算结果
[ 0 10 20 30]
-----------------------------------------------------------
[[ 0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30]]

矩阵获取最大值

import numpy as np
data = np.sin(np.arange(20)).reshape(5,4)
print(data)

print("---------------------------------------------------")
# argmax 返回最大值的索引值 index,下面按列返回
ind = data.argmax(axis=0)
print(ind)

print("---------------------------------------------------")
# 打印出每一列的最大值
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]
---------------------------------------------------
4
[0.98935825 0.84147098 0.99060736 0.6569866 ]

矩阵排序

import numpy as np

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

# 排序

print("-------------------------------------------")
# 按行排序
b = np.sort(a, axis=1)
print(b)

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

print("-------------------------------------------")
a = np.array([4,3,1,2])
# 返回排序后的索引,从小到大排序
j = np.argsort(a)
print(j)

print("-------------------------------------------")
print(a[j])
运算结果

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值