• Skip to main content
  • Skip to primary sidebar

学習記録

機械学習

LSTM時系列データ予測

2017年12月28日 by 河副 太智 Leave a Comment

LSTM(長短期記憶ユニット)とはRNN(再帰型ネットワーク)のひとつ
時系列データの解析、言語の解析、音声の解析、売上予測等に使用。

RNNには長期間の時系列を保持することが難しいが、
inputgateやfogetgate、ouputgateで問題がクリアできる。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# データセットの作成
def create_dataset(dataset, look_back):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return numpy.array(dataX), numpy.array(dataY)
# 乱数設定
numpy.random.seed(7)
# データセット読み込み
dataframe = read_csv('monthly-champagne-sales-in-1000s.csv', usecols=[1], engine='python', skipfooter=3)
dataset = dataframe.values
dataset = dataset.astype('float32')
# 訓練データ、テストデータ
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
 
# データスケーリング
scaler = MinMaxScaler(feature_range=(0, 1))
scaler_train = scaler.fit(train)
train = scaler_train.transform(train)
test = scaler_train.transform(test)
 
# データ作成
look_back = 10
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# データ整形
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# LSTMモデルの作成と学習
model = Sequential()
model.add(LSTM(64, return_sequences=True,input_shape=(look_back, 1)))
model.add(LSTM(32))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=10, batch_size=1, verbose=2)
# 予測データの作成
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# スケールしたデータを元に戻す
trainPredict = scaler_train.inverse_transform(trainPredict)
trainY = scaler_train.inverse_transform([trainY])
testPredict = scaler_train.inverse_transform(testPredict)
testY = scaler_train.inverse_transform([testY])
# 予測精度の計算
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# プロットのためのデータ整形
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# テストデータのプロット
plt.plot(dataframe[round(len(dataset)*0.67):])
plt.plot(testPredictPlot)
plt.show()

 

Filed Under: 教師有り, 機械学習

訓練データ、テストデータの作成

2017年12月27日 by 河副 太智 Leave a Comment

データを訓練用と訓練用に分ける

 

時系列分析の場合は前回のデータを基にするのでランダムにしない
一般的な分別の場合はランダムに分ける

 

◆一般的な分析の場合

1
2
3
4
y = data.quality
X = data.drop('quality', axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,random_state=123,stratify=y)

 

 

 

◆時系列分析の場合

前半67%を訓練用、残りはテスト用

1
2
3
4
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
print(len(train), len(test))

 

Filed Under: 教師有り, 時系列分析, 機械学習

confusion_matrix

2017年12月20日 by 河副 太智 Leave a Comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
print(__doc__)
 
import itertools
import numpy as np
import matplotlib.pyplot as plt
 
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
 
# import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target
class_names = iris.target_names
 
# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
 
# Run classifier, using a model that is too regularized (C too low) to see
# the impact on the results
classifier = svm.SVC(kernel='linear', C=0.01)
y_pred = classifier.fit(X_train, y_train).predict(X_test)
 
 
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')
 
    print(cm)
 
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)
 
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")
 
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
 
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, y_pred)
np.set_printoptions(precision=2)
 
# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names,
                      title='Confusion matrix, without normalization')
 
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
                      title='Normalized confusion matrix')
 
plt.show()

 

 

Filed Under: 教師有り, 機械学習

GridSearchで最良のgamma値とc値を算出

2017年12月16日 by 河副 太智 Leave a Comment

c値、交差検証のそれぞれ最良のパラメーターを調べる

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import Imputer
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
 
#読込
diabetes_df = pd.read_csv(
    'http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data',
    header=None)
 
#8列目が目的変数8列目を別にする
X = diabetes_df.iloc[:, :8]
y = diabetes_df.iloc[:, 8:].values.flatten() # 1次元に展開
 
 
# X shape: (768, 8), y shape: (768,)
print('X shape: {}, y shape: {}'.format(X.shape, y.shape))
 
 
#欠損値処理 (欠損値の指定defaultは'NaN',mean, median, mode のどれか,行か列かの指定
med_imp = Imputer(missing_values=0, strategy='median', axis=0)
med_imp.fit(X.iloc[:, 1:6])
X.iloc[:, 1:6] = med_imp.transform(X.iloc[:, 1:6])
 
# 学習データとテストデータを分ける
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
 
 
std_scl = StandardScaler()
std_scl.fit(X_train)
X_train = std_scl.transform(X_train)
X_test = std_scl.transform(X_test)
 
#svc分類、真偽陰陽にて判定
svc = SVC()
svc.fit(X_train, y_train)
print('Confusion matrix:\n{}'.format(confusion_matrix(y_test, svc.predict(X_test))))
 
 
#Cとgammaのそれぞれ0.001, 0.01, 0.1, 1, 10, 100の6つの値を計算
#6かける6で36パターンを表示
 
#交差検証は10分割
svc_param_grid = {
    'C': [0.001, 0.01, 0.1, 1, 10, 100],
    'gamma': [0.001, 0.01, 0.1, 1, 10, 100]
}
 
