参考:
https://blog.csdn.net/Hym_eric/article/details/84794012?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_paramhttps://blog.csdn.net/u014159143/article/details/80307717Python中
x[m,n]是通过numpy库引用数组或矩阵中的某一段数据集的一种写法,m代表第m维,n代表m维中取第几段特征数据。
通常用法:
x[:,n]或者x[n,:]
X[:,0]表示对一个二维数组,取该二维数组第一维中的所有数据,第二维中取第0个数据。 X[0,:]使用类比前者。
举例说明:
x[:,0]: import numpy as np X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]]) print X[:,0]输出结果是:
x[:,1]: import numpy as np X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]]) print X[:,1]输出结果是:
x[1,:]: 即取第一维中下标为1的元素的所有值,输出结果为:扩展用法: x[:,m:n],即取所有数据集的第m到n-1列数据
例:输出X数组中所有行第1到2列数据
X = np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14],[15,16,17],[18,19,20]]) print X[:,1:3]输出结果为:
举例说明: #!usr/bin/env python #encoding:utf-8 from __future__ import division import numpy as np def simple_test(): ''' 简单的小实验 ''' data_list=[[1,2,3],[1,2,1],[3,4,5],[4,5,6],[5,6,7],[6,7,8],[6,7,9],[0,4,7],[4,6,0],[2,9,1],[5,8,7],[9,7,8],[3,7,9]] # data_list.toarray() data_list=np.array(data_list) print 'X[:,0]结果输出为:' print data_list[:,0] print 'X[:,1]结果输出为:' print data_list[:,1] print 'X[:,m:n]结果输出为:' print data_list[:,0:1] data_list=[[[1,2],[1,0],[3,4],[7,9],[4,0]],[[1,4],[1,5],[3,6],[8,9],[5,0]],[[8,2],[1,8],[3,5],[7,3],[4,6]], [[1,1],[1,2],[3,5],[7,6],[7,8]],[[9,2],[1,3],[3,5],[7,67],[4,4]],[[8,2],[1,9],[3,43],[7,3],[43,0]], [[1,22],[1,2],[3,42],[7,29],[4,20]],[[1,5],[1,20],[3,24],[17,9],[4,10]],[[11,2],[1,110],[3,14],[7,4],[4,2]]] data_list=np.array(data_list) print 'X[:,:,0]结果输出为:' print data_list[:,:,0] print 'X[:,:,1]结果输出为:' print data_list[:,:,1] print 'X[:,:,m:n]结果输出为:' print data_list[:,:,0:1] if __name__ == '__main__': simple_test()输出结果为:
X[:,0]结果输出为: [1 1 3 4 5 6 6 0 4 2 5 9 3] X[:,1]结果输出为: [2 2 4 5 6 7 7 4 6 9 8 7 7] X[:,m:n]结果输出为: [[1] [1] [3] [4] [5] [6] [6] [0] [4] [2] [5] [9] [3]] X[:,:,0]结果输出为: [[ 1 1 3 7 4] [ 1 1 3 8 5] [ 8 1 3 7 4] [ 1 1 3 7 7] [ 9 1 3 7 4] [ 8 1 3 7 43] [ 1 1 3 7 4] [ 1 1 3 17 4] [11 1 3 7 4]] X[:,:,1]结果输出为: [[ 2 0 4 9 0] [ 4 5 6 9 0] [ 2 8 5 3 6] [ 1 2 5 6 8] [ 2 3 5 67 4] [ 2 9 43 3 0] [ 22 2 42 29 20] [ 5 20 24 9 10] [ 2 110 14 4 2]] X[:,:,m:n]结果输出为: [[[ 1] [ 1] [ 3] [ 7] [ 4]] [[ 1] [ 1] [ 3] [ 8] [ 5]] [[ 8] [ 1] [ 3] [ 7] [ 4]] [[ 1] [ 1] [ 3] [ 7] [ 7]] [[ 9] [ 1] [ 3] [ 7] [ 4]] [[ 8] [ 1] [ 3] [ 7] [43]] [[ 1] [ 1] [ 3] [ 7] [ 4]] [[ 1] [ 1] [ 3] [17] [ 4]] [[11] [ 1] [ 3] [ 7] [ 4]]] 补充: X[: : m]代表了[开始:结束:步进],步进默认为 1: In [19]: string = 'python' In [20]: string[::1] # 步进为1 Out[20]: 'python' In [21]: string[::2] # 步进为2, [0, 0+2, 0+2+2...] Out[21]: 'pto'X[: : -1]自然而然就想到了是将列表或字符倒过来:
python列表如何逆向地访问值?
比如说我有一个list = [1,2,3,4,5,6,7,7,8] 我想访问从倒数第一位到倒数第三位怎么做到? 我想要的输出效果应该是[8,7,7]:
使用切片就可以了。
list[::-1][:3] 或者 list[-3:][::-1]list[::-1]是将列表反过来,一种是先反过来,然后取前三位;一种是先取后三位,再反过来。
>>> list = [1,2,3,4,5,6,7,7,8] >>> list[::-1][:3] [8, 7, 7] >>> list[-3:][::-1] [8, 7, 7]