程序說明:利用sklearn機器學習庫,調用svm方法訓練/預測數據結果,最後郵箱發送訓練進度和結果
Note: 需要安裝sklearn機器學習庫, matplotlib
# coding: utf-8
# 載入數據
from sklearn import datasets
digits = datasets.load_digits()
result = 0
# 利用svm訓練和預測
from sklearn import svm
clf = svm.SVC(gamma=0.001, C= 100.)
clf.fit(digits.data[:-1], digits.target[:-1])
x_data = digits.data[-1]
x = x_data.reshape(1, -1)
result = clf.predict(x)
# matplot繪圖
import matplotlib.pyplot as plt
plt.figure(1, (3, 3))
plt.imshow(digits.images[-1], cmap = plt.cm.gray_r, interpolation = 'nearest')
plt.show()
# 郵件發送訓練結果
output = "predict result is:" + str(result[0])
if result != 0:
import smtplib
from email.mime.text import MIMEText
mailto_list = ["××××@qq.com"]
mail_host = "smtp.126.com" # 設置服務器
mail_user = "××××" # 用戶名
mail_pass = "××××××××" # 代理登錄密碼
mail_postfix = "126.com" # 發件箱的後綴
def send_mail(to_list, sub):
me = "hello" + "<" + mail_user + "@" + mail_postfix + ">"
msg = MIMEText(output, _subtype='plain', _charset='gb2312')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user, mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception:
return False
if __name__ == '__main__':
if send_mail(mailto_list, "SVM訓練"):
print("發送成功")
else:
print("發送失敗")
else:
print('未發送')