svc_grid_search = GridSearchCV(SVC(), svc_param_grid, cv=10)
svc_grid_search.fit(X_train, y_train)
 
print('Train score: {:.3f}'.format(svc_grid_search.score(X_train, y_train)))
print('Test score: {:.3f}'.format(svc_grid_search.score(X_test, y_test)))
 
print('Confusion matrix:\n{}'.format(confusion_matrix(y_test, svc_grid_search.predict(X_test))))

 

Filed Under: 教師有り, 機械学習

get.dummies

2017年12月15日 by 河副 太智 Leave a Comment

get_dummiesを使用して各値がどの分類に属するのかを0と1で表す

例えばキノコのデータの
http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data
gill_colorというのは

black=k,
brown=n,
buff=b,
chocolate=h,
gray=g,
green=r,
orange=o,
pink=p,
purple=u,
red=e,

というようにそれぞれの色を表している

 

http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data

のデータには左から10番目に色を表すアルファベットが入っている
一部省略されているので下の表では左から3番目

 

1
2
3
4
5
6
7
8
9
10
11
12
     gill_spacing gill_size <span style="color: #ff0000;">gill_color</span>   ...   stalk_surface_below_ring  \
0               c         n          k   ...                          s  
1               c         b          k   ...                          s  
2               c         b          n   ...                          s  
3               c         n          n   ...                          s  
4               w         b          k   ...                          s  
5               c         b          n   ...                          s  
6               c         b          g   ...                          s  
7               c         b          n   ...                          s  
8               c         n          p   ...                          s  
9               c         b          g   ...                          s  
10              c         b          g   ...                          s

 

データを扱いやすくするため以下のように設定する

 

#urlを読み込む
mush_data_url = “http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data”
s = requests.get(mush_data_url).content

#urlのデータをutf=8で読み込む
mush_data = pd.read_csv(io.StringIO(s.decode(“utf-8”)),header=None)

#データの一番上のカラムにそれぞれのアルファベットに対応した種類を割り当てる
mush_data.columns = [“classes”, “cap_shape”, “cap_surface”, “cap_color”, “odor”, “bruises”,
“gill_attachment”, “gill_spacing”, “gill_size”, “gill_color”, “stalk_shape”,
“stalk_root”, “stalk_surface_above_ring”, “stalk_surface_below_ring”,
“stalk_color_above_ring”, “stalk_color_below_ring”, “veil_type”, “veil_color”,
“ring_number”, “ring_type”, “spore_print_color”, “population”, “habitat”]

#gill_colorを対象とするのでmush_data_dummyにターゲットを絞る

mush_data_dummy = pd.get_dummies(
mush_data[[“gill_color”]])

#gill_colorがk,n,b,h,g,r,o,p,u,eの該当するものに1をそうでないものに0をつける

mush_data_dummy[“flg”] = mush_data[“classes”].map(
lambda x:1 if x == “p” else 0)

 

 

その結果、元のデータのgill_colorの項目でpに該当する8行目を対象にして

1
2
3
4
5
6
7
8
9
10
     gill_spacing gill_size gill_color   ...   stalk_surface_below_ring  \
0               c         n          k   ...                          s  
1               c         b          k   ...                          s  
2               c         b          n   ...                          s  
3               c         n          n   ...                          s  
4               w         b          k   ...                          s  
5               c         b          n   ...                          s  
6               c         b          g   ...                          s  
7               c         b          n   ...                          s  
8               c         n          <span style="color: #ff0000;"><strong>p</strong></span>   ...                          s

 

以下のようにpに該当するものを1に変更する

1
2
3
4
5
6
7
8
9
10
11
0       0
1       0
2       0
3       0
4       0
5       0
6       0
7       0
8       1
9       0
10      0

 

 

Filed Under: 教師有り, 機械学習

糖尿病データ

2017年12月15日 by 河副 太智 Leave a Comment

データのリンク

https://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data

アトリビュート(それぞれの行の数値が何を表しているか)

1. Number of times pregnant
2. Plasma glucose concentration a 2 hours in an oral glucose tolerance test
3. Diastolic blood pressure (mm Hg)
4. Triceps skin fold thickness (mm)
5. 2-Hour serum insulin (mu U/ml)
6. Body mass index (weight in kg/(height in m)^2)
7. Diabetes pedigree function
8. Age (years)
9. Class variable (0 or 1)

 

一行目の例

6,1. Number of times pregnant
148,Plasma glucose concentration a 2 hours in an oral glucose tolerance test
72,Diastolic blood pressure (mm Hg)
35,Triceps skin fold thickness (mm)
0,2-Hour serum insulin (mu U/ml)
33.6,Body mass index (weight in kg/(height in m)^2)
0.627,Diabetes pedigree function
50,Age (years)
1,Class variable (0 or 1)

Filed Under: 教師有り, 機械学習

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • Page 5
  • Page 6
  • Interim pages omitted …
  • Page 9
  • Go to Next Page »

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in