TIL

2024-06-14

여연찌 2024. 6. 14. 21:03
  • Today

1. SQL 코드카타

2. 머신러닝 라이브세션 수강

3. SQL Challenge 3회차 과제 풀이

4. Python 으로 코드 짜보기

+ 머신러닝 개인과제 4번

 

 


  • Today I Learned

 

머신러닝 개인과제

 

 

Logistic Regression  모델로 정확도 계산

# 데이터 불러오기
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target

# train_test_split 를 이용해서 test 데이터를 분리
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Logistic Regression 모델 불러오기
from sklearn.linear_model import LogisticRegression

# Logistic Regression 모델로 학습
lo=LogisticRegression()
lo.fit(X_train, y_train)

# 예측값 계산
y_pred=lo.predict(X_test)
len(y_pred)

# accuracy_score 불러오기
from sklearn.metrics import accuracy_score

# accuracy_score 정확도를 계산하기
accuracy_score(y_test,y_pred)

 

Linear Regeression 모델로 MSE 평가

# 데이터 불러오기
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
X, y = housing['data'], housing['target']

# train_test_split 를 이용해서 test 데이터를 분리
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 데이터 확인 
X_train.shape
X_test.shape
y_train.shape  
y_test.shape

# 모델 학습
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train, y_train)

# 예측값
y_pred2=lr.predict(X_test)

# mean_squared_error 이용하여 MSE 계산
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test,y_pred2)

 

DecisionTree 모델로 정확도 계산

# 데이터 불러오기
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target

# train_test_split 를 이용해서 test 데이터를 분리
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# DecisionTree 모델 불러오기
from sklearn.tree import DecisionTreeClassifier

# DecisionTree 모델로 학습
dt= DecisionTreeClassifier()
dt.fit(X_train,y_train)

# 예측값 계산
y_pred3=dt.predict(X_test)

# accuracy_score 정확도를 계산하기
accuracy_score(y_test,y_pred3)

 

랜덤포레스트 분류기

# 데이터 불러오기
url = "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
titanic = pd.read_csv(url)

titanic.info()

# 라벨인코더 로 범주형을 수치형으로 변환해주기
from sklearn.preprocessing import LabelEncoder
# 랜덤포레스트 패키지 불러오기
from sklearn.ensemble import RandomForestClassifier

rf=RandomForestClassifier(random_state=42)

# X 변수에 담아두기
X_features = ['Pclass','Sex','Age','SibSp','Parch']
# Pclass, Sex: LabelEncoder
# Age: 결측치 > 평균

le = LabelEncoder()
titanic['Pclass']=le.fit_transform(titanic['Pclass'])

le = LabelEncoder()
titanic['Sex']=le.fit_transform(titanic['Sex'])

age_mean = titanic['Age'].mean()
titanic['Age']=titanic['Age'].fillna(age_mean)

X = titanic[X_features]
y = titanic['Survived']

# train_test_split 를 이용해서 test 데이터를 분리
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

rf.fit(X_train,y_train)

# 예측값 계산
y_pred4=rf.predict(X_test)

# 평가
# accuracy_score 정확도를 계산하기
accuracy_score(y_test,y_pred4)

# 개별검증
rf.feature_importances_

 

클러스터링으로 시각화

# 데이터 불러오기
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target

# 클러스터 불러오기
from sklearn.cluster import KMeans

# plt 불러오기
import matplotlib.pyplot as plt
# seaborn 불러오기
import seaborn as sns

kmeans=KMeans(n_clusters=3,random_state=42)

# 예측값
y_kmeans=kmeans.fit_predict(X)

# 산점도 그리기
plt.figure(figsize = (8,6))
plt.scatter(X[:,0] , X[:,1], c=y_kmeans)

 

 


  • Next

1. SQL

2. 프로젝트

 

 

'TIL' 카테고리의 다른 글

2024-06-17  (0) 2024.06.17
2024-06-15  (1) 2024.06.15
2024-06-13  (0) 2024.06.13
2024-06-12  (1) 2024.06.12
2024-06-11  (1) 2024.06.11