“深度学习”学习日记。numpy

2022.12.19

import numpy as np

# 引入numpy查看numpy版本
print(np.__version__)
#  1.19.5
print("\n")

# 创建一维数组,内容0-9
arr = np.arange(10)
print(arr)
# [0 1 2 3 4 5 6 7 8 9]
print("\n")

# 创建布尔数组
b = np.full((3, 3), True, dtype=bool)
c = np.ones((3, 3), dtype=bool)
print(c)
print(b)
print("\n")

# 按要求抽选数组中的元素
# 从0到9中,抽出奇数
arr1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a = arr[arr1 % 2 == 1]
print(a)
print("\n")

# 从0到9中抽出偶数
d = arr[arr1 % 2 == 0]
print(d, '\n')  # 这样换行输出宽度为3行

# 修改数组中的元素(原地修改)
# 将0到9的所有奇数改成-1
d = arr[arr1 % 2 == 1] = -1  # 需要注意
print(d, "\n")  # 这样换行输出宽度为1行

# 修改数组中的内容(返回新的数组)
e = np.where(arr % 2 == 1, -1, arr1)
print(e, '\n')

# 改变数组形状
arr2 = arr1.reshape(2, -1)  # -1 表示自动计算他的维度的大小 二维
print(arr2, "\n")

# 合并数值(列方向)
# 将给定数组在列方向合并
arr3 = np.arange(10).reshape(2, -1)  # [0,1,2,3,4,5,6,7,8,9]
arr4 = np.repeat(1, 10).reshape(2, -1)  # [1,1,1,1,1,1,1,1,1,1]
arr5 = np.concatenate([arr3, arr4], axis=0)  # 参数axis的作用:1,在列方向合并;2,在水平方向上和并
arr51 = np.vstack([arr3, arr4])  # arr51 = np.hstack([arr3, arr4]) (水平方向上合并)
arr52 = np.r_[arr3, arr4]  # arr52 = np.c_[arr3, arr4] (水平方向上合并)
print(arr5, "\n")
print(arr51, "\n")
print(arr52, "\n")

# 创建数组
arr6 = np.repeat(1, 10)  # (生成内容, 生成的数量)
print(arr6, '\n')

# 创建随机数组
np.set_printoptions(precision=3)
print(np.random.random(9).reshape(3, 3), '\n')

row1 = np.array([1, 2, 3])
arrage1 = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
newarray = np.r_[np.repeat(row1, 3), np.tile(row1, 3)]
print(newarray, '\n')

try1 = np.tile(row1, 3)  # 将数组row1粘贴3次
print(try1, '\n')

# 返回公共元素
# 去找出两个数组的公共元素(交集)
arr7 = np.random.random(10)
arr8 = np.random.random(10)
arr7uarr8 = np.intersect1d(arr7, arr8)  # 求交集(包括空集)
print(arr7uarr8, '\n')

# 删除元素
# 在两个数组中,删去数组1里面数组2的元素
arr9 = np.setdiff1d(arr3, arr4)  # np.setdiff1d()
print(arr9, '\n')  # [0 2 3 4 5 6 7 8 9]

# 找出两个数组中相同的元素
print(np.where(arr3 == arr4), '\n')

# 按要求抽取元素
# 从数组中抽取大于等于5的元素
f = np.arange(150)
print(np.where((f >= 50) & (f <= 100)), '\n')  # 这样输出会带有比较数据类型
index1 = np.where((f >= 50) & (f <= 100))
print(f[index1], '\n')  # 这样输出不会去比较数据类型
print(np.where(np.logical_and(f >= 100, f <= 150)), '\n')
index2 = np.where(np.logical_and(f >= 100, f <= 150))
print(f[index2], '\n')


# 对比两个数组取其最大值
def maxx(x, y):
    if x > y:
        return x
    else:
        return y


compare = np.vectorize(maxx, otypes=[float])  # 讲函数向量化

print(compare(arr3, arr4), '\n')

# 交换二维数组
# 交换某一行
arraxis22 = np.arange(9).reshape(3, 3)
print(arraxis22[:, [1, 0, 2]], '\n')  # 注意:的位置

# 行交换
print(arraxis22[[1, 0, 2], :], '\n')

# 将一个二维数组反序
print(arraxis22[::-1], '\n')
# python中切片说明arr[start:end:step] step=-1 为逆序输出,
# arr[-1] 输出最后一个元素,
# arr[:-1] 输出除了最后一个元素外的全部元素,
# arr[::-1] 全部逆序输出

# 按要求输出数组
# 数组为小数点的形式打出
np.random.seed(10)  # 当使用seed(num)函数,且给定同一个num值,获取一个随机数,不管执行多少次,得到的均是同一个数
randomarr2 = np.random.random([3, 3]) / 1e3
np.set_printoptions(suppress=True, precision=6)  # suppress 为是否设定小数点
print(randomarr2, '\n')

# 加载特殊矩阵
# 著名的 iris 数据集是包含兰花属性和种类的数据集,其中每行属性有数字和文字,用 numpy 来加载他们。
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')
# 输出前三行
print(iris[:3], '\n')

# 重新定义数组元素范围
# 将 iris 数组集的第一个列的数据范围缩放为 0 到 1
sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])  # delimiter 自定义分隔符
max = sepallength.max()
min = sepallength.min()
S = (sepallength - min) / (max - min)
np.set_printoptions(precision=6)
print(S, '\n')

# 按比例返回元素
# 要求返回按数组中从小到大排序,位置在5%和95%的数
print(np.percentile(S, q=[5, 95]), '\n')

# 找到数组的缺省值
S = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0, 1, 2, 3])
S[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan  # ex:0.      ,0.25    为缺省值
# 缺省值的判断
print(np.isnan(S).any())  # Ture or False
# 对与重复的缺省值不会输出
print(np.isnan(S[:, 0]).sum(), '\n')  # 缺省值的数量
print(np.where(np.isnan(S[:, 0])), '\n')  # 缺省值的位置

#  缺省值的处理
# 替换数值的缺失值
S[np.isnan(S)] = 0
print(S, '\n')

# 去掉所有缺省值
print(S[~np.isnan(S)])

# 返回数值中出现过的所有元素 删去重复的元素
arr10 = [1, 1, 3, 4]
print(np.unique(arr10), '\n')
# numpy.unique(arr, return_index, return_inverse, return_counts)
# arr:输入数组,如果不是一维数组则会展开
# return_index:如果为 true,返回新列表元素在旧列表中的位置(下标),并以列表形式存储。
# return_inverse:如果为true,返回旧列表元素在新列表中的位置(下标),并以列表形式存储。
# return_counts:如果为 true,返回去重数组中的元素在原数组中的出现次数

# 二维数组的排序、根据第一列元素大小
np.random.seed(10)
arraxis23 = np.random.random(100).reshape(2, -1)
print(arraxis23, '\n')
print(arraxis23[:, arraxis23[0].argsort()], '\n')

# 返回出现最频繁的元素
array = [0, 1, 2, 2, 3, 4, 4, 4, 5, 6]
print(np.bincount(array))
print(np.argmax(np.bincount(array)))

# 找去数组中某元素满足第一次大于其下标的元素
print(np.argwhere(S[:, 3].astype(float) > 1)[0], '\n')

# 设定元素上下限
np.set_printoptions(precision=2)
np.random.seed(100)
a = np.random.uniform(1, 50, 20)  # numpy.random.uniform(low,high,size)
# 功能:从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.
np.clip(a, a_min=10, a_max=30)
print(np.where(a < 10, 10, np.where(a > 30, 30, a)))

你可能感兴趣的:(numpy,深度学习,学习)