Introduction to Numpy and Python in Japanese.Intended to programmers interested in Python.The original motivation was to share information inside the lab where I belong.
配列を出力する162018年3月1日 D-WiSE LTC言語❖array_print.c#include <stdio.h>int main(void){int array[10];for(int i = 0; i < 10; i++){array[i] = i;}for(int i = 0; i < 10; i++){printf(“%dn”, array[i]);}return 0;}$ gcc array_print.c$ ./a.out0123456789
17.
配列を出力する172018年3月1日 D-WiSE LTPython❖array_print.pydef main():lst = [i for i in range(10)]for i in lst:print(i)# [print(i) for i in lst]if __name__ == ‘__main__’:main()$ python array_print2.py0123456789
配列を出力する242018年3月1日 D-WiSE LTint型の(一次元の)配列にしか有効ではない❖array_print3.c❖ array_print2.c#include <stdio.h>void print(int *array, int size){printf("[");for(int i = 0; i < size; i++){printf("%d,", array[i]);}printf("b]n");}int main(void){int array[10];for(int i = 0; i < 10; i++){array[i] = i;}print(array, 10);return 0;}#include <stdio.h>#include <stdlib.h>#define ARRAY_SIZE 10typedef struct{int array[ARRAY_SIZE];int size;} Array;void print(Array *array){printf("[");for(int i = 0; i < array->size; i++){printf("%d,", array->array[i]);}printf("b]n");}int main(void){Array *array = (Array*)malloc(sizeof(Array));array->size = 10;for(int i = 0; i < array->size; i++){array->array[i] = i;}print(array);free(array);return 0;}
25.
配列を出力する252018年3月1日 D-WiSE LTPythonのprint()の凄さ#include<stdio.h>void print(int *array, int size){printf("[");for(int i = 0; i < size; i++){printf("%d,", array[i]);}printf("b]n");}int main(void){int array[10];for(int i = 0; i < 10; i++){array[i] = i;}print(array, 10);return 0;}❖ array_print2.pydef main():lst = [i for i in range(10)]print(lst)if __name__ == ‘__main__’:main()❖ array_print2.c
26.
262018年3月1日 D-WiSE LTPythonのlistの凄さ$pythonPython 3.6.2 |Anaconda custom (64-bit)| (default,Jul 20 2017, 13:14:59)[ G C C 4 . 2 . 1 C o m p a t i b l e A p p l e L L V M 6 . 0(clang-600.0.57)] on darwinType "help", "copyright", "credits" or "license"for more information.>>> exit()$✤ 対話型インタープリタの起動簡単な動作確認をすることができるPythonのデバッグのお供
27.
272018年3月1日 D-WiSE LTPythonのlistの凄さ>>>dir(list)[ ' _ _ a d d _ _ ' , ' _ _ c l a s s _ _ ' , ' _ _ c o n t a i n s _ _ ' ,'__delattr__', '__delitem__', '__dir__', '__doc__',' _ _ e q _ _ ' , ' _ _ f o r m a t _ _ ' , ' _ _ g e _ _ ' ,'__getattribute__', '__getitem__', '__gt__','__hash__', '__iadd__', '__imul__', '__init__','__init_subclass__', '__iter__', '__le__','__len__', '__lt__', '__mul__', '__ne__','__new__', '__reduce__', '__reduce_ex__',' _ _ r e p r _ _ ' , ' _ _ r e v e r s e d _ _ ' , ' _ _ r m u l _ _ ' ,'__setattr__', '__setitem__', '__sizeof__','__str__', '__subclasshook__', 'append', 'clear','copy', 'count', 'extend', 'index', 'insert','pop', 'remove', 'reverse', 'sort']✤ 充実した既存の関数群Pythonのデバッグのお供
Numpy302018年3月1日 D-WiSE LTPythonのNumpyの凄さ$pythonPython 3.6.2 |Anaconda custom (64-bit)| (default,Jul 20 2017, 13:14:59)[ G C C 4 . 2 . 1 C o m p a t i b l e A p p l e L L V M 6 . 0(clang-600.0.57)] on darwinType "help", "copyright", "credits" or "license"for more information.>>>✤ 対話型インタープリタの起動
31.
Numpy312018年3月1日 D-WiSE LT>>>import numpy as np✤ importするPythonのライブラリ利用法★ C言語の類似表記:numpyを書くのは長いのでnpと表記する (慣例)#include <stdio.h>#include “myheader.h”★ Pythonの場合は出自を記す必要があるimport numpy as np#numpyで定義されたsum()を使いたいnp.sum()
352018年3月1日 D-WiSE LTnumpyで定義された関数を用いるNumpyimportnumpy as npdef main():#array = np.array([i for i in range(10)])array = np.arange(10)print(array)if __name__ == "__main__":main()np.arange(n): 0~n-1で初期化された一次元配列❖ np_array2.py
502018年3月1日 D-WiSE LTNumpy- 行列演算a =2412335 , b =2445635In [9]: a = np.array([1,2,3])In [9]: b = np.array([4,5,6])In [10]: a + bOut[10]: array([5, 7, 9])加算In [11]: a - bOut[11]: array([-3, -3, -3])減算
51.
512018年3月1日 D-WiSE LTNumpy- 行列演算a =2412335 , b =2445635In [9]: a = np.array([1,2,3])In [9]: b = np.array([4,5,6])In [12]: a * bOut[12]: array([ 4, 10, 18])乗算In [13]: a / bOut[13]: array([ 0.25, 0.4 , 0.5 ])除算
52.
522018年3月1日 D-WiSE LTNumpy- 線形代数np.linalg>>> dir(np.linalg)['LinAlgError', '__builtins__', '__cached__','__doc__', '__file__', '__loader__', '__name__',' _ _ p a c k a g e _ _ ' , ' _ _ p a t h _ _ ' , ' _ _ s p e c _ _ ' ,' _ n u m p y _ t e s t e r ' , ' _ u m a t h _ l i n a l g ' ,'absolute_import', 'bench', 'cholesky', 'cond','det', 'division', 'eig', 'eigh', 'eigvals','eigvalsh', 'info', 'inv', 'lapack_lite', 'linalg',' l s t s q ' , ' m a t r i x _ p o w e r ' , ' m a t r i x _ r a n k ' ,'multi_dot', 'norm', 'pinv', 'print_function','qr', 'slogdet', 'solve', 'svd', 'tensorinv','tensorsolve', 'test']>>>
53.
532018年3月1日 D-WiSE LTNumpy- 線形代数In [14]: c = np.arange(1,5).reshape(2,2)In [15]: np.linalg.det(c)Out[15]: -2.0000000000000004行列式: det cIn [16]: np.linalg.norm(c)Out[16]: 5.4772255750516612ノルム: ||c||c =1 23 4
552018年3月1日 D-WiSE LTNumpy- 乱数生成random (既存ライブラリ)>>> import random>>> dir(random)['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random',' S G _ M A G I C C O N S T ' , ' S y s t e m R a n d o m ' , ' T W O P I ' ,'_BuiltinMethodType', '_MethodType', '_Sequence', '_Set','__all__', '__builtins__', '__cached__', '__doc__','__file__', '__loader__', '__name__', '__package__','__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e','_exp', '_inst', '_itertools', '_log', '_pi', '_random','_sha512', '_sin', '_sqrt', '_test', '_test_generator','_urandom', '_warn', 'betavariate', 'choice', 'choices','expovariate', 'gammavariate', 'gauss', 'getrandbits',' g e t s t a t e ' , ' l o g n o r m v a r i a t e ' , ' n o r m a l v a r i a t e ' ,'paretovariate', 'randint', 'random', 'randrange', 'sample','seed', 'setstate', 'shuffle', 'triangular', 'uniform','vonmisesvariate', 'weibullvariate']
662018年3月1日 D-WiSE LTまとめ1950sFortran1960s BASIC1970s C SQL SmallTalk1980s C++ Common Lisp Perl1990s Python Ruby Haskell2000s C# Go Nim2010s Rust Julia Kotlinプログラミング言語年代記