작성자 없음
작성자 정보가 삭제된 글입니다.
해결된 질문
작성
·
331
0
안녕하세요
2유형을 공부하는 중에
train_test_split의 random_state 값에 따라서 값의 편차가 크게 차이 나는 것을 발견했습니다.
# random_state = 1 : 0.8643817947300534 # random_state = 2023 : 0.7804496038326884
이 정도로 차이가 나는데 테스트 값에는 크게 영항이 없는 것인가요 ? 다른 코드들은 모두 동일했습니다 !
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
train= pd.read_csv('https://raw.githubusercontent.com/Datamanim/datarepo/main/stroke_/train.csv')
test= pd.read_csv('https://raw.githubusercontent.com/Datamanim/datarepo/main/stroke_/test.csv')
train = train.drop('id',axis=1)
test_id = test.pop('id')
y_train = train.pop('stroke')
# 결측치 채우기
train['bmi'] = train['bmi'].fillna(train['bmi'].mean())
test['bmi'] = test['bmi'].fillna(train['bmi'].mean())
train['age'] = train['age'].str.replace('*','').astype('int')
# StandarScaler
# print(train.info())
# train.nunique()
cols = ['age','avg_glucose_level', 'bmi']
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
train[cols] = scaler.fit_transform(train[cols])
test[cols] = scaler.transform(test[cols])
# LabelEncoder
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
train['gender'] = le.fit_transform(train[['gender']])
test['gender'] = le.fit_transform(test[['gender']])
# get_dummies
train = pd.get_dummies(train)
test = pd.get_dummies(test)
cols = ['ever_married_No','work_type_Govt_job','Residence_type_Rural','smoking_status_Unknown']
train = train.drop(cols,axis=1)
test = test.drop(cols,axis=1)
from sklearn.model_selection import train_test_split
X_tr,X_val,y_tr,y_val = train_test_split(train,y_train,test_size=0.2,random_state=2023,stratify = y_train)
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(random_state=2023,n_estimators = 200)
model.fit(X_tr,y_tr)
pred_val = model.predict_proba(X_val)
from sklearn.metrics import roc_auc_score
roc_auc_score(y_val,pred_val[:,1])
# random_state = 1 : 0.8643817947300534
# random_state = 2023 : 0.7804496038326884