質問編集履歴

4

コード囲み忘れ

2021/11/07 13:20

投稿

barobaro
barobaro

スコア1286

test CHANGED
File without changes
test CHANGED
@@ -158,6 +158,8 @@
158
158
 
159
159
 
160
160
 
161
+ ```python
162
+
161
163
  df4 = df.copy()
162
164
 
163
165
 
@@ -186,6 +188,8 @@
186
188
 
187
189
  sns.scatterplot(x='ta', y='m', hue='cluster', data=df4)
188
190
 
191
+ ```
192
+
189
193
 
190
194
 
191
195
  ![イメージ説明](32a22c50bf19936ab2aa15e0abc9e2e8.png)

3

LinearRegressionを追加

2021/11/07 13:20

投稿

barobaro
barobaro

スコア1286

test CHANGED
File without changes
test CHANGED
@@ -154,6 +154,44 @@
154
154
 
155
155
 
156
156
 
157
+ #### LinearRegression
158
+
159
+
160
+
161
+ df4 = df.copy()
162
+
163
+
164
+
165
+ from sklearn.linear_model import LinearRegression
166
+
167
+
168
+
169
+ lr = LinearRegression()
170
+
171
+ lr.fit(df4['ta'].values.reshape(-1, 1), df4['m'].values.reshape(-1, 1))
172
+
173
+ pred_y = lr.predict(df4['ta'].values.reshape(-1, 1)).reshape(-1)
174
+
175
+ df4['cluster'] = (df4['m'] < pred_y).astype(int)
176
+
177
+
178
+
179
+ for name, dfg in df4.groupby('cluster'):
180
+
181
+ lr.fit(dfg['ta'].values.reshape(-1, 1), dfg['m'].values.reshape(-1, 1))
182
+
183
+ print(name, lr.coef_, lr.intercept_)
184
+
185
+
186
+
187
+ sns.scatterplot(x='ta', y='m', hue='cluster', data=df4)
188
+
189
+
190
+
191
+ ![イメージ説明](32a22c50bf19936ab2aa15e0abc9e2e8.png)
192
+
193
+
194
+
157
195
  ### 補足情報(FW/ツールのバージョンなど)
158
196
 
159
197
 

2

df3を追加

2021/11/07 13:19

投稿

barobaro
barobaro

スコア1286

test CHANGED
File without changes
test CHANGED
@@ -124,6 +124,10 @@
124
124
 
125
125
  ```python
126
126
 
127
+ df3 = df.copy()
128
+
129
+
130
+
127
131
  from sklearn import cluster
128
132
 
129
133
 

1

サンプルデータ追加、GaussianMixtureとSpectralClusteringを追加

2021/11/06 23:02

投稿

barobaro
barobaro

スコア1286

test CHANGED
File without changes
test CHANGED
@@ -38,6 +38,18 @@
38
38
 
39
39
  ```python
40
40
 
41
+ import pandas as pd
42
+
43
+
44
+
45
+ url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSA9NhTNG6rcb1BAdVzC2RYgPPCCd0ryo1YconlDj7TK15IAO8rIi3uY9FzRCkXsj48BO4hWtceriKq/pub?gid=0&single=true&output=csv"
46
+
47
+
48
+
49
+ df = pd.read_csv(url)
50
+
51
+
52
+
41
53
  sns.scatterplot(x='ta', y='m', data=df)
42
54
 
43
55
  ```
@@ -48,7 +60,13 @@
48
60
 
49
61
 
50
62
 
63
+ #### KMeans
64
+
51
65
  ```python
66
+
67
+ df1 = df.copy()
68
+
69
+
52
70
 
53
71
  from sklearn.cluster import KMeans
54
72
 
@@ -56,17 +74,79 @@
56
74
 
57
75
  kmeans = KMeans(n_clusters=2, random_state=0)
58
76
 
77
+
78
+
59
- clusters = kmeans.fit(df)
79
+ clusters = kmeans.fit(df1)
80
+
81
+ df1['cluster'] = clusters.labels_
60
82
 
61
83
 
62
84
 
85
+ sns.scatterplot(x='ta', y='m', hue='cluster', data=df1)
86
+
87
+ ```
88
+
63
- df['cluster'] = clusters.labels_
89
+ ![イメージ説明](e2b2395c12b50c3ac062d8305b8cda6a.png)
64
90
 
65
91
 
66
92
 
93
+ #### GaussianMixture
94
+
95
+ ```python
96
+
97
+ df2 = df.copy()
98
+
99
+
100
+
101
+ from sklearn.mixture import GaussianMixture
102
+
103
+
104
+
105
+ model = GaussianMixture(n_components=2)
106
+
107
+ model.fit(df2)
108
+
109
+ df2['cluster'] = model.predict(df2)
110
+
111
+
112
+
67
- sns.scatterplot(x='ta', y='m', hue='cluster', data=df)
113
+ sns.scatterplot(x='ta', y='m', hue='cluster', data=df2)
68
114
 
69
115
  ```
116
+
117
+ ![イメージ説明](7ee8a9a1129e77289039593222a2f45b.png)
118
+
119
+
120
+
121
+ #### SpectralClustering
122
+
123
+
124
+
125
+ ```python
126
+
127
+ from sklearn import cluster
128
+
129
+
130
+
131
+ spectral = cluster.SpectralClustering(n_clusters=2, eigen_solver='arpack', affinity='nearest_neighbors')
132
+
133
+
134
+
135
+ clusters = spectral.fit(df3)
136
+
137
+
138
+
139
+ df3['cluster'] = clusters.labels_
140
+
141
+
142
+
143
+ sns.scatterplot(x='ta', y='m', hue='cluster', data=df3)
144
+
145
+ ```
146
+
147
+
148
+
149
+ ![イメージ説明](72b2494b8fffbf8c2194355d3d655215.png)
70
150
 
71
151
 
72
152