#로지스틱 모듈 설치 import sklearn
from sklearn.linear_model import LogisticRegression
# X=fare, y=survived 변수 할당
X_1 = titanic_df[['Fare']]
y_true = titanic_df[['Survived']]
#모델 생성 & 학습
model_lor = LogisticRegression()
model_lor.fit(X_1,y_true)
#예측
y_pred = model_lor.predict(X_1)
# 평가from sklearn.metrics import accuracy_score,f1_score
defget_metrics (true,pred):
print(f'정확도 : {accuracy_score(true,pred)}, f-score : {f1_score(true,pred)}')
get_metrics(y_true,y_pred)
# 로지스틱 회귀 정봉 및 회귀식 함수로 생성 (계속 넣어서 써야하니까)defget_att(x):
#x 모델 넣기print('클래스 종류', x.classes_)
print('독립변수 갯수', x.n_features_in_)
print('들어간 독립변수(X)의 이름', x.feature_names_in_)
print('가중치', x.coef_)
print('편향', x.intercept_)
get_att(model_lor) #함수 파라미터로 훈련 모델 받기
# sex 컬럼 인코딩 defget_sex(x):
if x=='female':
return0else:
return1
titanic_df['Sex_en'] = titanic_df['Sex'].apply(get_sex)
#모델 생성 & 학습
X_2 = titanic_df[['Fare','Pclass','Sex_en']]
y_true = titanic_df[['Survived']]
model_lor2 = LogisticRegression()
model_lor2.fit(X_2,y_true)
# 예측 및 평가
y_pred2 = model_lor2.predict(X_2)
#get_metrics 함수 사용
get_metrics(y_true,y_pred2)