# -*- coding: utf-8 -*-
import numpy as np
def demo_1():
'''
维度
:return:
'''
a_1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(a_1.shape) # 8
a_2 = np.array([[1], [2], [3], [4], [5], [6], [7], [8]])
print(a_2.shape) # 8, 1
a_3 = np.array([[[1], [2], [3], [4], [5], [6], [7], [8]]])
print(a_3.shape) # (1,8,1)
a_4 = np.array([[[1, 2, 3, 4, 5, 6, 7, 8]]])
print(a_4.shape) # (1,1,8)
b_1 = np.floor(10 * np.random.rand(8, 1)) # 二维数组 由8个1维数组组成 每个一维数组1个元素
b_2 = np.floor(10 * np.random.rand(8, 2)) # 二维数组 由8个1维数组组成 每个一维数组2个元素
c_3 = np.floor(10 * np.random.rand(8, 3, 2)) # 三维数组 由8个2维数组组成 每个二维数组由3个一维数组组成 每个一维数组2个元素
d_4 = np.floor(10 * np.random.rand(5)) # 1维数组 5个元素
def demo_2():
'''
切片
:return:
'''
x = np.array([[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
print(x.shape) # 6,5
print(x[1]) # 取第二行数据 序号从0开始 结果: [11 12 13 14 15]
print("\n")
# 下面四个都是等价的
print(x[:1]) # 截取[0:1) 结果: [[ 6 7 8 9 10]]
print(x[0:1]) # 截取[0:1) 结果: [[ 6 7 8 9 10]]
print(x[0:1, ])
print(x[0:1, :])
print("\n")
# 取某一列数据
print(x[:, 1]) # [ 7 12 17 22 27 32]
# 截取 下面两个是等价的
print(x[:, :1]) # 截取[0:1)列 [[6][11][16][21][26][31]]
print(x[:, 0:1])
print(x[:, 1:3]) # 截取[1,3)列
# 取中间的4行3列的的数据
print(x[1:5, 1:4]) # 截取[1,5)行 [1,4)列
# 按索引切片
# 行 下标 [1 5] 按步长 2切片
print(x[1:5:2]) # 即第2行第4行
# 列
print(x[:, 1:5:2]) # 即第2列第4列
def demo_3():
'''
合并
:return:
'''
x = np.array([[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
a = x[0:1] # 第一行
b = x[5:6] # 第六行
print(np.concatenate((a, b), axis=0)) # 按照0 轴合并 [[ 6 7 8 9 10][31 32 33 34 35]]
print(np.concatenate((a, b), axis=1)) # 按照1 轴合并 [[ 6 7 8 9 10 31 32 33 34 35]]
c = x[:, 1:2] # 第一列
d = x[:, 3:4] # 第4列
print(np.concatenate((c, d), axis=0))
print(np.concatenate((c, d), axis=1))
e = x[1:4, 0:4] # shape 3,4
f = x[1:4, :] # shape (3, 5)
g = x[:, 1:5] # shape (6, 4)
# 合并
print(np.concatenate((e, f), axis=1))
print(np.concatenate((e, g), axis=0))
def demo_4():
'''
排序
功能寥寥
:return:
'''
x = np.array([[6, 7, 11, 9, 10],
[26, 27, 28, 29, 30],
[11, 12, 11, 14, 15],
[16, 12, 18, 19, 20],
[13, 22, 23, 24, 25],
[31, 32, 33, 34, 35]])
x_1 = x.copy()
x_1.sort(axis=0) # 将每一列的数据进行从小到大排序
print(x_1)
print('\n')
print(x_1[:: -1]) # 按列翻转 即从小到大 排列
print('\n')
x_2 = x.copy()
x_2.sort(axis=1) # 将每一行的数据进行从小到大排序
print(x_2)
print(x_2[:, :-1]) # 按行翻转 即从小到大 排列
def demo_5():
'''
常用计算
:return:
'''
x = np.array([[6, 65, 11, 9, 10],
[26, 27, 1, 29, 30],
[11, 12, 11, 4, 15],
[16, 12, 18, 19, 20],
[13, 22, 23, 24, 25],
[31, 32, 33, 34, 35]])
# 每一列中的 最小值
print(np.amin(x, axis=0))
# 每一列中的最大值
print(np.amax(x, axis=0))
# 每一行中的 最小值
print(np.amin(x, axis=1))
# 每一行中的最大值
print(np.amax(x, axis=1))
print("\n")
# 列中极值
print(np.ptp(x, axis=0))
# 行中极值
print(np.ptp(x, axis=1))
# 均值
# 列均值
print(np.mean(x, axis=0))
# 行均值
print(np.mean(x, axis=1))
print(np.amin(x)) # 全局最小值
print(np.amax(x)) # 全局最大值
print(np.mean(x)) # 全局平均值
pass
def demo_6():
'''
大于等于15的值置换成30 小于15的置换成0
:return:
'''
x = np.array([[6, 65, 11, 9, 10],
[26, 27, 1, 29, 30],
[11, 12, 11, 4, 15],
[16, 12, 18, 19, 20],
[13, 22, 23, 24, 25],
[31, 32, 33, 34, 35]])
print(x)
print('\n')
b = np.where(x < 15, 0, x)
b = np.where(b >= 15, 30, b)
print(b)
# 删除3行
print(np.delete(x, 2, axis=0))
print('\n')
# 删除2列
print(np.delete(x, 1, axis=1))
print("\n")
# 删除第三列 第5列
print(np.delete(x, [2, 4], axis=1))
def demo_7():
# 变形
x = np.array([[6, 7, 8, 9, 10], [31, 32, 33, 34, 35]])
print(x.shape) # (2,5)
'''
reshape(1,-1)转化成1行:
reshape(2,-1)转换成两行:
reshape(-1,1)转换成1列:
reshape(-1,2)转化成两列
'''
print(np.reshape(x, newshape=[1, -1])) # 数据降为1维
arr = np.arange(12)
print(arr)
re_1 = np.reshape(arr, newshape=[2, 6])
print(re_1)
# 三维数组 中 有 3个二维数组
# 每个二维数组中 2个 1维数组
# 每个一维数组中 2个元素 最后一个中括号有几个元素
print(np.reshape(arr, newshape=[3, 2, 2]))
#
print(np.reshape(re_1, (6, 2)))
print(np.reshape(re_1, (12, 1)))
if __name__ == '__main__':
demo_7()
numpy的一些常用方法
最新推荐文章于 2024-07-24 23:27:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)