Chapter1_Python基础
1.1 Python基础
- 推导式: 两个对象之间的映射关系,类似于实数之间关系变换的函数,只不过对象变成了矩阵。
| |
| vacabulary = ['conform', 'deem', 'indispensiable', 'intensify', 'ascribe'] |
| {i: len(i) for i in vacabulary} |
- 匿名函数: 关键字lambda,后紧接形参名,和映射关系以冒号分开
| |
| list(filter(lambda x: sum(1 if i%3 == 0 else 0 for i in x) > 0, my_list)) |
| |
| L3 = ["0", "1", "2", "3", "4"] |
| for l3, l2 in zip(L3, L2): |
| print(l3, l2) |
1.2 NumPy基础
1.2.1 NumPy数组的构造
| np.linspace(闭起始点,闭终止点, 样本个数) |
| np.arange(闭起始点,开终止点,步长) |
| np.zeros((2, 3, 4)) |
| np.ones((2, 1, 2)) |
| np.full((2, 3), 10) |
| np.zeros_like(arr) |
| np.ones_like(arr) |
| np.full_like(arr, [100, 200]) |
| np.eyes(n) |
| np.random.uniform(闭区间起始点, 闭区间终止点, size) |
| np.random.rand(size) |
| np.random.normal(mu, sigma, size) |
| np.random.randn(size) |
| np.random.randint(闭起始点, 开终止点, size) |
| np.random.choice(arr, size, replace=False, p) |
| np.random.permutation(arr) |
1.2.2 NumPy数组的变形
| array = array.transpose(2, 0, 1) |
| array = array.T |
| array = np.swapaxes(array, a, b) |
| my_matrix = np.arange(8).reshape((2, 4), order = 'C') |
| array = np.expand_dims(array, (0, 2)) |
| array = np.squeeze(array, (0, 2)) |
| |
| res = np.stack([pop_man, pop_women], axis=2) |
| res = np.concatenate([pop_1_6, pop_7_12], axis=1) |
| res = np.split(pop_1_6, indices_or_sections=3, axis=1) |
| res = np.split(pop_1_6, indices_or_sections=[1, 4], axis=1) |
| array = np.repeat(array, repeats=2) |
| |
| concadate(),类似于同质性的划分 |
| |
| array = np.zeros((10, 10)) |
| print(array) |
| array[0] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| array[:, 0] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| print(array) |
| |
| |
| |
| |
1.2.3 NumPy数组的切片
| target[0:3, :, 1:3] |
| target[[True, False, True, False], :, :] |
| target[:, np.newaxis, np.newaxis].shape |
1.2.4 广播机制
| |
| 判断是否符合广播机制的条件:从后往前进行比对,如满足一下任一条件则符合广播机制,广播结果的维度以高纬度者为准:①值相等;②值为1. |
1.2.5 常用函数
| max() |
| min() |
| mean() |
| median() |
| std() |
| var() |
| sum() |
| quantile() |
| max() |
| min() |
| mean() |
| median() |
| std() |
| var() |
| sum() |
| quantile() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步