#Python basics (#Numpy 2/2)

This is a continuation of the previous article. #Python basics (#Numpy 1/2) The environment uses the environment created in the previous article. → Building Anaconda python environment on Windows 10

1.Numpy shape conversion reshape

Convert a 1-by-6 array to a 2-by-3 array

import numpy as np

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

Execution result


[[0 1 2]
 [3 4 5]]

By setting the argument of reshape to -1, you can convert an array of any shape to a one-dimensional array.

import numpy as np

c = np.array([[[0, 1, 2],
                   [3, 4, 5]],
                  
                  [[5, 4, 3],
                   [2, 1, 0]]])  #Create a 3D array of NumPy from a triple list

print(c)
print("--------------------------")
d = c.reshape(-1)
print(d)

Execution result


[[[0 1 2]
  [3 4 5]]

 [[5 4 3]
  [2 1 0]]]
--------------------------
[0 1 2 3 4 5 5 4 3 2 1 0]

2. Access to elements

Access to each element of ndarray specifies the index as well as list.

One-dimensional array


import numpy as np

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

Multidimensional array


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

print(b[1, 2])  # b[1][2]Same as
print(b[1][2])
# 5
# 5
import numpy as np

def func_a(x):
    y = x * 2 + 1
    return y

a = np.array([[0, 1, 2],
              [3, 4, 5]])  #A two-dimensional array
b = func_a(a)  #Pass an array as an argument

print(b)

Execution result


[[ 1  3  5]
 [ 7  9 11]]

3.sum, average, max, min

import numpy as np

a = np.array([[0, 1, 2],
              [3, 4, 5]])  #A two-dimensional array

print("sum : ",np.sum(a))
print("average : ",np.average(a))
print("max : ",np.max(a))
print("min : ",np.min(a))

Execution result


sum :  15
average :  2.5
max :  5
min :  0

4.axis Specify the direction and calculate

import numpy as np

b = np.array([[0, 1, 2],
              [3, 4, 5]])  #A two-dimensional array

print('axis=0 : ',np.sum(b, axis=0))  #Total in the vertical direction
print('axis=1 : ',np.sum(b, axis=1))  #Total in the horizontal direction

Execution result


axis=0 :  [3 5 7]
axis=1 :  [ 3 12]

Recommended Posts

#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
Python #Numpy basics
Python basics 8 numpy test
Python basics ⑤
NumPy basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
My Numpy (Python)
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (functions)
Python array basics
Python profiling basics
Python basics: functions
#Python basics (class)
Python basics summary
[Python] Numpy memo
Python and numpy tips
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
Errbot: Python chatbot basics
[Python] Search (NumPy) ABC165C
python numpy array calculation
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
[Python] Sorting Numpy data
Basics of python: Output
Python Basic --Pandas, Numpy-
Python / Numpy np.newaxis thinking tips
Convert numpy int64 to python int
Python
[Python] Calculation method with numpy
Implemented SMO with Python + NumPy
Matrix multiplication in python numpy
python: Basics of using scikit-learn ①
Create a python numpy array
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
[Python] Numpy reference, extraction, combination
Python3 | Getting Started with numpy
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
SNS Python basics made with Flask
Subscript access to python numpy array
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Put python, numpy, opencv3 in ubuntu14