Magicode

# テスト投稿（Jupyter Notebook)

## JupyterNotebookで記事作成

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

### Irisデータのインポート

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)の桁数を指定

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,)

### 線形関数

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])

### 学習・評価

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])

### 重み・バイアスの確認

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

所感は下記の通りです。