Python Numpy Tutorials: 数组--2

来源:互联网 发布:云计算一般薪资多少 编辑:程序博客网 时间:2024/06/08 01:25

本文内容:
1. 从二维数组里面提取一维和二维数组,注意Index的区别和用法
2. 区分matlab的用法

# -*- coding: utf-8 -*-"""Python Version: 3.5Created on Thu May 11 10:52:38 2017E-mail: Eric2014_Lv@sjtu.edu.cn@author: DidiLv"""import numpy as np# [[ 1  2  3  4]#  [ 5  6  7  8]#  [ 9 10 11 12]]a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])# 从二维数组的列(第二个维度)里面提取一维数组和二维数组的方法# 一维数组提取没什么问题,直接输入相应的Index,类比matlab# 二维数组一定要注意方式更要注意跟matlab的区分# original array:row_r1 = a[1, :]    # 提取的是一维数组  row_r2 = a[1:2, :]  # 提取的是二维数组,这里注意看下怎样提取第一个维度的方法和下标print(row_r1, row_r1.shape)  # 输出 "[5 6 7 8] (4,)"print(row_r2, row_r2.shape)  # 输出 "[[5 6 7 8]] (1, 4)"# We can make the same distinction when accessing columns of an array:col_r1 = a[:, 1]col_r2 = a[:, 1:2]print(col_r1, col_r1.shape)  # 输出 "[ 2  6 10] (3,)"print(col_r2, col_r2.shape)  # 输出 "[[ 2]                            #          [ 6]                            #          [10]] (3, 1)"
0 0