k-Means 演算法:

k種聚類, k怎麼取?


測試資料:
| chinese | english | math |
| 100 | 86 | 91 |
| 30 | 55 | 45 |
| 50 | 75 | 42 |
| 62 | 85 | 93 |
| 98 | 100 | 99 |
| 71 | 53 | 56 |
| 60 | 60 | 51 |
import pandas as pd
import matplotlib.pyplot as plt
fpath = r”C:\Python\P107\doc\student_grades.csv”
df = pd.read_csv(fpath)
print(df)
lisChi = df[“chinese”].tolist()
lisEng = df[“english”].tolist()
lisMath= df[“math”].tolist()
fig = plt.figure()
ax = plt.axes( projection = “3d”)
#官網建議要放參數的話使用 plt.axes()
#或 plt.subplot(projection=”3d”)
ax.scatter(lisChi, lisEng, lisMath,
label = “students’ Grade distribution”,
color = “#ff00ff”, marker= “^”)
ax.set_xlabel(“Chinese”)
ax.set_ylabel(“English”)
ax.set_zlabel(“Math”)
for i in range(len(lisChi)):
textLabel = “(%d,%d,%d)” \
%(lisChi[i], lisEng[i], lisMath[i])
ax.text(lisChi[i],lisEng[i],lisMath[i],
s=textLabel)
ax.legend(loc=”best”)
plt.show()
“””
spyder內嵌的靜態圖片無法用滑鼠改變視角
console中輸入:
%matplotlib qt
再執行py檔,會生出可互動的獨立視窗
%matplotlib inline
切換回原本的內嵌模式
“””

print(df):

%matplotlib qt
可互動的3D散佈圖:

%matplotlib inline
內嵌無法互動的靜態3D散佈圖:

推薦hahow線上學習python: https://igrape.net/30afN
]*>.*?底下插入一個圖檔.*?</w:p>’, flags = re.DOTALL) ; new_xml, n = pattern.subn(”, xml, count=1)' title='Python正則替換:全面掌握 re.sub 與 re.subn 的差異與實戰 #substitute(替換); . 預設匹配「除\n以外的任意單一字元」; pattern = re.compile(r'<w:p[^>]*>.*?底下插入一個圖檔.*?</w:p>’, flags = re.DOTALL) ; new_xml, n = pattern.subn(”, xml, count=1)' loading='lazy' width=350 height=233 />








近期留言