ML (MachineLearning) 17

ํ”„๋กœํŽซ(Prophet) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

install : pip install prophet์œ„ ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ : conda install -c conda-forge prophet  ์ž„ํฌํŠธํ•˜๊ธฐ import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport randomimport seaborn as snsfrom prophet import Prophet    ์•„๋ณด์นด๋„ ๋ฐ์ดํ„ฐ   ํ”„๋กœํŽซ ๋ถ„์„์„ ์œ„ํ•ด, ๋‘๊ฐœ์˜ ์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ ('Date', 'AveragePrice') avocado_prophet_df = df[['Date','AveragePrice']]  avocado_prophet_df DateAveragePrice02015-01-041.3312015-01-041.3522015..

๋ฐ์ดํ„ฐ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ์ฆ๊ฐ•ํ•˜๊ณ  Transfer Learningํ•˜๊ธฐ

BASE MODEL HEAD MODEL - ํŠธ๋žœ์Šคํผ ๋Ÿฌ๋‹์€, ํ•™์Šต์ด ์ž˜ ๋œ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์™€์„œ ์šฐ๋ฆฌ์˜ ๋ฌธ์ œ์— ๋งž๊ฒŒ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ํ•™์Šต์ด ์ž˜ ๋œ ๋ชจ๋ธ์˜ base model๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค ์ฆ‰, head๋ชจ๋ธ์€ ๋นผ๊ณ  ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ head๋ชจ๋ธ์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํŠธ๋žœ์Šคํผ ๋Ÿฌ๋‹์˜ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค ์„ค์น˜ํ•˜๊ธฐ !pip install tensorflow-gpu==2.0.0.alpha0 !pip install tqdm Dogs vs Cats dataset ๋‹ค์šด๋กœ๋“œ๋ฐ›๊ธฐ !wget --no-check-certificate \ https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip \ -O ./cats_and_dogs_filtered.zip ์ž„..

๋”ฅ๋Ÿฌ๋‹ : CNN(ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง, Convolutional Neural Network), ์ปฌ๋Ÿฌ์‚ฌ์ง„ ์‹๋ณ„ํ•˜๊ธฐ

CNN์€ ์ปจ๋ณผ๋ฃจ์…˜ ๊ณ„์ธต(convolutional layer)๊ณผ ํ’€๋ง ๊ณ„์ธต(pooling layer)์œผ๋กœ ๊ตฌ์„ฑ๋œ ์‹ ๊ฒฝ๋ง์ด๋‹ค. 1. ์ปจ๋ณผ๋ฃจ์…˜ ๊ณ„์ธต์€ ์ž…๋ ฅ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ํ•„ํ„ฐ(๋˜๋Š” ์ปค๋„)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณ„์ธต์ด๋‹ค. ์ด ํ•„ํ„ฐ๋Š” ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ ํŠน์ • ํŒจํ„ด์„ ๊ฐ์ง€ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ€์žฅ์ž๋ฆฌ, ์งˆ๊ฐ, ์ƒ‰์ƒ ๋“ฑ์„ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. 2. ํ’€๋ง ๊ณ„์ธต์€ ์ถœ๋ ฅ์˜ ๊ณต๊ฐ„ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ตœ๋Œ€ ํ’€๋ง(max pooling)์ด๋‚˜ ํ‰๊ท  ํ’€๋ง(average pooling)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅ์„ ๊ฐ ์˜์—ญ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’ ๋˜๋Š” ํ‰๊ท  ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค. ์ด๋Š” ๊ณต๊ฐ„ ํ•ด์ƒ๋„๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๊ณ , ๊ณ„์‚ฐ๋Ÿ‰์„ ์ค„์ด๋ฉฐ, ๋ถˆํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ํŠน์ง•์„ ๋ณด๋‹ค ๊ฐ•์กฐํ•œ๋‹ค. CNN์€ ์ด๋Ÿฌํ•œ ์ปจ๋ณผ๋ฃจ์…˜ ๊ณ„์ธต๊ณผ ํ’€๋ง ๊ณ„์ธต์„ ..

tensorflow(ํ…์„œํ”Œ๋กœ์šฐ)์—์„œ def๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ•

import numpy as npimport tensorflow as tffrom tensorflow.keras.datasets import fashion_mnist# ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()   sklearn๊ณผ ์ €์žฅ๋ฐฉ์‹์˜ ์ฐจ์ด์  sklearn => joblib   .pkltensorflow => save   .h5     def ์ƒ์„ฑํ•˜๊ธฐ def build_model(): model = Sequential() model.add( Flatten() ) model.add( Dense(128, 'relu') ) model.add( Dense(10, 'softmax')) model.compile('..

ํ‘๋ฐฑ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ์…‹์„ AI์—๊ฒŒ ํŒ๋ณ„์‹œ์ผœ, ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์ •๋‹ต์„ ๋งž์ถ”๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ ๊ธฐ๋ฒ•

