您好,登錄后才能下訂單哦!
本文代碼來之《數(shù)據(jù)分析與挖掘?qū)崙?zhàn)》,在此基礎(chǔ)上補(bǔ)充完善了一下~
代碼是基于SVM的分類器Python實(shí)現(xiàn),原文章節(jié)題目和code關(guān)系不大,或者說給出已處理好數(shù)據(jù)的方法缺失、源是圖像數(shù)據(jù)更是不見蹤影,一句話就是練習(xí)分類器(▼㉨▼メ)
源代碼直接給好了K=30,就試了試怎么選的,挑選規(guī)則設(shè)定比較單一,有好主意請(qǐng)不吝賜教喲
# -*- coding: utf-8 -*- """ Created on Sun Aug 12 12:19:34 2018 @author: Luove """ from sklearn import svm from sklearn import metrics import pandas as pd import numpy as np from numpy.random import shuffle #from random import seed #import pickle #保存模型和加載模型 import os os.getcwd() os.chdir('D:/Analyze/Python Matlab/Python/BookCodes/Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)/圖書配套數(shù)據(jù)、代碼/chapter9/demo/code') inputfile = '../data/moment.csv' data=pd.read_csv(inputfile) data.head() data=data.as_matrix() #seed(10) shuffle(data) #隨機(jī)重排,按列,同列重排,因是隨機(jī)的每次運(yùn)算會(huì)導(dǎo)致結(jié)果有差異,可在之前設(shè)置seed n=0.8 train=data[:int(n*len(data)),:] test=data[int(n*len(data)):,:] #建模數(shù)據(jù) 整理 #k=30 m=100 record=pd.DataFrame(columns=['acurrary_train','acurrary_test']) for k in range(1,m+1): # k特征擴(kuò)大倍數(shù),特征值在0-1之間,彼此區(qū)分度太小,擴(kuò)大以提高區(qū)分度和準(zhǔn)確率 x_train=train[:,2:]*k y_train=train[:,0].astype(int) x_test=test[:,2:]*k y_test=test[:,0].astype(int) model=svm.SVC() model.fit(x_train,y_train) #pickle.dump(model,open('../tmp/svm1.model','wb'))#保存模型 #model=pickle.load(open('../tmp/svm1.model','rb'))#加載模型 #模型評(píng)價(jià) 混淆矩陣 cm_train=metrics.confusion_matrix(y_train,model.predict(x_train)) cm_test=metrics.confusion_matrix(y_test,model.predict(x_test)) pd.DataFrame(cm_train,index=range(1,6),columns=range(1,6)) accurary_train=np.trace(cm_train)/cm_train.sum() #準(zhǔn)確率計(jì)算 # accurary_train=model.score(x_train,y_train) #使用model自帶的方法求準(zhǔn)確率 pd.DataFrame(cm_test,index=range(1,6),columns=range(1,6)) accurary_test=np.trace(cm_test)/cm_test.sum() record=record.append(pd.DataFrame([accurary_train,accurary_test],index=['accurary_train','accurary_test']).T) record.index=range(1,m+1) find_k=record.sort_values(by=['accurary_train','accurary_test'],ascending=False) # 生成一個(gè)copy 不改變?cè)兞?find_k[(find_k['accurary_train']>0.95) & (find_k['accurary_test']>0.95) & (find_k['accurary_test']>=find_k['accurary_train'])] #len(find_k[(find_k['accurary_train']>0.95) & (find_k['accurary_test']>0.95)]) ''' k=33 accurary_train accurary_test 33 0.950617 0.95122 ''' ''' 計(jì)算一下整體 accurary_data 0.95073891625615758 ''' k=33 x_train=train[:,2:]*k y_train=train[:,0].astype(int) model=svm.SVC() model.fit(x_train,y_train) model.score(x_train,y_train) model.score(datax_train,datay_train) datax_train=data[:,2:]*k datay_train=data[:,0].astype(int) cm_data=metrics.confusion_matrix(datay_train,model.predict(datax_train)) pd.DataFrame(cm_data,index=range(1,6),columns=range(1,6)) accurary_data=np.trace(cm_data)/cm_data.sum() accurary_data
REF:
《數(shù)據(jù)分析與挖掘?qū)崙?zhàn)》
源代碼及數(shù)據(jù)需要可自取:https://github.com/Luove/Data
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。