Ccmmutty logo
Commutty IT
3 min read

テスト投稿(Jupyter Notebook)

https://cdn.magicode.io/media/notebox/e05f05c2-6037-4e37-bbbf-0a62685c30ce.jpeg

JupyterNotebookで記事作成

 JupyterNotebookを挿入できるとのことだったため、実際に実験してみました。
下記がJuypter Notebook

Irisデータのインポート

python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

import sklearn
from sklearn import datasets
from sklearn.model_selection import train_test_split
np.set_printoptions(precision=3) # 出力値(ndarray)の桁数を指定

iris = datasets.load_iris()
datas, target = iris.data, iris.target
df_data = pd.DataFrame(datas, columns=iris.feature_names)
x_train, x_test, y_train, y_test = train_test_split(datas, target, test_size=0.2, random_state=0) # 学習データとテストデータへ7:3で分割

print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

(120, 4) (30, 4) (120,) (30,)

線形関数

python
from sklearn.linear_model import LinearRegression

model_lin = LinearRegression()
model_lin.fit(x_train,y_train)
y_pred = model_lin.predict(x_test)
model_lin.score(x_test, y_test)

model_lin.coef_

array([-0.106, -0.04 , 0.229, 0.611])

学習・評価

python
from sklearn.linear_model import LogisticRegression

logit = LogisticRegression(max_iter=1000)
logit.fit(x_train, y_train)

y_pred = logit.predict(x_test)
logit.score(x_test, y_test)

y_pred

array([2, 1, 0, 2, 0, 2, 0, 1, 1, 1, 2, 1, 1, 1, 1, 0, 1, 1, 0, 0, 2, 1, 0, 0, 2, 0, 0, 1, 1, 0])

重み・バイアスの確認

python
weight= np.array([[-0.394,  0.851, -2.36 , -1.012],
       [ 0.41 , -0.319, -0.146, -0.788],
       [-0.016, -0.532,  2.506,  1.8  ]])

bias = np.array([  9.273,   2.317, -11.59 ])

x = x_test[0]
y = weight@x+bias
y_softmax = np.exp(y)/np.exp(y).sum()

1.0

まとめ

 所感は下記の通りです。
  • Jupyter Notebookがそのまま挿入(Upload)できるので非常に便利
  • 先にcodeやMarkdownで記事追加後にUploadするとデータが全部消し飛ぶため、①Jupyter NotebookのUpload、②追加記事記載 の手順は重要である。
  • 普段のJupyter Notebookはマークダウンの見出しまでは丁寧に書いていないため、今後のJupyter Notebook作成時は考慮したい。

Discussion

コメントにはログインが必要です。