質問編集履歴
1
コード全体を追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -17,3 +17,96 @@
|
|
17
17
|
なお、h5pyのバージョンは2.10.0、Kerasのバージョンは2.8.0、Tensorflowのバージョンは1.14.0です。
|
18
18
|
|
19
19
|
不足している情報などあればお伝えください。よろしくお願いします。
|
20
|
+
|
21
|
+
追記
|
22
|
+
指摘があったため、以下にコード全体を載せます。参考にしていたサイトはIkemen Mas Kot氏のhttps://qiita.com/maskot1977/items/b2cfb369f60ad02887b0です。
|
23
|
+
|
24
|
+
```python3.7
|
25
|
+
# 数値計算やデータフレーム操作に関するライブラリをインポートする
|
26
|
+
import numpy as np
|
27
|
+
import pandas as pd
|
28
|
+
import statsmodels.api as sm
|
29
|
+
import matplotlib.pyplot as plt
|
30
|
+
from sklearn.preprocessing import StandardScaler
|
31
|
+
import csv
|
32
|
+
|
33
|
+
# 図やグラフを図示するためのライブラリをインポートする。
|
34
|
+
#%matplotlib inline
|
35
|
+
import matplotlib.pyplot as plt
|
36
|
+
from pandas import plotting
|
37
|
+
|
38
|
+
# 機械学習関連のライブラリ群
|
39
|
+
|
40
|
+
from sklearn.model_selection import train_test_split # 訓練データとテストデータに分割
|
41
|
+
from sklearn.metrics import confusion_matrix # 混合行列
|
42
|
+
|
43
|
+
from sklearn.decomposition import PCA #主成分分析
|
44
|
+
from sklearn.linear_model import LogisticRegression # ロジスティック回帰
|
45
|
+
from sklearn.neighbors import KNeighborsClassifier # K近傍法
|
46
|
+
from sklearn.svm import SVC # サポートベクターマシン
|
47
|
+
from sklearn.tree import DecisionTreeClassifier # 決定木
|
48
|
+
from sklearn.ensemble import RandomForestClassifier # ランダムフォレスト
|
49
|
+
from sklearn.ensemble import AdaBoostClassifier # AdaBoost
|
50
|
+
from sklearn.naive_bayes import GaussianNB # ナイーブ・ベイズ
|
51
|
+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA# 線形判別分析
|
52
|
+
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA # 二次判別分析
|
53
|
+
|
54
|
+
# データを読み込む
|
55
|
+
df = pd.read_csv('./data/wine.csv')
|
56
|
+
|
57
|
+
# 標準化
|
58
|
+
scaler = StandardScaler()
|
59
|
+
scaler.fit(np.array(df))
|
60
|
+
df_std = scaler.transform(np.array(df))
|
61
|
+
df_std = pd.DataFrame(df_std,columns=df.columns)
|
62
|
+
|
63
|
+
# qualityが6未満は0,6以上は1に分類
|
64
|
+
df["class"] = [0 if i < 6 else 1 for i in df['quality'].tolist()]
|
65
|
+
|
66
|
+
# col_name取得
|
67
|
+
col_name = df.columns
|
68
|
+
#print(col_name[:12])
|
69
|
+
|
70
|
+
# それぞれに与える色を決める。
|
71
|
+
color_codes = {0:'#0000FF', 1:'#FF0000'}
|
72
|
+
colors = [color_codes[x] for x in df['class'].tolist()]
|
73
|
+
|
74
|
+
# 散布図行列の作成(全要素の一対一の相関関係)
|
75
|
+
# 2変数に強い相関があるなら片方に絞る
|
76
|
+
# 一定値しかとらない変数は削除
|
77
|
+
# plotting.scatter_matrix(df.dropna(axis=1)[df.columns[:-1]], figsize=(20, 20), color=colors, alpha=0.5)
|
78
|
+
# plt.show()
|
79
|
+
|
80
|
+
"""
|
81
|
+
# 主成分分析
|
82
|
+
dfs = df.apply(lambda x: (x-x.mean())/x.std(), axis=0).fillna(0) # データの正規化
|
83
|
+
pca = PCA()
|
84
|
+
pca.fit(dfs.iloc[:, :12])
|
85
|
+
# データを主成分空間に写像 = 次元圧縮
|
86
|
+
feature = pca.transform(dfs.iloc[:, :12])
|
87
|
+
#plt.figure(figsize=(6, 6))
|
88
|
+
plt.scatter(feature[:, 0], feature[:, 1], alpha=0.5, color=colors)
|
89
|
+
plt.title("Principal Component Analysis")
|
90
|
+
plt.xlabel("The first principal component")
|
91
|
+
plt.ylabel("The second principal component")
|
92
|
+
plt.grid()
|
93
|
+
plt.show()
|
94
|
+
"""
|
95
|
+
|
96
|
+
X = df.iloc[:, 1:12] # 説明変数(Column1は明らかに関係ないので外す)
|
97
|
+
print(X.columns)
|
98
|
+
y = df.iloc[:, 13] # 目的変数
|
99
|
+
print(X)
|
100
|
+
print(y)
|
101
|
+
|
102
|
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4) # 訓練データ・テストデータへのランダムな分割(40%がテスト60%が訓練)
|
103
|
+
|
104
|
+
clf = LogisticRegression() #モデルの生成
|
105
|
+
clf.fit(X_train, y_train) #学習
|
106
|
+
|
107
|
+
# 正解率 (train) : 学習に用いたデータをどのくらい正しく予測できるか
|
108
|
+
clf.score(X_train,y_train)
|
109
|
+
|
110
|
+
# 正解率 (test) : 学習に用いなかったデータをどのくらい正しく予測できるか
|
111
|
+
clf.score(X_test,y_test)
|
112
|
+
```
|