NumPy 数组对象

NumPy 数组对象

NumPy 数组一般是同质的 (但有一种特殊的数组类型例外,它是异质的),即数组中的所有元素类型必须是一致的。这样有一个好处:如果我们知道数组中的元素均为同一类型,该数组所需的存储空间就很容易确定下来。NumPy 数组的下标也是从 0 开始的。

1. Example 1

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
 
import os
import sys
import numpy as np
import tensorflow as tf
 
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
 
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
 
# Beginning with NumPy fundamentals
# Demonstrates the dtype and shape attributes of ndarray.
# Run from the commandline with python arrayattributes.py
 
a = np.arange(5)
print("In: a = arange(5)")
 
print("In: a.dtype")
print(a.dtype)
# Out: dtype('int64')
 
print()
 
print("In: a.shape")
print(a.shape)
# Out: (5,)
 
print()
 
print("In: a")
print(a)
# Out[4]: array([0, 1, 2, 3, 4])
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
In: a = arange(5)
In: a.dtype
int64
 
In: a.shape
(5,)
 
In: a
[0 1 2 3 4]
 
Process finished with exit code 0

2. Example 2

strong@foreverstrong:~$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.arange(5)
>>> a.dtype
dtype('int64')
>>> a.shape
(5,)
>>> a
array([0, 1, 2, 3, 4])
>>> exit()
strong@foreverstrong:~$

这是一个包含 5 个元素的向量,取值分别为 0~4 的整数。数组的 shape 属性返回一个元组 (tuple),元组中的元素即为 NumPy 数组每一个维度上的大小。上面例子中的数组是一维的,因此元组中只有一个元素。

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] (印尼) Ivan Idris (伊德里斯) 著, 张驭宇 译. Python数据分析基础教程:NumPy学习指南 (第2版) [M]. 北京:人民邮电出版社, 2014. 1-226

相关推荐

  1. NumPy 数组对象

    2024-04-06 17:42:04       41 阅读
  2. Numpy】(2)numpy对象和random模块

    2024-04-06 17:42:04       43 阅读
  3. NumPy数组】:深入解析numpy.array()函数

    2024-04-06 17:42:04       32 阅读
  4. numpy数组追加元素

    2024-04-06 17:42:04       61 阅读
  5. Numpy】(1)创建数组

    2024-04-06 17:42:04       38 阅读
  6. 数据分析 — Numpy 数组处理

    2024-04-06 17:42:04       40 阅读
  7. 数据分析NumPy

    2024-04-06 17:42:04       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 17:42:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 17:42:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 17:42:04       87 阅读
  4. Python语言-面向对象

    2024-04-06 17:42:04       96 阅读

热门阅读

  1. 星弟专享面试题汇总(星弟亲自总结)

    2024-04-06 17:42:04       40 阅读
  2. xss介绍及作用

    2024-04-06 17:42:04       41 阅读
  3. ChatGPT Word 大师

    2024-04-06 17:42:04       44 阅读
  4. 网络安全常用命令

    2024-04-06 17:42:04       41 阅读