0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

#1 Numpyの使用方法 (個人用)

Posted at
  1. Numpyの使用方法。
numpy_example.py
# Jupyterを使用
import numpy as np

my_list1 = [1, 2, 3, 4]
my_array1 = np.array(my_list1)
my_array1 #Out: array([1, 2, 3, 4])

my_list2 = [11, 22, 33, 44]
my_lists = [my_list1, my_list2]
my_lists #Out: [[1, 2, 3, 4,], [11, 22, 33, 44]]

my_array2 = np.array(my_lists)
my_array2 #Out: array([[1, 2, 3, 4], [11, 22, 33, 44]])

my_array2.shape #Out: (2, 4)
# 'shape'属性は二行四列がタプルで入っている

my_array2.dtype #Out: dtype('int64')

np.zeros(5) #Out: array([0, 0, 0, 0, 0])
# => my_zeroes = np.zeroes(5)

np.ones((5, 5))
# Out: array([[1., 1., 1., 1., 1.],
#             [1., 1., 1., 1., 1.],
#             [1., 1., 1., 1., 1.],
#             [1., 1., 1., 1., 1.],
#             [1., 1., 1., 1., 1.]])

np.eye(5) # 単位行列の作成
# Out: array([[1., 0., 0., 0., 0.],
#             [0., 1., 0., 0., 0.],
#             [0., 0., 1., 0., 0.],
#             [0., 0., 0., 1., 0.],
#             [0., 0., 0., 0., 1.]])

np.arange(5)
# Out: array([0, 1, 2, 3, 4])

np.arange(5, 50, 2)
# Out: array([ 5,  7,  9, 11, 13,  15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49])

#もし5/2の出力が'2'ならば、以下をインポート。もし'2.5'ならば必要なし。 
from __future__ import division

##########################################################################
arr1 = np.array([[1, 2, 3, 4], [8, 9, 10, 11]])

arr1 * arr1
#Out: array([[  1,   4,   9,  16], [ 64,  81, 100, 121]])

arr1 - arr1
#Out: array([[0, 0, 0, 0], [0, 0, 0, 0]])

1 / arr1
#Out: array([[1.        , 0.5       , 0.33333333, 0.25      ], 
#           [0.125     , 0.11111111, 0.1       , 0.09090909]])

arr1 ** 3 # 3乗
#Out: array([[   1,    8,   27,   64],
#            [ 512,  729, 1000, 1331]])

##################################################################

arr = np.arange(0, 11)

arr[1:5] #Out: array([0, 1, 2, 3, 4])

arr[0:5] = 100 #Out: array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])

slice_arr = arr[0:6]
slice_arr #Out: array([0, 1, 2, 3, 4, 5])
slice_arr[:] = 99
slice_arr #Out: array([99, 99, 99, 99, 99, 99])

arr_copy = arr.copy() #arrをコピーする

arr_2d = np.array([[5, 10, 15],[20, 25, 30], [35, 40, 45]])  #二次元array
arr_2d
#Out: array([[ 5, 10, 15],
#            [20, 25, 30],
#            [35, 40, 45]])

arr_2d[1, 0] #Out: 20

arr_2d[:2, 1:]
#Out: array([[10, 15],
#            [25, 30]])

arr2d = np.zeros((10, 10))

arr_length = arr2d.shape[1]
for i in range(arr_length):
    arr2d[i] = i

# Out:array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#            [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
#            [2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
 #           [3., 3., 3., 3., 3., 3., 3., 3., 3., 3.],
#            [4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
#            [5., 5., 5., 5., 5., 5., 5., 5., 5., 5.],
#            [6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
#            [7., 7., 7., 7., 7., 7., 7., 7., 7., 7.],
#            [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],
#            [9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]])

################################################################
arr = np.arange(9).reshape((3, 3))
arr
#Out: array([[0, 1, 2],
#            [3, 4, 5],
#            [6, 7, 8]])

