1.參考攻略完成 "凱歌地牢" 1-22
2.走出凱哥地牢後開始挑戰 "邊遠地區的森林"
#coding:utf8
import os, sys
from imp import reload
reload(sys)
from win32com.client import Dispatch, constants, gencache
def doc2pdf(sourceF,tartgetF):
#sourceF = 'D:\\1.doc'
#tartgetF = 'D:\\1.pdf'
print(sourceF,' ==> ',tartgetF)
# enable python COM support for Word 2007
# this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library"
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
# 開始轉換
w = Dispatch("Word.Application")
try:
doc = w.Documents.Open(sourceF, ReadOnly=1)
doc.ExportAsFixedFormat(tartgetF, constants.wdExportFormatPDF, \
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
except: print('exception')
finally: w.Quit(constants.wdDoNotSaveChanges)
if os.path.isfile(tartgetF):print('translate success')
else:print('translate fail')
desktop = os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop')
print('DOC/DOCX to PDF batch converter . by K.Y. Lee (c) 2020 All Rights Reserved')
folder = input('enter the target folder of MS doc/docx files on YOUR Desktop\n輸入欲轉換之word文件資料夾名稱(桌面上)[不需完整路徑]:')
DIR = desktop+'\\'+folder+"\\"
files = os.listdir(DIR)
for f in files:
ii = f.find('.doc')
if ii>0:
doc2pdf(DIR+f,DIR+f[:ii]+'.pdf')
import numpy as np
import winreg
offset = 40
scale = 20
#取得桌面路徑
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
deskTop=winreg.QueryValueEx(key, "Desktop")[0]
x1 = np.loadtxt(deskTop+'/1.txt',delimiter=',',dtype='float')
print("已讀取 1 訓練資料共{}筆...".format(len(x1)))
x2 = np.loadtxt(deskTop+'/2.txt',delimiter=',',dtype='float')
print("已讀取 2 訓練資料共{}筆...".format(len(x1)))
x3 = np.loadtxt(deskTop+'/3.txt',delimiter=',',dtype='float')
print("已讀取 3 訓練資料共{}筆...".format(len(x1)))
# 將 x1,x2,x3 三個矩陣垂直合併
xx = np.vstack((x1,x2,x3))
已讀取 1 訓練資料共50筆... 已讀取 2 訓練資料共50筆... 已讀取 3 訓練資料共50筆...
y1 = np.array([1]*len(x1))
y2 = np.array([2]*len(x2))
y3 = np.array([3]*len(x3))
yy = np.vstack((y1,y2,y3)).flatten()
print(yy)
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3]
#資料對時間微分
xx_ = xx[:,2:]
dX =xx[:,:-2] - xx[:,2:]
xx = dX
#資料平移與縮放
xx = (xx+offset)/scale
print('samples(訓練樣本)')
print(xx)
print('labels(訓練樣本對應的答案標籤)')
print(yy)
samples(訓練樣本) [[2. 2. 2. ... 2. 2. 2. ] [2. 2. 2. ... 2. 2. 2. ] [2. 2. 2. ... 2. 2. 2. ] ... [1.5 1.85 1.3 ... 2. 2. 2. ] [1.45 1.85 1.35 ... 2. 2. 2. ] [1.75 1.85 1.95 ... 2.35 2.3 2.05]] labels(訓練樣本對應的答案標籤) [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3]
from sklearn.svm import SVC
clf=SVC()
#clf=SVC(probability=True)
clf.fit(xx,yy)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)
test=np.loadtxt(deskTop+'/TEST_123.txt',delimiter=',',dtype='float')
dTest =test[:,:-2] - test[:,2:]
test = dTest
test = (test+offset)/scale
print(clf.predict(test[0:10,:]))
print(clf.predict(test[10:20,:]))
print(clf.predict(test[20:30,:]))
[1 1 1 1 1 1 1 1 1 1] [2 2 2 2 2 2 2 2 2 2] [3 3 3 3 3 3 3 3 3 3]