떼닝로그

Python for Data Science, AI & Development - Working with Data in Python (3) 본문

Coursera/IBM Data Science

Python for Data Science, AI & Development - Working with Data in Python (3)

떼닝 2024. 1. 24. 00:07

Python for Data Science

Numpy in Python

One Dimensional Numpy

The Basics & Array Creation

List

 

Numpy

 

Basic Operations - Vector Addition and Subtraction

# one-line code
u = np.array([1, 0])
v = np.array([0, 1])
z = u+v
z:array([1, 1])

# multi-line code
u = [1, 0]
v = [0, 1]
z = []

for n, m in zip(u, v):
    z.append(n+m)
# one-line subtraction
u = np.array([1, 0])
v = np.array([0, 1])
z = u-v
z : array([1, -1])

# multi-line subtraction
u = [1, 0]
v = [0, 1]
z = []

for n, m in zip(u, v):
    z.append(n-m)

 

Basic Operations - Array multiplication with a Scalar

# using numpy
y = np.array([1, 2])
z = 2*y
z:array([2, 4])

# without using numpy
y = [1, 2]
z = []

for n in y:
    z.append(2*n)

 

Basic Operations - Product of two numpy arrays

# using numpy
u = np.array([1, 2])
v = np.array([3, 2])
z = u*v
z:array([3, 4])

# wihtout using numpy
u = [1, 2]
v = [3, 2]
z = []

for n, m in zip(u, v):
	z.append(n*m)

 

Basic Operations - Dot Product

 

# with numpy
u = np.array([1, 2])
v = np.array([3, 1])
result = np.dot(u, v)
result : 5

 

Basic Operations - Adding Constant to an numpy Array

Universal Functions

- mean() : 평균값 구해주기

- max() : 최대값 구하기

- min() : 최소값 구하기

- pi : pi값 내장 (3.141592...)

- sin : sin 함수 내장

- linspace(시작 값, 끝 값, num=사이 값 개수) : 시작 값 이상, 끝 값 이하 사이를 일정한 간격으로 num개의 값 목록을 만들어냄

 

Plotting Mathematical Functions

 

Two Dimensional Numpy

 

Reading : Numpy

Key aspects of NumPy in Python:

- Efficient Data Structures : faster and more memory-efficient than Python lists. crucial for handling large datasets

- Multi-Dimensional Arrays : enabling the representation of matrices and tensors.

- Element-Wise Operations : making it easy to perform calculations on entire datasets in one go

- Random Number Generation : useful for simulations and statistical analysis

- Integration with Other Libraries : integrates with other libraries like SciPy, Pandas, and Matplotlib, enhancing its utility in various domains

- Performance Optimization : implemented in low-level languages like C and Fortran, which significantly boosts their performance.

 

Operation with Numpy

- Array Creation : Creating a NumPy array. (ar = np.array([1,2,3,4,5]))

- Element-Wise Arthimetic : Element-wise addition, subtraction, etc. (result = arr1 + arr2)

- Scalar Arthimetic : Scalar addition, subtraction, etc. (result = arr*2)

- Element-Wise Functions : Applying functions to each element (result = np.sqrt(arr))

- Sum and Mean : Calculating the sum and mean of an array. (total = np.sum(arr) \\ average = np.mean(arr))

- Maximum and Minimum : Finding the maximum and minimum values (max_val = np.max(arr) \\ min_val = np.min(arr))

- Reshaping : Changing the shape of an array (reshaped_arr = arr.reshape(2, 3))

- Transposition : Transposing a multi-dimensional array (transposed_arr = arr.T)

- Matrix Multiplication : Performing matrix multiplication (result = np.dot(matrix1, matrix2))

 

Practice Quiz

Q. What is the Python library used for scientific computing and is a basis for Pandas?

A. Numpy

 

Q. What attribute is used to retrieve the number of elements in an array?

A. a.size

 

Q. How would you change the first element to "10" in this array c:array([100, 1, 2, 3, 0])?

A. c[0] = 10

 

Q. What attribute is used to return the number of dimensions in an array?

A. a.ndim

Comments