728x90
SyncRNG함수를 쓰면 난수를 동일하게 발생시킬 수 있다.
같은 시퀀스 난수 발생시키는 패키지
GitHub - GjjvdBurg/SyncRNG: Reliably generate the same random numbers in R and Python
arrays - Creating same random number sequence in Python, NumPy and R - Stack Overflow
Creating same random number sequence in Python, NumPy and R
Python, NumPy and R all use the same algorithm (Mersenne Twister) for generating random number sequences. Thus, theoretically speaking, setting the same seed should result in same random number seq...
stackoverflow.com
Python
<python />
# 데이터 셋 7:3 으로 분할
v=list(range(1,len(raw_data)+1))
s=SyncRNG(seed=42)
ord=s.shuffle(v)
idx=ord[:round(len(raw_data)*0.7)]
# R에서는 데이터프레임이 1부터 시작하기 때문에 -1
for i in range(0,len(idx)):
idx[i]=idx[i]-1
# 학습데이터, 테스트데이터 생성
train=raw_data.loc[idx] # 70%
#train=train.sort_index(ascending=True)
test=raw_data.drop(idx) # 30%
R
<r />
#모델 생성
library(SyncRNG)
v <- 1:nrow(df)
s <- SyncRNG(seed=42)
idx <- s$shuffle(v)[1:round(nrow(df)*0.7)]
idx[1:length(idx)]
train <- df[idx,]
test <- df[-idx,]
'파이썬 > 파이썬 기초' 카테고리의 다른 글
Python을 활용 Excel 파일 생성 및 내용 수정 (9) | 2022.12.29 |
---|---|
파이썬 pandas columns 인덱스삭제(컬럼명이 같을 경우) (0) | 2022.12.14 |
파이썬 자료형의 값을 저장하는 공간, 변수 (2) | 2022.12.13 |
파이썬 문자열 (0) | 2022.12.12 |
파이썬 실수값의 오차 (0) | 2022.12.12 |