import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import scipy as sp
from matplotlib import pyplot
from pandas import Series,DataFrame
from sklearn.model_selection import train_test_split
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import seaborn as sns
import sklearn
data1=DataFrame(pd.read_csv("m_ave.csv"))
#気温データのcsvファイルを読み込み data1
jupyter notebookで上記のようにすると表示できます。
ソースコード
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import scipy as sp
from matplotlib import pyplot
from pandas import Series,DataFrame
from sklearn.model_selection import train_test_split
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import seaborn as sns
import sklearn
data1=DataFrame(pd.read_csv("m_ave.csv"))
data1
#data1の転置行列
t_data=data1.T
t_data
#kmeans.fitでクラスタ分けをする。
kmeans=KMeans(init='k-means++',n_clusters=4,n_init=10,max_iter=400)
kmeans.fit(data3)
y_pred=kmeans.predict(data3)
print(y_pred)
merge_data=pd.concat([data1,pd.DataFrame(y_pred)],axis=1)
#クラスタ分けをした結果をデータフレームに挿入する。
merge_data.columns=["feature1","feature2","2009","2010","2011","2012","2013","2014","2015","2016","2017","cluster"]
merge_data
ax=None
colors=['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf',"yellow","blue","black","green"]
for i,data in merge_data.groupby("cluster"):
ax=data.plot.scatter(x="feature1",y="feature2",color=colors[i],label=f'cluster{i}',ax=ax)
plt.savefig("class.png")
#出力結果を画像に表示する。