FlyEgle


  • Home

  • About

  • Tags

  • Categories

  • Archives

  • Schedule

  • Sitemap

  • Commonweal 404

numpy of axis

Posted on 2018-03-08

Numpy axis简单解释

在学习python中经常会用到numpy这个科学库,它是用C语言编写的所以对于矩阵的计算速度远远快于python自己的计算方法。这里对于Numpy的安装和介绍就不再提示了,重点说明一下自己对于axis轴的理解。

axis

图中的矩阵是一个shape=(3, 5)的numpy.array, 这里不按照是行还是列的说法。而是按照ndims来说,就是第一个维度和第二个维度。按照索引的顺序,axis=0代表的就是第一个维度的方向,axis=1就是代表第二个维度的方向,依次往下类推。

一个简单的二维数组

1
2
3
4
5
import numpy as np
arr = np.random.random((3, 5))

>>>print(arr.shape)
(3, 5)

数组的维度

1
2
>>>print(arr.ndim)
2

数组求和

1
2
sum_0 = np.sum(arr, axis=0)
sum_1 = np.sum(arr, axis=1)

按照上面的图表示,当axis=0的时候求和的结果是按照row的方向来求和,也就是说是每一列的求和。当axis=1的时候求和的结果就是按照col的方向来求和, 也就是说是每一行的求和。看下面的结果

1
2
3
4
5
6
7
8
9
import numpy as np

array = np.random.random((3, 5))

print(np.sum(array, axis=0))
print(np.sum(array, axis=1))

>>>[1.61948631 1.11130637 1.66794932 1.20041938 1.24456878]
>>>[1.97046523 3.43930555 1.43395938]

三维数组

1
2
3
4
5
array_3 = np.random.randint(0, 5, [2, 3, 2])

print(np.sum(array_3, axis=0).shape)
print(np.sum(array_3, axis=1).shape)
print(np.sum(array_3, axis=2).shape)

三维数组的三个维度,axis=0输出的就是剩下两个维度的结果。同理一样

1
2
3
>>>(3, 2)
>>>(2, 2)
>>>(2, 3)

axis的分析并不难,只是不是通常所记忆的索引问题,所以每次用axis的时候一定要想一想上面的那个图。记住0和1的方向指引,熟能生巧。

集成学习Stacking实现

Posted on 2018-03-05

Stacking

Stacking在机器学习中是一种常用的集成方法,网上有很多的基础教程。我在这里就不说太多了,按照惯例先开始放一个图。

first

Posted on 2018-03-03

你好,这是我个人的技术博客。记载自己一些学习的记录和整理。

Hello World

Posted on 2018-03-03

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

MC Jiang

人生苦短,python当歌

4 posts
2 tags
GitHub
© 2018 MC Jiang
Powered by Hexo
|
Theme — NexT.Muse v6.0.5