Python3 | Getting Started with numpy

** Development environment: Jupyter Notebook **

First, read.

import numpy as np

It means "load a library called numpy as np". By declaring this, the method can be referred to as np.

Array

qiita_header.png

One-dimensional array

a = np.array([0, 1, 2])

print(a)
#array([0, 1, 2])

a[2]
#2

Create an array with np.array. In the case of a one-dimensional array, it can be created with array ([]). Any value can be called with a [].

A two-dimensional array

a = np.array([[0, 1, 2], [3, 4, 5]])

print(a)
#array([[0, 1, 2],
#       [3, 4, 5]])

a[0]
#array([0, 1, 2])
a[1]
#array([3, 4, 5])

a[0][0]
#0
a[1][2]
#5

In the case of a two-dimensional array, it can be created with array ([], []). Here, a [] calls the value for each row. With a [] [], you can specify a row and a column to retrieve a single value.

Array manipulation

qiita_header (1).png

shape, ndim, dtype, size

a.shape
#(2, 3)
a.ndim
#2
a.dtype
#'int64'
a.size
#6

You can retrieve the number of rows and columns with ** a.shape **. ** a.ndim ** represents the number of dimensions. In this case, 2 is taken out because it is a two-dimensional matrix. ** a.dtype ** represents the number of bits. This time, it is a 2x3 matrix, so if one value is 2 bits, 2 to the 6th power is calculated as int64. ** a.size ** is displayed as 6 from the 2x3 matrix.

arange, linspace

np.arange(6)
#array([0, 1, 2, 3, 4, 5])

np.arange(0, 20, 5)
#array([0, 5, 10, 15])

np.linspace(0, 20, 5)
#array([0., 5., 10., 15., 20.])

** np.arange ** can make arrays with arithmetic progression. In the above example, simply writing 6 will extract 0 to 6 elements. Alternatively, it is represented as an array starting from 0 and increasing by 5 from 0 to less than 20. ** np.linspace ** can be divided into equal parts to create an array. In the above example, the array is created by dividing it into 5 equal parts, starting from 0 and ending with 20.

zeros, ones

np.zeros((2, 3))
#array([[0., 0., 0.],
#       [0., 0., 0.]])

np.ones((2, 3))
#array([[1., 1., 1.],
#       [1., 1., 1.]])

** np.zeros ** can create an array with 0 as an element. Similarly, ** np.ones ** has 1 as an element.

append, vstack, hstack

a = np.arange(5)
b = np.arange(0, 10, 2)
c = np.arange(0, 100, 20)

a
#array([0, 1, 2, 3, 4])
b
#array([0, 2, 4, 6, 8])
c
#array([0, 20, 40, 60, 80])

np.append(a, b)
#array([0, 1, 2, 3, 4, 0, 2, 4, 6, 8])

np.vstack([a, b, c])
#array([[0,  1,  2,  3,  4],
#       [0,  2,  4,  6,  8],
#       [0, 20, 40, 60, 80]])

np.hstack([a, b, c])
#array([0,  1,  2,  3,  4,  0,  2,  4,  6,  8,  0, 20, 40, 60, 80])

** np.append ** can be followed by array values. ** np.vstack ** allows you to arrange the array row by row, that is, vertically. In addition, ** np.hstack ** can arrange arrays horizontally (horizontal).

random, sum, min, max, mean

a = np.random.rand((2, 3))

a
#array([[0.3026967 , 0.7533224 , 0.26057491],
#       [0.91502592, 0.54166851, 0.60867887]])

a.sum()
# 3.381967315194162

a.min()
# 0.26057491207931693

a.max()
# 0.9150259161906477

a.mean()
# 0.563661219199027

** np.random.rand ** can randomly describe numbers from 0 to less than 1. ** a.sum ** is the sum of all values, ** a.min ** is the minimum value of the array, ** a.max ** is the maximum value of the array, ** a.mean ** is the minimum value of the array Represents the average value.

Recommended Posts

Python3 | Getting Started with numpy
1.1 Getting Started with Python
Getting Started with Python
Getting Started with Numpy
Getting Started with Python Functions
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (5)
Getting Started with Python responder v2
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Python Basics of Python
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Django 1.11 started with Python3.6
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Golang 2
Getting Started with Python Web Scraping Practice
Getting started with apache2
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Python Web Scraping Practice
Getting Started with Golang 3
Getting started with Spark
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Getting started with Python with 100 knocks on language processing
Getting started with AWS IoT easily in Python
Materials to read when getting started with Python
Settings for getting started with MongoDB in python
Translate Getting Started With TensorFlow
Getting Started with Tkinter 2: Buttons
Getting Started with Go Assembly
[Python] Calculation method with numpy
Implemented SMO with Python + NumPy
Get started with Python! ~ ② Grammar ~
Getting Started with Django with PyCharm
Getting Started with python3 # 2 Learn about types and variables
Getting Started with Google App Engine for Python & PHP
started python
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Get started with Python in Blender
Getting Started with CPU Steal Time
Getting Started with python3 # 3 Try Advanced Computations Using Import Statements
Getting Started with Mathematics Starting with Python Programming Challenges Personal Notes-Problem 1-1
My Numpy (Python)
FizzBuzz with Python3
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Scraping with Python
Statistics with python
Getting Started with Flask with Azure Web Apps
Scraping with Python