python&numpy基础———joyful pandas学习笔记

列表推导式

基本形式[* for i in *]

第一个 * 为映射函数,其输入为后面 i 指代的内容,第二个 * 表示迭代的对象。

例如生成5个偶数2 4 6 8 10 

[2*i for i in range(1,6)]

[2, 4, 6, 8, 10]

多层嵌套

第一个 for 为外层循环,第二个为内层循环

例1 

[m+'_'+n for m in ['a', 'b'] for n in ['c', 'd']]

['a_c', 'a_d', 'b_c', 'b_d']

 例2(生成99乘法表)

[str(a)+"x"+str(b)+'='+str(a*b) for a in range(1,10) for b in range(a,10)]

['1x1=1', '1x2=2', '1x3=3', '1x4=4', '1x5=5', '1x6=6', '1x7=7', '1x8=8', '1x9=9', '2x2=4', '2x3=6', '2x4=8', '2x5=10', '2x6=12', '2x7=14', '2x8=16', '2x9=18', '3x3=9', '3x4=12', '3x5=15', '3x6=18', '3x7=21', '3x8=24', '3x9=27', '4x4=16', '4x5=20', '4x6=24', '4x7=28', '4x8=32', '4x9=36', '5x5=25', '5x6=30', '5x7=35', '5x8=40', '5x9=45', '6x6=36', '6x7=42', '6x8=48', '6x9=54', '7x7=49', '7x8=56', '7x9=63', '8x8=64', '8x9=72', '9x9=81']

匿名函数与map

lambda   相当于简单的函数映射

例(简单求和函数)

fun1 = lambda a,b : a+b
fun1(3,5)

8

如果去掉函数名:

(lambda a,b : a+b)(3,5)

map方法

[(lambda x:2*x)(i) for i in range(5)]

用map可写作

list(map(lambda x: 2*x, range(5)))

[0, 2, 4, 6, 8]

zip打包

for循环中:

for i, j in zip(range(5),[4,3,2,1,0]):
    print(i,j)

0 4
1 3
2 2
3 1
4 0

建立字典映射时:

a = [1,2,3]
b = ['one', 'two', 'three']
dict(zip(a, b)) 
{1: 'one', 2: 'two', 3: 'three'}

numpy基础

import numpy as np

np数组:

基础形式:

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

[1 2 3 4 5]

特殊数组:

linspace

arange

np.linspace(0,100,11)#起始值,终值,元素个数

[  0.  10.  20.  30.  40.  50.  60.  70.  80.  90. 100.]

np.arange(1,5,2) # 起始、终止(不包含)、步长

[1 3]

矩阵

np.zeros((2,3)) # 传入元组表示各维度大小
np.empty(3,3)#未进行初始化的3*3矩阵
np.eye(n) # n*n 的单位矩阵
np.full((2,3), 10) # 元组传入大小,10 表示填充数值
np.random.rand(3,3)
#生成3*3的随机矩阵,矩阵值0-1

[[0.86270792 0.90534714 0.40187139]
 [0.634286   0.25515137 0.25612804]
 [0.9526155  0.58756532 0.80411909]]
np.random.randint(low, high, size)
#指定大小,范围的随机矩阵

np.random.randint(5,9,(4,4))

[[8 7 6 7]
 [7 6 6 6]
 [5 7 6 8]
 [5 5 6 5]]

choice抽取元素:

choice 可以从给定的列表中,以一定概率和方式抽取结果

基本形式:

np.random.choice(my_list, n, replace=, p=)
#my list表示所选的列表
#n为选择的个数
#repalce表示是否允许重复
#p为各个选项对应的概率

 例1

np.random.choice(my_list, 2, replace=False, p=[0.1, 0.7, 0.1 ,0.1])

例二

np.random.choice(my_list, (3,3))

[['a', 'c', 'b']
 ['b', 'b', 'd']
 ['d', 'b', 'c']]

permutation随机排列

np.random.permutation(my_list)

['c' 'a' 'b' 'd']

随机种子 

用于固定随机情况

list1 = np.random.rand(3)
print(list1)
list2 = np.random.rand(3)
print(list2)
np.random.seed(0)
list3 = np.random.rand(3)
print(list3)

[0.5488135  0.71518937 0.60276338]
[0.54488318 0.4236548  0.64589411]
[0.5488135  0.71518937 0.60276338]

'''
list1 equal list3
'''

 np数组的相关操作:

转置:

np.zeros((2,3)).T

[[0., 0.],
 [0., 0.],
 [0., 0.]]

矩阵合并:

例1 

np.r_[np.zeros((2,3)),np.zeros((2,3))]

[[0., 0., 0.],
 [0., 0., 0.],
 [0., 0., 0.],
 [0., 0., 0.]]

np.c_[np.zeros((2,3)),np.zeros((2,3))]

[[0., 0., 0., 0., 0., 0.],
 [0., 0., 0., 0., 0., 0.]]

r_ 和 c_ 分别表示上下合并(按行)和左右合并(按列):

数组与矩阵合并时,数组看作列向量

例2

np.c_[np.array([0,0]),np.zeros((2,3))]

[[0., 0., 0., 0.],
 [0., 0., 0., 0.]]

reshape转换大小:

reshape 能够帮助用户把原数组按照新的维度重新排列。在使用时有两种模式,分别为 C 模式和 F 模式,分别以逐行和逐列的顺序进行填充读取。

import numpy as np
np.arange(8).reshape(2,4)

[[0, 1, 2, 3],
[4, 5, 6, 7]]

target.reshape((4,2))

[[0 1]
 [2 3]
 [4 5]
 [6 7]]

 np数组的切片与索引

np.ix_ 布尔索引

 例:

target = np.arange(9).reshape(3,3)
target[np.ix_([True, False, True], [True, False, True])]
#第一个列表表示所取行
#第二个列表表示所取列
#或者表示成:
target[np.ix_([1,3], [True, False, True])]

[[0, 2],
[6, 8]]

start:end:step 切片

二维数组两个参数,分别为行的切片,每行中元素的切片

例:

target = np.arange(9).reshape((3,3))
target[:-1, [0,2]]

[[0 2]
 [3 5]]

常用函数:

shape

返回矩阵的大小

where

条件函数,可以指定满足条件与不满足条件位置对应的填充值

where(条件,满足条件的替代值值,不满足的替代值)

a = np.array(range(5))
np.where(a>=3,a,-1)#a数组中大于等于3的,保持a中元素不变,其他变成-1

[-1 -1 -1  3  4]
np.where(a>=3,1,-1)

[-1 -1 -1  1  1]

nonzero, argmax, argmin

nonzero返回非0索引

argmax返回最大值索引

argmin返回最小值索引

a = np.array(range(5))
a.argmax()

4

统计函数

对于数组:

a = np.array(range(5))
a.max()#返回最大值4
a.min()#返回最小值0
a.mean()#返回平均数2
np.median(a)#中位数2
a.sum()#返回和10

对于矩阵(二维数组):

axis参数可以选定行或者列

axis=0 时结果为列的统计指标,当 axis=1 时结果为行的统计指标

target = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
target.sum(0)

[12, 15, 18]


target.sum(1)

[ 6, 15, 24]

向量&矩阵计算:

向量内积(点乘):

a,b为向量,a·b表示为

a.dot(b)

矩阵乘法

A,B为矩阵,矩阵A乘矩阵B表示为

A@B

相关练习题:

未完...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Freshman小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值