<jupyter_start><jupyter_text>"[MachineLearning] Regression Analysis - Logistic Regression"> 2022.03.20 - toc: false- badges: false- comments: false- categories: [regression analysis]- hide_{github,colab,binder,deepnote}_badge: true __Content creators:__ 이중원__Content reviewers:__   1. Overview  Logistic Regression란?- Regression기법을 분류 문제로 확장한 것- 이항 형태의 범주형 데이터인 경우, 사용가능 ex) 양성/음성, 합격/불합격- 입력 값이 각 클래스에 속하는 확률값을 회귀분석으로 예측 Odds- 임의의 사건 X가 발생하지 않을 확률 대비 일어날 확률의 비율- $Odds = {P(y=1|x)\over {1-P(y=1|x)}} = exp(mx + b)$ Logistic Function (Sigmoid Function)- 𝑥 ∈ (−∞,∞)를 −1,1 범위로 매핑하는 S자형 함수- $f(x) = {1\over {1 + \exp(-x)}}$ = $exp(x)\over {1 + \exp(x)}$  2. Example  데이터셋 준비<jupyter_code>from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)

X_train, X_valid, y_train, y_valid = train_test_split(X, y, stratify=y, test_size=0.2, random_state=30)

X_train.shape, y_train.shape, X_valid.shape, y_valid.shape<jupyter_output><empty_output><jupyter_text>학습<jupyter_code>classifier = LogisticRegression(random_state=0)

classifier.fit(X_train, y_train)<jupyter_output>/Users/jwon/opt/anaconda3/lib/python3.8/site-packages/sklearn/linear_model/_logistic.py:763: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  n_iter_i = _check_optimize_result(<jupyter_text>예측<jupyter_code>y_pred = classifier.predict(X_valid)
y_pred

classifier.score(X_valid, y_valid)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1185064935064935, 'fraction_numerical': 0.016233766233766232, 'mean_word_length': 6.003787878787879, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '654691', 'license': 'Apache-2.0', 'n_tokens_mistral': 828, 'n_tokens_neox': 872, 'n_words': 160, 'path': '_notebooks/regression-analysis/ml-regression-logistic-regression.ipynb', 'repo_name': 'KNU-AI-Researcher/reports', 'size': '1848'}