Exemplo n.º 1
0
    image5 = image_to_array('digit5.bmp') + 255
    # image5 = np.where(image5 == 0, 1, 0)
    print(image5)

    plt.imshow(image5, cmap='binary')
    plt.show()

    # ML 알고리즘을 테스트할 때 사용할 테스트 세트
    X_test = [image1.reshape((784,)),
              image3.reshape((784,)),
              image5.reshape((784,))]

    # mnist 예제 세트 로드
    mnist = load_mnist_from_pickle()
    # SGDClassifier를 mnist로 학습 - 2진 분류기
    X_train, y_train, _, _ = split_mnist(mnist)
    print(X_train.shape, y_train.shape)

    sgd_clf = SGDClassifier(random_state=1)

    scaler = StandardScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    y_train_5 = (y_train == 5)
    sgd_clf.fit(X_train_scaled, y_train_5)

    # X_test로 SGD 분류기 테스트
    X_test_scaled = scaler.transform(X_test)
    predicts = sgd_clf.predict(X_test_scaled)
    pred_scores = sgd_clf.decision_function(X_test_scaled)
    print(predicts)
    print(pred_scores)
Exemplo n.º 2
0
import matplotlib.pyplot as plt
import numpy as np

from sklearn.linear_model import SGDClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score, f1_score, \
    precision_recall_curve, roc_curve, roc_auc_score

from ch03.ch03_all import load_mnist_from_pickle, split_mnist

if __name__ == '__main__':
    # mnist 데이터 셋 준비
    mnist = load_mnist_from_pickle()

    # train set/test set 분리
    X_train, y_train, X_test, y_test = split_mnist(mnist)
    print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)

    # 2진 분류 - 레이블의 클래스가 2개(5, 5가 아님)
    y_train_5 = (y_train == 5)  # 2진 분류에서 사용할 레이블

    # SGD 분류기 생성
    sgd_clf = SGDClassifier(random_state=1)
    # 분류기를 학습(ML 모델(알고리즘) 학습)
    sgd_clf.fit(X=X_train, y=y_train_5)
    # train set의 예측값
    y_binary_predicts = sgd_clf.predict(X=X_train)
    print('예측값(predicts):', y_binary_predicts)
    print('실제값(actual):', y_train_5)

    # 정확도: 전체 샘플에서 정확하게 예측한 비율
    # accuracy = (TP + TN) / (TP + TN + FP + FN)
Exemplo n.º 3
0
    여러개의 레이블에 속하는 데이터들을 각각의 클래스로 분류
3) 다중 출력 분류(Multi-output classification)
"""
import joblib
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import cross_val_predict

from ch03.ch03_all import load_mnist_from_pickle, split_mnist
from ch03.ex02_mnist import plot_digit

if __name__ == '__main__':
    mnist = load_mnist_from_pickle()
    X_train, y_train, X_test, y_label = split_mnist(mnist)

    # ML 모델(알고리즘) 선택
    # sgd_clf = SGDClassifier(random_state=1)
    # 학습 데이터를 사용해서 모델을 학습시킴.
    # sgd_clf.fit(X=X_train, y=y_train)
    # 학습 결과를 저장
    # joblib.dump(sgd_clf, 'ex07_1.joblib')

    # 저장된 파일에서 ML 모델(알고리즘) 불러옴.
    sgd_clf = joblib.load('ex07_1.joblib')
    print(sgd_clf)

    # 학습 데이터의 예측값
    sgd_train_predicts = sgd_clf.predict(X_train[:5])
    print('예측값:', sgd_train_predicts)