머신러닝 label encoder, 하이퍼파라미터 튜닝 등 - 4월 11일

조준수·2023년 4월 18일
0

2장 11타이타닉 생존자분석 - EDA 2_

  1. crosstab
    pd.crosstab(titanic["pclass"], titanic["survived"], margins=True)

  2. FacetGrid
    grid = sns.FacetGrid(titanic, row="pclass", col="sex", height=4, aspect=2)
    grid.map(plt.hist, "age", alpha=0.8, bins=20)
    grid.add_legend();

  3. plotly.express
    import plotly.express as px
    fig = px.histogram(titanic, x="age")
    fig.show();

  4. cut
    pd.cut(titanic["age"], bins=[0,7,15,30,60,100], include_lowest=True, labels=["baby", "teen", "young", "adult", "old"])

3장 12타이타닉 생존자분석 - EDA 3_

  1. re

    import re

    for idx, dataset in titanic.iterrows():
    tmp = dataset["name"]
    print(re.search("\,\s\w+(\s\w+)?.", tmp).group())

    (1) "\,\s\w+(\s\w+)?."는 ,로 시작해서 한 칸 띄우고 글자가 나오다가 (한 칸 띄우고 글자가 나올 수도 있고 아닐 수도 있고) 마지막엔 .로 끝나는 글자는 추출한다.

3장 16_encoder and scaler - label_encoder

  1. label_encoder

    from sklearn.preprocessing import LabelEncoder
    import pandas as pd

    df = pd.DataFrame({"A":["a", "b", "c", "a", "b"], "B":[1, 2, 3, 1, 0]})
    le = LabelEncoder( )
    le.fit(df["A"])
    le.transform(df["A"])

3장 26하이퍼파라미터 튜닝 - 하이퍼파라미터 튜닝_

  1. 하이퍼파라미터 튜닝
    모델의 성능을 확보하기 위해 조절하는 설정 값

  2. 예시
    red_url = "https://raw.githubusercontent.com/PinkWink/ML_tutorial/master/dataset/winequality-red.csv"
    white_url = "https://raw.githubusercontent.com/PinkWink/ML_tutorial/master/dataset/winequality-white.csv"

    red_wine = pd.read_csv(red_url, sep=";")
    white_wine = pd.read_csv(white_url, sep=";")

    red_wine["color"] = 1.
    white_wine["color"] = 0.

    wine = pd.concat([red_wine, white_wine])
    wine["taste"] = [1. if grade>5 else 0. for grade in wine["quality"]]

    X = wine.drop(["taste", "quality"], axis=1)
    y = wine["taste"]

    from sklearn.model_selection import GridSearchCV
    from sklearn.tree import DecisionTreeClassifier

    params = {"max_depth":[2, 4, 7, 10]}

    wine_tree = DecisionTreeClassifier(max_depth=2, random_state=13)

    grid_search = GridSearchCV(estimator=wine_tree,
    param_grid=params, cv=5)
    grid_search.fit(X, y)

  3. 확인
    import pprint

    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(gridsearch.cv_results)

  4. 최적의 성능을 가진 모델은?
    gridsearch.best_estimator
    gridsearch.best_score
    gridsearch.best_params

4장 27모델평가 - 모델 평가의 개념 - 1_

  1. 모델 평가의 개념
    (1) 회귀모델들은 실제 값과의 에러치를 가지고 계산
    (2) 분류 모델의 평가 항목은 정확도, 오차행렬, 정밀도, 재현율, F1 score, ROC AUC 등 많음

4장 28모델평가 - 모델 평가의 개념 - 2_

  1. 분류모델의 결과
    그 결과를 속할 비율(확률)을 반환한다.

  2. recall
    참인 데이터 중에서 참이라고 예측한 데이터의 비율
    -> 실제 양성인 데이터를 음성이라고 판단하면 안되는 경우라면 중요

  3. precision
    참이라고 예측한 것 중에서 실제 참인 데이터의 비율
    -> 실제 음성인 데이터를 양성이라고 판단하면 안되는 경우라면 중요

  4. F1-score
    recall과 precision을 결합한 지표
    -> 어느 한 쪽으로 치우치지 않고 둘 다 높은 값을 가질수록 높은 값을 가짐

profile
print(‘안녕하세요! 반갑습니다!’)

0개의 댓글