#Python basics (#Numpy 1/2)

1.Numpy Numpy: Useful for working with multidimensional arrays in Python extension modules

I always have less memory than people, is that? How about this? In the state Every time I fall into it, I search with the same search word, so I will make a note of it on this occasion.

The environment uses the environment created in the previous article. → Preparing for python development on Windows 10

2.list to Numpy

How to convert from a python list to a Numpy array

import numpy as np

a = np.array([0, 1, 2, 3, 4, 5])  #Create an array of NumPy from a Python list
print(a) 

Execution result


[0 1 2 3 4 5]
<class 'numpy.ndarray'>

Get / confirm object type: type () function

print(type(a))

Execution result



<class 'numpy.ndarray'>

3.lit to Numpy multidimensional array

import numpy as np

b = np.array([[0, 1, 2], [3, 4, 5]])  #Create a two-dimensional array of NumPy from a double list
print(b)

Execution result


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

ndarray shape: shape

print(b.shape) #Get the shape as a tuple with shape (number of rows, number of columns)

Execution result


(2, 3)

4. Array operation 1

import numpy as np

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

print(a)

#[[0 1 2]
# [3 4 5]]
print(a + 10) #Add 10 to each element

# [[10 11 12]
#  [13 14 15]]
print(a * 10) #Multiply each element by 10

#[[ 0 10 20]
# [30 40 50]]

5. Array operation 2

Arithmetic between arrays

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

print(b)
print("--------------")
print(c)
print("--------------")
print(b + c)
print("--------------")
print(b * c)

Execution result


[[0 1 2]
 [3 4 5]]
--------------
[[2 0 1]
 [5 3 4]]
--------------
[[2 1 3]
 [8 7 9]]
--------------
[[ 0  0  2]
 [15 12 20]]

Recommended Posts

#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
Python #Numpy basics
Python basics ⑤
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 CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
#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 basics ② for statement
Python: Unsupervised Learning: Basics
Basics of Python scraping 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
Introduction to Python Numerical Library NumPy
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Put python, numpy, opencv3 in ubuntu14