https://www.tensorflow.org/datasets/catalog/fashion_mnist?hl=ko ํŒจ์…˜_์— ๋‹ˆ์ŠคํŠธ | TensorFlow Datasets ์ด ํŽ˜์ด์ง€๋Š” Cloud Translation API๋ฅผ ํ†ตํ•ด ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŒจ์…˜_์— ๋‹ˆ์ŠคํŠธ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ˜ํ…์ธ ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜์„ธ์š”. Fashion-MNIST๋Š” 60,000๊ฐœ์˜ ์˜ˆ์ œ๋กœ ๊ตฌ์„ฑ๋œ www.tensorflow.org import numpy as np import tensorflow as tf from tensorflow.keras.datasets import fashion_mnist #fashion_mnist ์‚ฌ์šฉ (X_train, y_train), (X_test, y_test) = fashion..

GridSearch ๋ฅผ ์ด์šฉํ•œ ์ตœ์ ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฐพ๊ธฐ

https://www.tensorflow.org/api_docs/python/tf/keras?_gl=1*7074vd*_up*MQ..*_ga*MjEzMDk5NTY2NC4xNzEzNDg3ODQ0*_ga_W0YLR4190T*MTcxMzQ4Nzg0NC4xLjAuMTcxMzQ4Nzg0NC4wLjAuMA.. Module: tf.keras | TensorFlow v2.16.1 DO NOT EDIT. www.tensorflow.org ! pip install scikeras from scikeras.wrappers import KerasClassifier from sklearn.model_selection import GridSearchCV from keras.models import Sequential from ke..

๋”ฅ๋Ÿฌ๋‹ : Neural Networks ์œผ๋กœ Classification(๋ถ„๋ฅ˜) ํ•˜๊ธฐ

๋”ฅ๋Ÿฌ๋‹์€ ์ด๋ฏธ์ง€ ์ธ์‹, ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ, ์Œ์„ฑ ์ธ์‹ ๋“ฑ ๋‹ค์–‘ํ•œ ์˜์—ญ์—์„œ ๋งค์šฐ ์„ฑ๊ณต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. ๋”ฅ๋Ÿฌ๋‹์€ ํžˆ๋“ ๋ ˆ์ด์–ด๊ฐ€ ๋งŽ๋‹ค๋Š” ๊ฒƒ์ด ํŠน์ง•์ด๋‹ค - ๋”ฅ๋Ÿฌ๋‹์—์„œ "ํžˆ๋“  ๋ ˆ์ด์–ด"๋Š” ์ž…๋ ฅ์ธต(input layer)๊ณผ ์ถœ๋ ฅ์ธต(output layer) ์‚ฌ์ด์— ์žˆ๋Š” ์ค‘๊ฐ„ ๋ ˆ์ด์–ด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ํžˆ๋“  ๋ ˆ์ด์–ด๊ฐ€ ๋งŽ์„์ˆ˜๋ก, ๋ชจ๋ธ์€ ๋” ๋ณต์žกํ•œ ํŒจํ„ด์ด๋‚˜ ๊ด€๊ณ„๋ฅผ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์ด๋Š” ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ๊ฐ„์˜ ๋ณต์žกํ•œ ๋น„์„ ํ˜• ๊ด€๊ณ„๋ฅผ ๋ชจ๋ธ๋งํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. - ๋” ๋งŽ์€ ํžˆ๋“  ๋ ˆ์ด์–ด๋ฅผ ๊ฐ€์ง„ ์‹ ๊ฒฝ๋ง์€ ๋” ๋งŽ์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์—, ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ์™€ ์—ฐ์‚ฐ ๋ฆฌ์†Œ์Šค๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ถฉ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ์™€ ๋ฆฌ์†Œ์Šค๊ฐ€ ์ œ๊ณต๋œ๋‹ค๋ฉด, ๊นŠ์€ ์‹ ๊ฒฝ๋ง์€ ๋งค์šฐ ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ํšจ๊ณผ์ ์ผ ์ˆ˜ ์žˆ๋‹ค. ๊ธˆ์œต์ƒํ’ˆ ๊ฐฑ์‹ (0 ๋˜๋Š” 1) ์—ฌ๋ถ€ ์˜ˆ์ธกํ•˜๋Š” ..

๋จธ์‹ ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋… ์š”์•ฝ

