본문 바로가기
반응형

python338

판다스 컬럼을 열로 변환하는 방법 판다스 컬럼을 열로 변환하는 방법¶ In [1]: import pandas as pd # 예시 데이터프레임 생성 df = pd.DataFrame({ 'A': ['jin', 'Lee', 'Yuri'], 'B': ['apple', 'banana', 'orange'], 'C': [1.1, 2.1, 3.1], 'D': [40, 50, 60] }) df Out[1]: A B C D 0 jin apple 1.1 40 1 Lee banana 2.1 50 2 Yuri orange 3.1 60 In [2]: # 'melt' 함수를 사용하여 컬럼을 열로 변환 melt.. 2023. 5. 22.
파이썬 string to datetime or datetime to string 파이썬 string to datetime or datetime to string string to datetime (strftime) example.strftime('%Y-%m-%d %H:%M:%S') # out : 2021-04-08 01:20:15 datetime to string (strptime) import datetime example = '2021-04-08 01:20:15' datetime.datetime.strptime(example, '%Y-%m-%d') # out : datetime.datetime(2021, 4, 8, 1, 20, 15) 2023. 5. 20.
pytorch tensor.new, 파이토치 tensor.new new_zeros(), new_ones(), new_full() pytorch tensor.new, 파이토치 tensor.new new_zeros(), new_ones(), new_full() 설명 tensor.new()는 PyTorch에서 주어진 tensor의 속성(데이터 타입, 디바이스 등)을 그대로 유지하면서 새로운 tensor를 생성하는 데 사용되는 메서드입니다. new() 사용예시 import torch # 기존 tensor 생성 original_tensor = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32, device='cpu') # new()를 사용하여 기존 tensor와 동일한 속성을 가진 새로운 tensor 생성 new_tensor = original_tensor.new(2, 2) print(new_ten.. 2023. 5. 18.
RNN의 파라미터 개수와 토큰 길이의 관계 RNN의 파라미터 개수와 토큰 길이의 관계¶ RNN의 파라미터 개수는 토큰 길이(sequence length)와 관계 없습니다. 아래 예시를 통해 확인 가능합니다. In [1]: # 패키지 불러오기 import torch import torch.nn as nn In [2]: # 파이토치 RNN 모델을 구성하기 위해 토큰의 # 백터 길이(feature_length), # 히든 사이즈(hidden_size), # 레이어 개수(num_of_layers) # 를 설정합니다. feature_length = 3 hidden_size = 1 num_of_layers = 1 # RNN 레이터 생성 rnn = nn.RNN(feature_length, hidden_size, num_of_layers) # 파라미터 관찰 r.. 2023. 5. 15.
파이썬 리스트를 tsv 로 저장 파이썬 리스트를 tsv 로 저장 예시 리스트 만들기 # (질문, 대답) 형태로 예시 리스트를 생성합니다. QA = [ ("What is the primary function of the heart?", "To pump blood through the circulatory system."), ("Where is the Great Barrier Reef situated?", "Off the coast of Queensland, Australia"), ("What is the Sun mainly composed of?", "Hot plasma"), ("Who designed the Eiffel Tower?", "Gustave Eiffel"), ("Will there be a London Marathon in .. 2023. 5. 10.
파이썬 assert 사용법 파이썬 assert 사용법¶ 조건을 만족하지 않을 경우 AssertionError를 리턴합니다. assert 함수는 테스트 코드 작성에 유용하게 사용됩니다. 특히, 유닛 테스트에서 함수가 예상대로 동작하는지 확인할 때 assert 함수를 사용할 수 있습니다. In [1]: # assert 조건, 에러 매세지 # 순으로 입력합니다. a = 1 assert a 6 assert a 5 assert type(i) is str, f"{i}는 str 타입이 아닙니다" 6 print(f"{i}는 문자열 입니다.") AssertionError: 1는 str 타입이 아닙니다 2023. 5. 10.
반응형