<jupyter_start><jupyter_text>0～9の手書き文字の認識（識別） ライブラリをインポート<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_digits
from sklearn.metrics import confusion_matrix

digits = load_digits()<jupyter_output><empty_output><jupyter_text>digitsの要素を確認<jupyter_code>dir(digits)<jupyter_output><empty_output><jupyter_text>説明を表示<jupyter_code>print(digits.DESCR)<jupyter_output>.. _digits_dataset:

Optical recognition of handwritten digits dataset
--------------------------------------------------

**Data Set Characteristics:**

    :Number of Instances: 5620
    :Number of Attributes: 64
    :Attribute Information: 8x8 image of integer pixels in the range 0..16.
    :Missing Attribute Values: None
    :Creator: <NAME> (alpaydin '@' boun.edu.tr)
    :Date: July; 1998

This is a copy of the test set of the UCI ML hand-written digits datasets
https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits

The data set contains images of hand-written digits: 10 classes where
each class refers to a digit.

Preprocessing programs made available by NIST were used to extract
normalized bitmaps of handwritten digits from a preprinted form. From a
total of 43 people, 30 contributed to the training set and different 13
to the test set. 32x32 bitmaps are divided into nonoverlapping blocks of
4x4 and the number of on pixels are counted in each blo[...]<jupyter_text>データの大きさを確認<jupyter_code>digits.data.shape<jupyter_output><empty_output><jupyter_text>イメージデータの大きさを確認<jupyter_code>digits.images.shape<jupyter_output><empty_output><jupyter_text>1つ目のイメージの中身を確認各要素の数値はその座標の明るさ(0～15)で表される<jupyter_code>digits.images[0]<jupyter_output><empty_output><jupyter_text>データをプロット<jupyter_code>plt.imshow(digits.images[0], cmap='Greys')<jupyter_output><empty_output><jupyter_text>データの要素を表示<jupyter_code>digits.data[0]
データセットから訓練用データとテスト用データをランダムに抽出
random_stateはシード値
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size = 1/3, random_state = 1)<jupyter_output><empty_output><jupyter_text>学習前の初期設定 \隠れ層 50\活性化関数 シグモイド関数\ソルバ 確率的勾配降下法\学習率 0.001<jupyter_code>clf = MLPClassifier(hidden_layer_sizes=50, activation='logistic', solver='sgd', learning_rate_init=0.001)<jupyter_output><empty_output><jupyter_text>学習<jupyter_code>clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/neural_network/_multilayer_perceptron.py:571: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)<jupyter_text>出力器の活性化関数を確認<jupyter_code>clf.out_activation_
認識率を表示
clf.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>土地の価格の予想（回帰） ライブラリをインポート<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split

boston = load_boston()<jupyter_output><empty_output><jupyter_text>各要素を表示* bostonの要素* bostonの説明* データを5個表示* 教師データを5個表示* 特徴の名前を表示* データ配列の大きさを表示* 価格と各特徴の散布図を表示<jupyter_code>dir(boston)
print(boston.DESCR)
boston.data[:5]
boston.target[:5]
boston.feature_names
boston.data.shape
plt.figure(figsize=(20.0, 10.0))
for i in range(len(boston.feature_names)):
    plt.subplot(3, 5, i+1)
    plt.xlabel(boston.feature_names[i])
    plt.ylabel("Price(*1000$)")
    plt.scatter(boston.data[:,i], boston.target, color='b', marker='.')<jupyter_output><empty_output><jupyter_text>訓練用とテスト用のデータをランダムに抽出<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size = 1/4, random_state = 0)<jupyter_output><empty_output><jupyter_text>学習<jupyter_code>reg = MLPRegressor(hidden_layer_sizes=1000, random_state = 0)
reg.fit(X_train, y_train)<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/neural_network/_multilayer_perceptron.py:571: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)<jupyter_text>決定係数を表示\関数の正確さを表す\$0\leqq R^2 \leqq 1$\1に近いほど良い関数<jupyter_code>reg.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>ハイパーパラメータ ライブラリをインポート<jupyter_code>from sklearn.model_selection import cross_val_score
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_digits
import numpy as np
import matplotlib.pyplot as plt
import warnings

warnings.filterwarnings('ignore')

digits = load_digits()
X = digits.data
y = digits.target<jupyter_output><empty_output><jupyter_text>学習率を$10^{-6}$から$10^1$まで$10$倍ずつ変化させたときの認識率<jupyter_code>scores = []
for i in range(-6, 1):
    clf = MLPClassifier(hidden_layer_sizes=100, activation='logistic',
    solver='sgd',learning_rate_init=10**(i))
    clf.fit(X, y)
    scores.append(cross_val_score(clf, X, y, cv=3).mean())
x = np.arange(-6,1,1)
plt.plot(x, scores)
plt.xlabel("learning rate(10^x)")
plt.ylabel("accuracy(%)")<jupyter_output><empty_output><jupyter_text>学習率は$10^{-3}$から$10^{-1}$が良い  過学習 ライブラリをインポート<jupyter_code>from numpy import random
import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>誤差を持つ3次関数上の点をプロット<jupyter_code>a = 1.0
b = 2.0
c = 3.0

target_min = 0.0
target_max = 4.0

f = np.poly1d([a, b, c], True)
x = np.linspace(target_min, target_max, int(target_max-target_min)*20)

print(f)
plt.plot(x,f(x))

ave = 0.0
sd = 1.0
data_N = 15

y=[]
data_x = np.linspace(target_min, target_max, data_N)
for i in range(len(f(data_x))):
    y.append(f(data_x)[i] + random.normal(loc=0.0, scale=1.0))

plt.xlabel("x")
plt.ylabel("y")
plt.plot(data_x, y, 'go');<jupyter_output>3     2
1 x - 6 x + 11 x - 6<jupyter_text>近似する関数の次元を変えてプロット<jupyter_code>count = 1
plt.figure(figsize=(10.0, 6.0))
for order in range(1, 12, 2):
    estimated_parameters = np.polyfit(data_x, y, order)
    estimated_curve = np.poly1d(estimated_parameters)
    plt.subplot(2,3,count)
    plt.xlabel("x")
    plt.ylabel("y")
    plt.plot(x, estimated_curve(x))
    plt.plot(x,f(x),linestyle='dashed', color='black')
    plt.plot(data_x, y, 'go')
    plt.text(0, 5, "order="+str(order))
    count += 1
plt.tight_layout()
plt.show;<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12758620689655173, 'fraction_numerical': 0.026959247648902823, 'mean_word_length': 6.877777777777778, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 0, '>': 82, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '585614', 'license': 'MIT', 'n_tokens_mistral': 2906, 'n_tokens_neox': 2777, 'n_words': 430, 'path': 'ANN.ipynb', 'repo_name': 'Akutagawa50/Google_Colaboratory', 'size': '6385'}