-
TIL - 06.12TIL 2024. 6. 12. 20:08
초안.
# 선형회귀 훈련(적합) from sklearn.linear_model import LinearRegression model_lr = LinearRegression() type(model_lr) sns.get_dataset_names() iris = sns.load_dataset('iris') model_lr.fit(X = X_1, y = y_1) # 가중치(w1) print(model_lr.coef_) # 편향(bias, w0) print(model_lr.intercept_) w1 = model_lr.coef_[0][0] w0 = model_lr.intercept_[0] print('y = {}x + {}'.format(w1.round(2),w0.round(2)))
import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd import sklearn from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression LogisticRegression = sklearn.linear_model.LogisticRegression model_lor = LogisticRegression() sns.get_dataset_names() iris = sns.load_dataset('iris') def get_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) from sklearn.metrics import accuracy_score, f1_score def get_metrics(true, pred): print('정확도', accuracy_score(true, pred)) print('f1-score', f1_score(true, pred)) X_2 = iris[['sepal_length','sepal_width','petal_length','petal_width']] y_2 = iris['species'] model_lor = LogisticRegression() model_lor.fit(X_2, y_2) # Print the classes print(f"Classes: {model_lor.classes_}") y_pred_2 = model_lor.predict(X_2) get_metrics(y_2, y_pred_2)
# 필요한 라이브러리 임포트 from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, classification_report # 1. 데이터셋 로드 iris = datasets.load_iris() X = iris.data y = iris.target # 2. 데이터 전처리 # 데이터셋을 훈련 세트와 테스트 세트로 분리 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # # 필요한 경우 데이터 정규화 scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # 3. 모델 생성 logistic_regression_model = LogisticRegression(max_iter=200) # 4. 모델 훈련 logistic_regression_model.fit(X_train, y_train) # 5. 모델 평가 y_pred = logistic_regression_model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) report = classification_report(y_test, y_pred) print(f'Accuracy: {accuracy}') print(f'Classification Report:\n{report}') # 6. 예측 수행 # 새로운 데이터에 대해 예측을 수행할 수 있습니다. new_data = [[5.1, 3.5, 1.4, 0.2]] # 예시 데이터 new_data_scaled = scaler.transform(new_data) prediction = logistic_regression_model.predict(new_data_scaled) predicted_class = iris.target_names[prediction[0]] print(f'Predicted class: {predicted_class}')
'TIL' 카테고리의 다른 글
TIL - 06.17 (0) 2024.06.17 TIL - 06.14 (0) 2024.06.17 TIL - 06.11 (0) 2024.06.11 TIL - 06.10 (0) 2024.06.10 TIL - 05.28 (0) 2024.05.28