from sklearn.datasets import load_digits
예제
digits = load_digits()
from sklearn.datasets import load_digits
digits = load_digits()type(digits) # sklearn.utils.Bunch
isinstance(digits, dict) # True
digits.keys()
dict_keys(['data', 'target', 'frame', 'feature_names', 'target_names', 'images', 'DESCR'])
digits['data'].shape, digits['target'].shape # ((1797, 64), (1797,))
# 이미지 데이터 보기
import numpy as np
plt.figure(figsize=(20,4)) # 20인치, 4인치
for index, (img, label) in enumerate(zip(digits['data'][:5],digits['target'][:5])):
# 1 ~ 5까지 이미지를 그리기
plt.subplot(1,5, index+1)
plt.imshow(np.reshape(img,(8,8)), cmap=plt.cm.gray)
plt.title('%s'%label, fontsize=20)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=.25, random_state=0)
x_train.shape, x_test.shape, y_train.shape, y_test.shape # ((1347, 64), (450, 64), (1347,), (450,))
from sklearn.linear_model import LogisticRegression
logisticReg = LogisticRegression(max_iter=5000) # 기본 반복 100
# 추정하기
pred = logisticReg.predict(x_test[0].reshape(1,-1)) # 1행, 열은 알아서
pred # array([2])
# 추정하기 2
pred = logisticReg.predict(x_test[:10])
pred # array([2, 8, 2, 6, 6, 7, 1, 9, 8, 5])
# 결정계수
logisticReg.score(x_test, y_test) # 0.9533333333333334