I'm checking paiza's programming skills, but I felt that I had to master the "numpy" array to move from B rank to A rank.
When I checked if there were any good exercises, I found "100 numpy exercises", so I tried it.
100 exercises related to numpy are published on GitHub in Jupyter Notebook format, and you can download them locally to solve the problems.
https://github.com/rougier/numpy-100/
Select "Download ZIP" from the "Clone or download" button on the above page to download the entire file locally.
There is something called "100_Numpy_exercises.ipynb" in it. This is a collection of problems, so let's open it in Jupyter Notebook.
When you read the first description
Run the initialize.py module, then for each question you can query the answer or an hint with hint(n) or answer(n) for n question number.
There is. Of the first cell
100_Numpy_exercises.ipynb
%run initialise.py
It seems that hints are displayed by hint (question number) and answers are displayed by answer (question number).
When I tried to execute it immediately, I got an error saying "There is no module called mdutils", so
I installed a module called mdutils with pip install mdutils
and it worked.
Let's solve it now. First of all, the 1st to 10th questions.
** "Import numpy with the name np" **
This is easy, isn't it?
100_Numpy_exercises.ipynb-(1)answer
import numpy as np
answer(1)
If you write and execute it, the answer will be displayed, so let's match the answers.
** "Show numpy version and settings" **
100_Numpy_exercises.ipynb-(2)answer
print(np.__version__)
np.show_config()
The execution result is displayed as shown below.
100_Numpy_exercises.ipynb-(2)output
1.18.1
blas_mkl_info:
libraries = ['mkl_rt']
library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl\\lib', 'C:/ProgramData/Anaconda3\\Library\\include']
blas_opt_info:
libraries = ['mkl_rt']
library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl\\lib', 'C:/ProgramData/Anaconda3\\Library\\include']
lapack_mkl_info:
libraries = ['mkl_rt']
library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl\\lib', 'C:/ProgramData/Anaconda3\\Library\\include']
lapack_opt_info:
libraries = ['mkl_rt']
library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2019.0.117\\windows\\mkl\\lib', 'C:/ProgramData/Anaconda3\\Library\\include']
What is show_config () used to check ...?
** "Create a one-dimensional array with size 10 and all elements" 0 "" **
You can generate an array with zero elements with np.zeros (size)
.
100_Numpy_exercises.ipynb-(3)answer
Z = np.zeros(10)
print(Z)
Execution result
100_Numpy_exercises.ipynb-(3)output
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
It's OK.
** "How do I know the memory size of an array? 』**
If array is an array, You can see the number of elements in the array with array.size and the number of bytes per element with array.itemsize. Therefore, the memory size of the entire array can be calculated by array.size * array.itemsize.
100_Numpy_exercises.ipynb-(4)answer
Z = np.zeros((10,10))
print("%d bytes" % (Z.size * Z.itemsize))
Execution result
100_Numpy_exercises.ipynb-(4)output
800 bytes
** "How do I see the numpy add function documentation from the command line?" 』**
In the official answer ``%run `python -c "import numpy; numpy.info(numpy.add)"``` It was, but
100_Numpy_exercises.ipynb-(5)answer
np.add?
But you can see it, so I think it's okay here as well.
*"The size is 10, all the elements"0"Make a one-dimensional array. However, the fifth element is"1"To be. 』*
This is an application of Problem 3. First np.zeros()All in"0"After making the array of, the fifth element"1"Rewrite to.
100_Numpy_exercises.ipynb-(6)answer
Z = np.zeros(10)
Z[4] = 1
print(Z)
Execution result
100_Numpy_exercises.ipynb-(6)output
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
"Make an array with elements from 10 to 49"
In other words, you just have to make an arithmetic progression with a tolerance of 1.np.arange()
Is used.
np.arange(start, stop, step)It is how to use. This time start=10、stop=50、step=It will be 1. ※step=If it is 1, step can be omitted.
100_Numpy_exercises.ipynb-(7)answer
Z = np.arange(10,50,1)
print(Z)
100_Numpy_exercises.ipynb-(7)output
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
"Reverse the array(So that the first element is the last)』
Is it also a function that reverses the array? When I thought about it and looked at hint ...
hint: array[::-1]
I see, you can just use slices and reverse the order.
100_Numpy_exercises.ipynb-(8)answer
Z = np.arange(1,10)
print("Z:", Z)
Z_reverse = Z[::-1]
print("Z_reverse:", Z_reverse)
Execution result
100_Numpy_exercises.ipynb-(8)output
Z: [1 2 3 4 5 6 7 8 9]
Z_reverse: [9 8 7 6 5 4 3 2 1]
"3 rows and 3 columns array(Elements are 0-8)Make
First make a one-dimensional array, then reshape()I think it's easy to transform it into 3 rows and 3 columns using.
100_Numpy_exercises.ipynb-(9)answer
Z = np.arange(0,9)
Z = np.reshape (Z, (3,3)) # Z is a 3 × 3 2D array transformation
print(Z)
Execution result
100_Numpy_exercises.ipynb-(9)output
[[0 1 2]
[3 4 5]
[6 7 8]]
『[1,2,0,0,4,0]From the array"Not 0"Find the index of the element ”
np.nonzero(a) #a is array
Returns the index of the non-zero element of the array called a.
100_Numpy_exercises.ipynb-(10)answer
Z = np.array([1,2,0,0,4,0])
print(np.nonzero(Z))
Execution result
100_Numpy_exercises.ipynb-(10)output
(array([0, 1, 4], dtype=int64),)
That's all for now. Next time, I will solve the 11th to 20th questions.
The next article is here. With [100 numpy exercises]"numpy power"Raise the bottom(11th to 20th questions)