Datawhale组队学习—Pandas(Task01))

1、列表推导式
2、匿名函数
匿名函数其实就是函数

## 原始函数
def func(number):
return 2*x
## 等价于匿名函数
func = lambda x:2*x

## 调用时直接一行
func(3)
## 再简洁一点
lambda x:2*x(3)

reshape直接把多维数组转化为一维数组

target.reshape(-1)

reshape

target = np.arange(8).reshape(2,4)
Out[64]: 
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
## 横着读取,横着填充
target.reshape((4,2), order='C') 
Out[65]: 
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])
## 竖着读取,竖着填充
target.reshape((4,2), order='F') 
Out[66]: 
array([[0, 2],
       [4, 6],
       [1, 3],
       [5, 7]])

np.ix_使用布尔索引,np.ix_([行],[列])

target
Out[72]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
target[np.ix_([True, False, True], [True, False, True])]
array([[0, 2],
       [6, 8]])

target[np.ix_([1,2], [True, False, True])]
Out[75]: 
array([[3, 5],
       [6, 8]])

索引
nonzero, argmax, argmin

累乘、累加、做差
cumprod, cumsum, diff

常用的统计函数包括 max, min, mean, median, std, var, sum, quantile ,其中分位数计算是全局方法;对于含有缺失值的数组,它们返回的结果也是缺失值,如果需要略过缺失值,必须使用 nan* 类型的函数,上述的几个统计函数都有对应的 nan* 函数

target = np.array([1, 2, np.nan])
target.max() ## output is nan
target.nanmax() ## output is 2

协方差与相关系数
cov & corrcoef

统计函数的 axis 参数

广播机制

总是扩充为最大维度

向量内积

练习题:
NO.1


image.png

解答如下:


image.png

NO.2
image.png

利用A矩阵,构造C矩阵(3*1)
image.png

其中C是我做得,C_answer是答案所得,用了sum(1)按行相加,并且加上了reshape,的确变简单了好多。。。。
这里同时让我们看一下sum(axis = 1,keepdims = True)和sum(1)的区别,加上了keepdims, 会保留原来的维度


image.png

NO3:
image.png

我的解法:
image.png

NO4:
image.png

我的解法:其实是少了一个循环,同时通过计算速度可以看到,计算速度高了很多
image.png

N05:
image.png

我的解法~竟然和答案是一样的!!!!


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容