πΌ λ¬Έμ μ μ
- λ 립λ³μ : 4κ° λ³μ(κ½λ°μΉ¨κΈΈμ΄, κ½λ°μΉ¨λλΉ, κ½μκΈΈμ΄, κ½μλλΉ)
- μ’ μλ³μ : μμ΄λ¦¬μ€ νμ’ 3κ°μ§(Iris Setosa, Iris Versicolour, Iris Virginica)
- λ°μ΄ν°μ μ΄ μ 150 (κ° νμ’ λ³ 50κ° λ°μ΄ν°)
- 4κ° λ³μ(κ° κ½μ κΈΈμ΄ λλΉ λ±)λ₯Ό λ 립λ³μλ‘ λ³΄κ³ μμ΄λ¦¬μ€ νμ’ μ λΆλ₯νλ λ€μ€ λΆλ₯ λ¬Έμ λ‘ μ μ
import pandas as pd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense
from tensorflow.keras.optimizers import RMSprop
from sklearn import preprocessing
import warnings
warnings.filterwarnings('ignore')
from sklearn.model_selection import train_test_split
dataset_path = tf.keras.utils.get_file("iris.data", "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
- μλ³Έ νμΌμ 보면 컬λΌλͺ
μ΄ λμμμ§ μκΈ° λλ¬Έμ 컬λΌλͺ
μ μ§μ ν΄μ£Όκ³ μ΄κΈ°
column_names = ['sepal length','sepal width','petal length','petal width','class']
raw_dataset = pd.read_csv(dataset_path, names=column_names)
dataset = raw_dataset.copy()
Iris-setosa -> 0
Iris Versicolour -> 1
Iris-virginica -> 2
label_encoder = preprocessing.LabelEncoder()
l_class = label_encoder.fit_transform(dataset['class'])
dataset['class'] =l_class
X = dataset[['sepal length','sepal width','petal length','petal width']]
y = dataset['class']
# νμ΅ λ°μ΄ν°: 120건(80%), ν
μ€νΈ λ°μ΄ν°: 30건(20%)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=7)
from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
np.random.seed(7)
model = Sequential()
# μ
λ ₯λ΄λ° 4κ°, νΌμ
νΈλ‘ κ°μ 16, νμ±νν¨μ relu
model.add(Dense(16, input_shape=(4, ), activation='relu'))
# μΆλ ₯λ΄λ° 3κ°, νμ±νν¨μ softmax
model.add(Dense(3, activation='softmax'))
# μμ€ν¨μ(loss): categorical_crossentropy , optimizer(νμ΄νΌνλΌλ―Έν°) : Adam , metrics : accuracy
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=200, batch_size=10)
scores = model.evaluate(X_test, y_test)
print("%s: %2.f%%" %(model.metrics_names[1], scores[1]*100))
π μ°¨νΈ
f, ax = plt.subplots(1,2, figsize=(12,6)) dataset['class'].value_counts().plot.pie(explode=None, autopct='%1.2f%%', ax=ax[0]) ax[0].set_title('iris class pie chart') ax[0].set_ylabel('') sns.countplot('class', data=dataset, ax=ax[1]) ax[1].set_title('Count of iris class') ax[1].set_ylabel('') plt.show()
π§ λ μ΄λΈμ λ²μ£Όν ννλ‘ λ³κ²½νλ λ°©λ²
from sklearn.preprocessing import LabelEncoder from keras.utils import np_utils encoder = LabelEncoder() encoder.fit(y) Y_encodered = encoder.transform(y) # λΌλ²¨λ§ Y = np_utils.to_categorical(Y_encodered)