# 데이터를 불러오는 과정
from numpy import genfromtxt
data = genfromtxt('../DATA/bank_note_data.txt', delimiter=',')
#####################데이터 정보#####################
array([[ 3.6216 , 8.6661 , -2.8073 , -0.44699, 0. ],
[ 4.5459 , 8.1674 , -2.4586 , -1.4621 , 0. ],
[ 3.866 , -2.6383 , 1.9242 , 0.10645, 0. ],
...,
[ -3.7503 , -13.4586 , 17.5932 , -2.7771 , 1. ],
[ -3.5637 , -8.3827 , 12.393 , -1.2823 , 1. ],
[ -2.5419 , -0.65804, 2.6842 , 1.1952 , 1. ]])
###################################################
# 데이터 속에서 라벨(지도 학습에서 사용되는 결과값)과 feature를 분리하는 작업
labels = data[:,4]
features = data[:,0:4]
# 통상적으로 대문자 X를 train 소문자 y를 라벨로 표시
X = features
y = labels
# 사이킷 런을 활용해서 데이터를 학습 데이터와, 테스트 데이터로 분리
# + 데이터를 랜덤하게 섞는과정
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
# 데이터 정규화
from sklearn.preprocessing import MinMaxScaler
scaler_object = MinMaxScaler()
scaler_object.fit(X_train)
scaled_X_train = scaler_object.transform(X_train)
scaled_X_test = scaler_object.transform(X_test)
데이터를 정규화가 필요한 이유
1. 데이터셋의 feature값의 범위가 다르면, 모델이 특정 feature를 더 중요하거나, 덜중요하게 판단할 수 있다.
2. 경사하강법을 사용하는경우 입력 scale에 따라 수렴속도가 민감해지는데, 정규화를 통해 안정성과 속도를 향상시킬수 있다.
# 모델 만들기
model = Sequential()
# 인공 신경망 구성
# 입력, 히든, 출력 3단계로 구성
model.add(Dense(4, input_dim=4, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# 모델 컴파일
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# 학습 시작 (Train 데이터만 활용하여 성능 측정)
model.fit(scaled_X_train,y_train,epochs=50, verbose=2)
# 결과 출력 (Test 데이터를 활용하여 성능 측정)
model.evaluate(x=scaled_X_test,y=y_test)
# 혼동 행렬을 활용해 결과를 출력하는 방법들...
from sklearn.metrics import confusion_matrix,classification_report
predictions = model.predict_classes(scaled_X_test)
confusion_matrix(y_test,predictions)
print(classification_report(y_test,predictions))