Python memorandum

This article is a record of learning for python beginners. I mainly write what I didn't understand. So the order is messy. For reference ,,,

NumPy np.array

# -*- coding: utf-8 -*-
import numpy as np

#Declaration / initialization of 2D array
A = np.array([[1, 2],
              [3, 4],
              [5, 6]])

#Matrix size
print("Matrix A size:", A.shape)
print("Number of rows in matrix A:", A.shape[0])
print("Number of columns in matrix A:", A.shape[1])

"""
Matrix A size:(3, 2)
Number of rows in matrix A:3
Number of columns in matrix A:2
"""

np.reshape shape [0] displays ** number of rows **, shape [1] displays ** number of columns **.

import numpy as np

a = np.arange(24)

print(a)
# [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

print(a.shape)
# (24,)

print(a.ndim)
# 1

a_4_6 = a.reshape([4, 6])

print(a_4_6)
# [[ 0  1  2  3  4  5]
#  [ 6  7  8  9 10 11]
#  [12 13 14 15 16 17]
#  [18 19 20 21 22 23]]

print(a_4_6.shape)
# (4, 6)

print(a_4_6.ndim)
# 2

In np.ndim, you can easily find the number of elements. Reshape is as in the above code.

[:, 0] All 0th column. If it is a 2x2 array, if it is written without omission, it will be a [0: 2, 0]. A [a: b, c: d] means to extract columns from c to d (not including b and d) in the rows from a to b.

np.where

import numpy as np

a = np.arange(9).reshape((3, 3))
print(a)
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]

print(np.where(a < 4, -1, 100))
# [[ -1  -1  -1]
#  [ -1 100 100]
#  [100 100 100]]

print(np.where(a < 4, True, False))
# [[ True  True  True]
#  [ True False False]
#  [False False False]]

print(a < 4)
# [[ True  True  True]
#  [ True False False]
#  [False False False]]

If the following is only a condition, true or false will appear.

np.hstack

Consolidation of arrays with the same number of columns. For rows, np.vstack.

np.unique()

In the sequence in (), extract without fog.

import numpy as np

a = np.array([0, 0, 30, 10, 10, 20])
print(a)
# [ 0  0 30 10 10 20]

print(np.unique(a))
# [ 0 10 20 30]

print(type(np.unique(a)))
# <class 'numpy.ndarray'>

Recommended Posts

Python Memorandum 2
Python memorandum
python memorandum
python memorandum
python memorandum
Python memorandum
Python basics memorandum
Python pathlib memorandum
Python memorandum (algorithm)
Python memorandum [links]
Python memorandum numbering variables
python memorandum (sequential update)
Python
Python memorandum (personal bookmark)
Python basic memorandum part 2
[Python] Iterative processing_Personal memorandum
Memorandum @ Python OR Seminar
python memorandum super basic
Effective Python Learning Memorandum Day 15 [15/100]
Cisco Memorandum _ Python config input
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
ABC memorandum [ABC163 C --managementr] (Python)
About python beginner's memorandum function
Memorandum @ Python OR Seminar: matplotlib
[Python] SQLAlchemy error avoidance memorandum
A memorandum about correlation [Python]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Memorandum @ Python OR Seminar: Pulp
Effective Python Learning Memorandum Day 13 [13/100]
A memorandum about Python mock
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Memorandum @ Python OR Seminar: Pandas
[python] Random number generation memorandum
Memorandum @ Python OR Seminar: scikit-learn
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
python parallel / asynchronous execution memorandum
Matplotlib memorandum
kafka python
linux memorandum
Python Summary
Built-in python
ABC memorandum [ABC159 C --Maximum Volume] (Python)
jinja2 memorandum
Python comprehension
Python technique
Python pywin32 (win32com) Excel operation memorandum
Studying python
Python 2.7 Countdown
Django memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo