AICE(AI Certificate for Everyone)/AICE Associate

[파이썬] DecisionTree 초 심플 예제

xemaker 2024. 2. 19. 16:24

import numpy as np
import pandas as pd
import json
from sklean.model_selection import train_test_split

with open('국민.json', 'r', encoding='UTF-8') as f:
  data=json.load(f)

sample_df=pd.DataFrame(data)

sample=sample_df.dropna()

sample=sample.astype('str')
y=sample.음주여부

X=sample.drop('음주여부', axis=1)
y.value_counts()

X_train, X_valid, y_train, y_valid = train_test_split(X,y,test_size=0.2,shuffle=True, random_state=120)

from sklearn.tree import DecisionTreeRegressor

dt=DecisionTreeRegressor(min_sample_split=3, max_depth=5, criterion='mae', splitter='best')

dt_model=dt.fit(X_train, y_train)
y_pred=dt.predict(X_valid)

from sklearn.metrics import mean_squrered_error

dt_mae=mean_squared_error(y_valid, y_pred)


극민json
{
"신장(5cm 단위)":{
    "0":"165,
    "1":"150"
  },
  "성별코드":{
    "0":"1",
    "1":"2"
  },
  "체중":{
    "0","60",
    "1":65
  },
  "음주여부":{
    "0":"0,0",
    "1":"1.0"
  }
}



}