이 글은 boostcourse 강의를 듣고 작성한 글입니다.
>>알아야하는 문법
- numpy 배열 생성법
- 0 배열 / 1 배열 생성법
- shape / reshape
- concatenate
- axis
- indexing VS slicing
- Operation, Comparison
- Boolean Index
- Fancy Index
- BroadCasting
- 배열 저장, pickle
>>신경 써야하는 부분
- numpy는 c스타일 배열 => 기존 파이썬 배열보다 빠르다.
- numpy 메서드가 복사된 배열을 반환하는지, 참조하는지 구분해야한다!!
=> 나중에 큰 데이터를 다룰 때 필요 - 연산 시에 BroadCasting이 어떻게 되는지 알아야된다.
- shape이 1차원인지 2차원인지
[1,2,3,4,5] => 1차원, [[1,2,3,4,5]] => 2차원
예제 설명은 이곳을 참고하자.
>>답안 코드
#1.
def n_size_ndarray_creation(n, dtype=np.int):
return np.arange(n**2).reshape(n,n)
#2.
def zero_or_one_or_empty_ndarray(shape, type=0, dtype=np.int):
if type == 0:
return np.zeros(shape = shape, dtype = dtype)
elif type == 1:
return np.ones(shape = shape, dtype = dtype)
else:
return np.empty(shape = shape, dtype = dtype)
#3.
def change_shape_of_ndarray(X, n_row):
return X.reshape(n_row,-1)
#4.
def concat_ndarray(X_1, X_2, axis):
try:
return np.concatenate((X_1,X_2),axis = axis)
except ValueError:
return False
#5.
def normalize_ndarray(X, axis=99, dtype=np.float32):
if axis == 99:
mean = np.mean(X)
std = np.std(X)
elif axis == 1:
mean = np.mean(X,axis = 1).reshape(-1,1)
std = np.std(X,axis = 1).reshape(-1,1)
elif axis == 0:
mean = np.mean(X,axis = 0).reshape(1,-1)
std = np.std(X,axis = 0).reshape(1,-1)
return (X - mean) / std
#6.
def save_ndarray(X, filename="test.npy"):
file = open(filename,"wb")
np.save(X,file)
#7.
def boolean_index(X, condition):
condition = eval(str('X')+condition)
return np.where(condition)
#8.
def find_nearest_value(X, target_value):
return X[np.argmin(np.abs(X-target_value))]
#9.
def get_n_largest_values(X, n):
return np.sort(X)[::-1][:n]
#return X[np.argsort(X[::-1])[:n]]
'Python > 데이터 분석 기초' 카테고리의 다른 글
Kaggle - [House Prices] 집값 예측 모델링 후기 (0) | 2021.06.06 |
---|---|
Multiple Linear Regression 정리 + 느낌 (0) | 2021.03.29 |
[머신러닝을 위한 파이썬] 3. pandas 활용 예제 (0) | 2021.03.18 |
[머신러닝을 위한 파이썬] 1.행렬 연산 구현해보기 (0) | 2021.03.18 |