arr.T # Transpose
#Out: array([[0, 3, 6],
#            [1, 4, 7],
#            [2, 5, 8]])

arr.swapaxes(0, 1)
#Out: array([[0, 3, 6],
#            [1, 4, 7],
#            [2, 5, 8]])

np.dot(arr.T, arr) #行列の掛け算
#Out: array([[45, 54, 63],
#            [54, 66, 78],
#            [63, 78, 93]])

arr3d = np.arange(12).reshape((3, 2, 2))
arr3d
#Out: array([[[ 0,  1],
#             [ 2,  3]],

#            [[ 4,  5],
#             [ 6,  7]],

#            [[ 8,  9],
#             [10, 11]]])

arr3d.transpose((0, 2, 1))
#Out: array([[[ 0,  2],
#             [ 1,  3]],

#            [[ 4,  6],
#             [ 5,  7]],

#            [[ 8, 10],
#             [ 9, 11]]])

#############################################################

arr = np.arange(11)
np.sqlt(arr) #平方根の計算

np.exp(arr) #exponentialの計算

A = np.random.randn(10) #正規分布に従う乱数を生成

B = np.random.randn(10)

np.add(A, B) #足し算

np.maximum(A, B) #A,Bの各要素の最大値を取り出す

##############################
### Arrayを使用したデータ処理 ####
##############################

import matplotlib.pyplot as plt
%matplotlib inline

points = np.arange(-5, 5, 0.01)

dx, dy = np.meshgrid(points, points)
dx
#Out: array([[-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
#            [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
#            [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
#            ...,
#            [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
#            [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
#            [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99]])

dy
#Out: array([[-5.  , -5.  , -5.  , ..., -5.  , -5.  , -5.  ],
#            [-4.99, -4.99, -4.99, ..., -4.99, -4.99, -4.99],
#            [-4.98, -4.98, -4.98, ..., -4.98, -4.98, -4.98],
#            ...,
#            [ 4.97,  4.97,  4.97, ...,  4.97,  4.97,  4.97],
#            [ 4.98,  4.98,  4.98, ...,  4.98,  4.98,  4.98],
#            [ 4.99,  4.99,  4.99, ...,  4.99,  4.99,  4.99]])


A = np.array([1, 2, 3, 4])
B = np.array([1000, 2000, 3000, 4000])

condition = np.array([True, True, False, False])

answer = [(a if cond else b) for a, b, cond in zip(A, B, condition)]
answer
#Out: [1, 2, 3000, 4000]

answer2 = np.where(condition, A, B)
answer2
#Out: array([1, 2, 3000, 4000])

from numpy.random import randn
arr = randn(5, 5) # 正規分布

np.where(arr < 0, 0, arr)

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

arr.sum() #全ての要素の足し算
arr.sum(0) # 0 => 行を示す。
# Out: array([12, 15, 18])

arr.std() #標準偏差

arr.var() #標準偏差の平均

bool_arr = np.array([True, False, True])
bool_arr.any() #1つでもTrueがあれば、Trueを返す。
#Out: True

bool_arr.all() #全てがTrueであるかどうか。
#Out: False

arr = randn(5)
arr.sort() #小さい順に並べる

countries = np.array(['France', 'Japan', 'USA', 'Russia', 'USA', 'Mexico', 'Japan'])
np.unique(countries) #重複を調べる(数字にも使える)
np.in1d(['France', 'USA', 'Sweden'], countries) # 第一引数の各要素がcountries内に存在するかどうか調べる。True/Falseで返す。
#Out: array([True, True, False])

#################################################################

arr = np.arange(5)
np.save('my_array', arr) #バイナリーとして保存
np.load('my_array.npy')
#Out: array([0, 1, 2, 3, 4])

arr2 = np.arange(10)
np.savez('ziparrays.npy', x=arr1, y=arr2)

archive_array = np.load('ziparrays.npy')













0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?