[PYTHON] Getting Started with Numpy

1. Numpy basics

1.1. numpy.ndarray Basics

1.1.1. numpy.ndarray attribute element

numpy.ndarray attribute element Acquisition contents Example 1
np.array([[1,0,0],[0,1,2]])
ndim Number of dimensions 2
shape Array shape (2,3)
size Number of array elements 6
dtype Array element data type int32
T Transpose array np.array([[1 0]
[0 1]
[0 2]])
flags Memory layout
flat One-dimensional (flattened) array generation
Array definition example: np.array(Array variables.flat)
imag Imaginary value array of array elements
real Real part array of array elements
itemsize Array element size (bytes)
Example: int32-> 32/8 =4 bytes
4
nbytes Array size(Part-Time Job) 32
strides Misalignment of adjacent array elements (bytes) (12,4)
Vertical: 12 bytes
->4 bytes3 elements(Lateral direction)
Lateral direction:4バイト
->4 bytes
1 element
ctypes Used in ctypes module
base Referenced array None

Example_1.py


### Library definition
import numpy as np

### Function definition
def print_attribute(input):
    print("")
    for key, value in input.items():
        print(">>> " + str(key))
        print("IN: print("+str(key)+")")
        print("OUT: "+str(value))
        print("")

### Array definition
array = np.array( [[1,0,0],[0,1,2]])

### numpy.ndarray element definition

attribute ={}
attribute['array.ndim'] = array.ndim
attribute['array.shape'] = array.shape
attribute['array.size'] = array.size
attribute['array.dtype'] = array.dtype
attribute['array.T'] = array.T
attribute['array.flags'] = array.flags
attribute['array.flat'] = array.flat
attribute['np.array(array.flat)'] = np.array(array.flat)
attribute['array.imag'] = array.imag
attribute['array.real'] = array.real
attribute['array.itemsize'] = array.itemsize
attribute['array.nbytes'] = array.nbytes
attribute['array.strides'] = array.strides
attribute['array.ctypes'] = array.ctypes
attribute['array.base'] = array.base

### numpy.ndarray element acquisition example
print_attribute(attribute)
"""

>>> array.ndim
IN: print(array.ndim)
OUT: 2

>>> array.shape
IN: print(array.shape)
OUT: (2, 3)

>>> array.size
IN: print(array.size)
OUT: 6

>>> array.dtype
IN: print(array.dtype)
OUT: int32

>>> array.T
IN: print(array.T)
OUT: [[1 0]
 [0 1]
 [0 2]]

>>> array.flags
IN: print(array.flags)
OUT:   C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False


>>> array.flat
IN: print(array.flat)
OUT: <numpy.flatiter object at 0x000001E00DB70A00>

>>> np.array(array.flat)
IN: print(np.array(array.flat))
OUT: [1 0 0 0 1 2]

>>> array.imag
IN: print(array.imag)
OUT: [[0 0 0]
 [0 0 0]]

>>> array.real
IN: print(array.real)
OUT: [[1 0 0]
 [0 1 2]]

>>> array.itemsize
IN: print(array.itemsize)
OUT: 4

>>> array.nbytes
IN: print(array.nbytes)
OUT: 24

>>> array.strides
IN: print(array.strides)
OUT: (12, 4)

>>> array.ctypes
IN: print(array.ctypes)
OUT: <numpy.core._internal._ctypes object at 0x000001E00D84BC50>

>>> array.base
IN: print(array.base)
OUT: None

"""

References

  1. Numpy Official User Guide (Version: 1.18)
  2. Numpy Official Reference (numpy.ndarray)

Recommended Posts

Getting Started with Numpy
Python3 | Getting Started with numpy
Getting started with Android!
1.1 Getting Started with Python
Getting Started with Golang 2
Getting started with apache2
Getting Started with Golang 1
Getting Started with Python
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Golang 3
Getting started with Spark
Getting Started with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Getting Started with Tkinter 2: Buttons
Getting Started with PKI with Golang ―― 4
Getting Started with Python Django (1)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Getting Started with Python Django (5)
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
Getting Started with Python Basics of Python
Getting Started with Cisco Spark REST-API
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with CPU Steal Time
Grails getting started
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Flask with Azure Web Apps
Getting Started with Python Web Scraping Practice
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Getting Started with Lisp for Pythonista: Supplement
Getting Started with Heroku, Deploying Flask App
Getting Started with TDD with Cyber-dojo at MobPro
Getting started with Python with 100 knocks on language processing
Django 1.11 started with Python3.6
MongoDB Basics: Getting Started with CRUD in JAVA
Getting Started with Drawing with matplotlib: Writing Simple Functions
Getting started with Keras Sequential model Japanese translation
[Translation] Getting Started with Rust for Python Programmers
Moving average with numpy
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Get started with MicroPython
Getting started with AWS IoT easily in Python
Get started with Mezzanine
Getting Started with Python's ast Module (Using NodeVisitor)