동현 유
척척석사
동현 유
전체 방문자
오늘
어제
  • 분류 전체보기 (178)
    • BlockChain (48)
      • [paper] Consensus (13)
      • [paper] Execution (19)
      • [paper] Storage (5)
      • [paper] ZKP (1)
      • [paper] Oracle (1)
      • Blockchains (9)
    • Java (19)
      • Java의 정석 (13)
      • Java 파헤치기 (5)
    • Python (20)
      • Python 뜯어보기 (6)
      • 데이터 분석 기초 (5)
      • Python 기초 강의 (6)
      • Python 기초 강의 부록 (3)
    • Golang (0)
    • MySQL (3)
      • programmers (2)
      • 기본 문법 (0)
    • 웹 프로젝트 (IBAS) (36)
      • Django 레거시 (14)
      • SpringBoot api 개편 (14)
      • Infra (3)
      • 서버 장애 기록 (4)
      • 신입팀원 교육 자료 (1)
    • CS (30)
      • Operating System (22)
      • Computer Security (3)
      • Network (4)
      • DBMS (1)
    • 책 (10)
      • 도메인 주도 설계 철저 입문 (9)
      • Real MySQL 8.0 (1)
    • BOJ 문제 풀이 (3)
    • 이러쿵저러쿵 (7)
    • 회고 (1)

인기 글

최근 댓글

최근 글

hELLO · Designed By 정상우.
동현 유

척척석사

Python/데이터 분석 기초

[머신러닝을 위한 파이썬] 2. numpy 활용 예제

2021. 3. 18. 19:44

이 글은 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차원

 

예제 설명은 이곳을 참고하자.

 

>>답안 코드

numpyPractice.zip
0.00MB

 

#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
    동현 유
    동현 유
    Fault Tolerant System Researcher for more Trustful World and Better Lives. (LinkedIn: https://www.linkedin.com/in/donghyeon-ryu-526b8a276/)

    티스토리툴바