<jupyter_start><jupyter_text>데이터 전처리 (,제거)<jupyter_code>df_feature=df[['지정업종','tCO2','TJ']]
df_feature['tCO2']=df_feature.tCO2.str.replace(',','')
df_feature['tCO2']=df_feature.tCO2.drop(index=571,axis=0)
df_feature['tCO2']=pd.to_numeric(df_feature['tCO2'])
df_feature['TJ']=df_feature.TJ.str.replace(',','')
df_feature['TJ']=df_feature.TJ.drop(571,axis=0)
df_feature['TJ']=pd.to_numeric(df_feature['TJ'])
# X=df.iloc[:,:]
df_feature['지정업종']=df_feature.지정업종.drop(index=571,axis=0)
dfx_induty=pd.get_dummies(df_feature['지정업종'])
dfx_induty
df_feature.info()
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
scaler.fit(dfx_induty)
dfx_induty=scaler.transform(dfx_induty)<jupyter_output><empty_output><jupyter_text>군집분석(KMeans)  Make model, traing and prediction<jupyter_code>from sklearn.cluster import KMeans
model=KMeans(n_clusters=37,random_state=0,algorithm='auto')
model.fit(dfx_induty)
model.labels_
df_feature['지정업종']=model.labels_
pred = pd.DataFrame(model.predict(dfx_induty))
pred.columns=['pred']

# concatenate labels to df as a new column
r = pd.concat([df_feature,pred],axis=1)
r = r.dropna()
 print(r)<jupyter_output>지정업종      tCO2       TJ  pred
1      4.0   21850.0    341.0   4.0
2      4.0  109380.0   1727.0   4.0
3      4.0   14747.0    208.0   4.0
4      4.0    6755.0     96.0   7.0
5      7.0  111653.0   1697.0   4.0
...    ...       ...      ...   ...
1056   3.0   57458.0   1195.0   3.0
1057   3.0  191050.0   3883.0   3.0
1058   3.0  323533.0   7495.0   3.0
1059   3.0  881811.0  18213.0   3.0
1060   3.0  465138.0   6985.0   3.0

[1059 rows x 4 columns]<jupyter_text>Visualize result<jupyter_code>import matplotlib.pyplot as plt
centers = pd.DataFrame(model.cluster_centers_)
center_x = centers[0]
center_y = centers[1]

# scatter plot
plt.scatter(r['지정업종'],r['tCO2'],c=r['pred'])
plt.scatter(center_x,center_y,s=50,marker='D',c='r')
plt.show()<jupyter_output><empty_output><jupyter_text>k-means clustering<jupyter_code>plt.scatter(x='지정업종',y='tCO2',data=df_feature)
import scipy as sp
points = df_feature[['tCO2', '지정업종']]
points.head()
centroids = points.sample(37, random_state=0)
centroids
import numpy as np
# 각 데이터에 대하여, 각 중심점과의 유클리드 거리 계산
distance = sp.spatial.distance.cdist(points, centroids, 'euclidean')

# 가장 거리가 짧은 중심점의 cluster로 할당
cluster_num = np.argmin(distance, axis=1)

# 결과 확인
result = points.copy()
result['cluster'] = np.array(cluster_num)
result.head()
center_x = centroids['지정업종']
center_y = centroids['tCO2']

plt.scatter(x='지정업종', y='tCO2', c='cluster', data=result)
plt.scatter(center_x,center_y,s=50,marker='D',c='r')
plt.show()
centroids_2 = result.groupby('cluster').mean()
centroids_2
# 각 데이터에 대하여, 각 중심점과의 유클리드 거리 계산
distance2 = sp.spatial.distance.cdist(points, centroids_2, 'euclidean')

# 가장 거리가 짧은 중심점의 cluster로 할당
cluster_num2 = np.argmin(distance2, axis=1)

# 결과 확인
result2 = points.copy()
result2['cluster2'] = np.array(cluster_num2)
result2.head()
center_x = centroids['지정업종']
center_y = centroids['tCO2']

plt.scatter(x='지정업종', y='tCO2', c='cluster2', data=result2)
plt.scatter(center_x,center_y,s=50,marker='D',c='r')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1664020323912353, 'fraction_numerical': 0.07589711019371229, 'mean_word_length': 5.287425149700598, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '517058', 'license': 'Apache-2.0', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1584, 'n_words': 218, 'path': 'TeamProject_KMeansClustering.ipynb', 'repo_name': 'SolbiChoi/Project_Guardians', 'size': '3149'}