/ W3SCHOOLS

W3schools - Python_Numpy Intro

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

NumPy

Is a Python library

Is used for working with arrays

Is short for “Numerical Python”

Aims to provide an array object that is up to 50x faster than traditional Python lists

The array object in NumPy is called ndarray

  • ndarray are stored at one continuous place in memory unlike lists, so processes can access and manipulate then very efficiently

  • This behavior is called locality of reference in computer science

Most of the parts that require fast computation are written in C or C++

Install it using this command pip install numpy

import numpy as np

# Check version
print(np.__version__)

Array

import numpy as np

arr = np.array([1,2,3,4,5])
# np.array((1,2,3,4,5)), Can pass a list, it will be converted into an ndarray
print(arr)	# output [1 2 3 4 5]
print(type(arr))	# output <class ‘numpy.ndarray’>
print(arr.ndim)	# output 1

# Array can have any number of dimentions
arr2 = np.array(arr,ndmin=5)		# output [[[[[1 2 3 4 5]]]]]

# Access Element, Is same as accessing an array element
print(arr[0])	# output 1

# Access elements from 2D arrays
arr3 = np.array([[1,2,3],[4,5,6]])
print(arr[0,1])		# output 2

# Negative indexing
print(arr[1,-1])		# output 6

# Slicing
print(arr[1:3])		# output [2 3]
print(arr[3:])		# output [4 5]
print(arr[-3:-1])	# output [3 4]
print(arr[1:4:2])	# output [2 4]
print(arr[::2])		# output [1 3 5]

# Slicing 2D
print(arr3[1, 1:3])	# output [5 6]
print(arr3[0:2, 2])	# output [3 6]
print(arr3[:3, 1:3])	# output [[2 3] [5 6]]

Data Type

Char Type
i integer
b boolean
u unsigned integer
f float
c complex float
m timedelta
M datetime
O object
S string
U unicode string
V fixed chuck of memory
for other type(void)
import numpy as np

arr = np.array([apple, banana, cherry])
# Check datatype
print(arr.dtype)	# output <U6

# Defined data type
arr2 = np.array([1,2,3,4], dtype=S)
print(arr2)	# output [b‘1’ b‘2’ b‘3’ b‘4’]
print(arr2.dtype)	# output |S1

# i, u, f, S, U can define size, if can not be converted, Error is raised
arr3 = np.array([1.2, 2.3, 3.4, 4.5], dtype=f4)
print(arr3)		# output [1.2 2.3 3.4 4.5]
print(arr.dtype)	# output float32

# Converting data type
arr4 = arr3.astype(i)	# or can use the data type directly like int for integer
print(arr4)		# output [1 2 3 4]
print(arr4.dtype)	# output int32

Copy and View

The main different between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array

import numpy as np

arr = np.array([1,2,3,4,5])
x = arr.copy()
y = arr.view()
arr[0] = 42
print(arr)	# output [42 2 3 4 5]
print(x)	# output [42 2 3 4 5]
print(x.base)	# output [42 2 3 4 5]
print(y)	# output [1 2 3 4 5]
print(y.base)	# output None

Array Shape

Is the number of elements in each dimension

import numpy as np

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

# Returns a tuple with each index having the number of corresponding elements
print(arr.shape)	# output (2,4)

# Can add or remove dimensions or change number of elements in each dimension
newarr = arr.reshape(8)
print(newarr)		# output [1 2 3 4 5 6 7 8]
# It is a view
print(newarr.base)	# output [[1 2 3 4] [5 6 7 8]]
# Unknown Dimension, numpy will calculate this number, can’t pass -1 to more than one dimension
print(arr.reshape(2,2,-1))	# output [[[1 2] [3 4] [5 6] [7 8]]]
# Flattening, converting a multidimentional array into a 1D
print(arr.reshape(-1))		# output [1 2 3 4 5 6 7 8]

Iterating

# Iterate on a n-D array it will go through n-1th dimension one by one
# To return the actual values, the scalars, we have to iterate the arrays in each dimension
import numpy as np

arr = np.array([[[1,2,3],[4,5,6]], [[7,8,9],[10,11,12]]])
for a in arr:
    for b in a:
        for c in b:
            print(z)
# helping function that can be used iterations
# Both are same result
for x in np.nditer(arr):
    print(x)

# op_dtypes arg can pass it the expected datatype to change the datatype of elements while iterating
for x in np.nditer(arr, flags=[buffered], op_dtypes=[S]):
    print(x, end=  )	# output b‘1’ b‘2’ b‘3’
# Numpy doesn’t change the data type of the element in-place,
# So needs some other space(buffer) to change dtype

# Different step
for x in np.nditer(arr[:,::2]):
    print(x, end=  )	# output 1 2 3 7 8 9

# Enumerated iteration
for idx, x in np.ndenumerate(arr):
    print(idx, x, end=  )	# output (0,0,0) 1 (0,0,1) 2 …(1,0,0) 7…(1,1,2) 12

Join

Putting contents of two or more arrays in a single array

If axis is not explicitly passed, it is taken as 0

import numpy as np

arr1 = np.array([[1,2],[3,4]])
arr2 = np.array([[5,6],[7,8]])
arr3 = np.concatenate((arr1,arr2), axis = 1)	# output [[1 2 5 6] [3 4 7 8]]
arr4 = np.concatenate((arr1,arr2))	# output [[1 2] [3 4] [5 6] [7 8]]

# stack : only difference is that stacking is done along a new axis
arr5 = np.stack((arr1,arr2), axis=1)	# output [[[1 2] [5 6]] [[3 4] [7 8]]]

# To stack along rows
arr6 = np.hstack((arr1,arr2))	# output [[1 2 5 6] [3 4 7 8]]

# To stack along columns
arr7 = np.vstack((arr1,arr2))	# output [[1 2] [3 4] [5 6] [7 8]]

# To stack along height
arr8 = np.dstakc((arr1,arr2))	# output [[[1 5] [2 6]] [[3 7] [4 8]]]

Split

Is reverse operation of join, breaks one array into multiple

import numpy as np

arr = np.array([[1,2], [3,4], [5,6], [7,8]])
arr1 = np.array_split(arr,2)	# output [array([[1, 2], [3,4]]), array([[5,6],[7,8]])]
# If the array has less elements than required, it will adjust from the end accordingly
arr2 = np.array_split(arr,3)	# output [array([[1,2], [3,4]]), array([[5,6]]), array([[7,8]])]
# Can specify which axis you want to do the split around
arr3 = np.array_split(arr,2,axis=1)	# output [array([[1], [3], [5], [7]]), array([[2], [4], [6], [8]])]
# An alternate solution is hsplit()
# Similar alternates to vstack() and dstack() are available as vsplit() and dsplit()
import numpy as np

arr = np.array([1,2,3,4,5,4,4])
a = np.where(arr==4)
print(a)	# output (array([3,5,6]),)

# Performs a binary search in the array, and returns the index where the specified value would be inserted to maintain the search order
arr1 = np.array([6,7,8,9])
b = np.searchsorted(arr1, 7)
print(b)	# output 1

# To return the right most index instead
c = np.searchsorted(arr1, 7, side= right)
print(c)		# output 2

# To search for more than one value, use an array with the specified values
d = np.searchsorted(arr1, [1,2,10])
print(d)	# output [0 0 4]

Filter

import numpy as np

arr = np.array([41,42,43,44])
a = [True, False, True, False]
arr1 = arr[a]
print(arr1)	# output  [41 43]

# Create filter directly
filter_arr = arr > 42
arr2 = arr[filter_arr]
print(filter_arr)	# output [False Flase True True]
print(arr2)	# output [43 44]