- ํ‰๊ท  ์ค‘์•™๊ฐ’ ๋ชจ๋“œ (ํ‰๊ท  ๊ฐ’, ์ค‘๊ฐ„์  ๊ฐ’, ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๊ฐ’) - ํ‘œ์ค€ํŽธ์ฐจ : ๊ฐ’์ด ์–ผ๋งˆ๋‚˜ ๋ถ„์‚ฐ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž - ๋ฐฑ๋ถ„์œ„์ˆ˜ : ํ†ต๊ณ„์—์„œ ํŠน์ • ๊ฐ’์ด ๋ฐฑ๋ถ„์œจ์ด ๋” ๋‚ฎ์€ ๊ฐ’ - ๋ฐ์ดํ„ฐ๋ฐฐํฌ : ๋น…๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ์–ป๋Š”์ง€ - ์ •๊ทœ ๋ฐ์ดํ„ฐ ๋ถ„ํฌ : ์ฃผ์–ด์ง„ ๊ฐ’ ์ฃผ์œ„์— ๊ฐ’์ด ์ง‘์ค‘๋˜๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• - ์‚ฐํฌ๋„ : ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ ๊ฐ ๊ฐ’์ด ์ ์œผ๋กœ ํ‘œ์‹œ๋˜๋Š” ๋‹ค์ด์–ด๊ทธ๋žจ - ์„ ํ˜•ํšŒ๊ท€ : ๋ณ€์ˆ˜๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ฐพ์œผ๋ ค๊ณ  ํ•  ๋•Œ ์‚ฌ์šฉ - ๋‹คํ•ญ์‹ ํšŒ๊ท€ : ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ๊ฐ€ ์„ ํ˜•(๋ชจ๋“  ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ์ง์„ )์— ๋งž์ง€ ์•Š์œผ๋ฉด ๋‹คํ•ญ์‹ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉ - ๋‹ค์ค‘ํšŒ๊ท€ : ์„ ํ˜•ํšŒ๊ท€์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๋‘๊ฐœ ์ด์ƒ์˜ ๋…๋ฆฝ์ ์ธ ๊ฐ’ ์ฆ‰, ๋‘๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ’์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•จ - ๊ทœ๋ชจ : ์Šค์ผ€์ผ ๊ธฐ๋Šฅ (๋ฐ์ดํ„ฐ ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด ๋น„๊ตํ•˜๊ธฐ..

ํ•˜์ด๋ผํ‚ค ํด๋Ÿฌ์Šคํ„ฐ๋ง(Hierarchical Clustering) : ๊ณ„์ธต์  ๊ตฐ์ง‘

๋จธ์‹ ๋Ÿฌ๋‹์˜ ๋น„์ง€๋„(unsupervised)ํ•™์Šต 1. ํ‰ํ• /๋ถ„ํ•  ๊ธฐ๋ฐ˜์˜ ๊ตฐ์ง‘ (Partition-based Clustering)- ๋น„์Šทํ•œ ํŠน์ง•์„ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ๋ผ๋ฆฌ ๋ฌถ๋Š”๊ฒƒ์ด๋‹ค- ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ˆ˜์˜ ๊ตฐ์ง‘์„ ํ˜•์„ฑํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด๋‹น ๊ตฐ์ง‘์— ํ• ๋‹นํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹คex ) K-Means Clustering   2. ๊ณ„์ธต์  ๊ตฐ์ง‘ (Hierarchical Clustering)- ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์  ๋˜๋Š” ๊ณ„์ธต์ ์œผ๋กœ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜- ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ ๋˜๋Š” ์œ ์‚ฌ๋„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ํ˜•์„ฑํ•˜์—ฌ ๊ตฐ์ง‘์„ ํ˜•์„ฑ- ๊ณ„์ธต์ ์ธ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ๊ตฐ์ง‘ํ™” ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์–‘ํ•œ ์ˆ˜์ค€์—์„œ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‹œ๊ฐ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์‰ฝ๋‹ค- ์‚ฌ์ „์— ๊ตฐ์ง‘์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†์–ด ํŽธ๋ฆฌ- ํฐ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•ด์„œ๋Š” ๊ณ„์‚ฐ ๋น„์šฉ..

K-Means ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋จธ์‹ ๋Ÿฌ๋‹์˜ ๋น„์ง€๋„(unsupervised)ํ•™์Šต 1. ํ‰ํ• /๋ถ„ํ•  ๊ธฐ๋ฐ˜์˜ ๊ตฐ์ง‘ (Partition-based Clustering) - ๋น„์Šทํ•œ ํŠน์ง•์„ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ๋ผ๋ฆฌ ๋ฌถ๋Š”๊ฒƒ์ด๋‹ค - ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ˆ˜์˜ ๊ตฐ์ง‘์„ ํ˜•์„ฑํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด๋‹น ๊ตฐ์ง‘์— ํ• ๋‹นํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค ex ) K-Means Clustering 2. ๊ณ„์ธต์  ๊ตฐ์ง‘ (Hierarchical Clustering) - ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์  ๋˜๋Š” ๊ณ„์ธต์ ์œผ๋กœ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ ๋˜๋Š” ์œ ์‚ฌ๋„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ํ˜•์„ฑํ•˜์—ฌ ๊ตฐ์ง‘์„ ํ˜•์„ฑ - ๊ณ„์ธต์ ์ธ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ๊ตฐ์ง‘ํ™” ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์–‘ํ•œ ์ˆ˜์ค€์—์„œ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‹œ๊ฐ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์‰ฝ๋‹ค - ์‚ฌ์ „์— ๊ตฐ์ง‘์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†์–ด ํŽธ๋ฆฌ - ํฐ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•ด์„œ๋Š”..