Method of implementing classifier based on SVM by Python

  • 2021-07-24 11:07:08
  • OfStack

The code of this article comes from "data analysis and mining actual combat", and on this basis, it supplements and perfects 1 ~

The code is based on the classifier Python of SVM. The title of the original chapter has little to do with code, or the method of processing the data is missing. The source is the image data, and it is missing. One sentence is to practice the classifier

Source code directly to good K=30, tried how to choose, select rules set comparison single 1, have a good idea please feel free to give yo


# -*- 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 # Save model and load model 
import os


os.getcwd()
os.chdir('D:/Analyze/Python Matlab/Python/BookCodes/Python Data analysis and mining actual combat / Book supporting data and codes /chapter9/demo/code')
inputfile = '../data/moment.csv'
data=pd.read_csv(inputfile)

data.head()
data=data.as_matrix()
#seed(10)
shuffle(data) # Random Rearrangement, Rearrangement by Column, Rearrangement by Column, Rearrangement by Column, Random Rearrangement by Column, Random Rearrangement by Column, seed
n=0.8
train=data[:int(n*len(data)),:]
test=data[int(n*len(data)):,:]

# Modeling data   Finishing 
#k=30 
m=100
record=pd.DataFrame(columns=['acurrary_train','acurrary_test']) 
for k in range(1,m+1):
  # k Feature expansion multiple, eigenvalue in 0-1 Between, the discrimination between each other is too small, and it is expanded to improve the discrimination and accuracy 
  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'))# Save model 
  #model=pickle.load(open('../tmp/svm1.model','rb'))# Loading model 
  # Model evaluation   Confusion matrix 
  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()   # Accuracy calculation 
#  accurary_train=model.score(x_train,y_train)             # Use model Self-contained method to find accuracy 
  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) #  Generate 1 A copy  Do not change the original variable 
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
'''
'''  Calculation 1 Lower whole  
 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:

[Data Analysis and Mining]

Source code and data requirements can be obtained by yourself: https://github.com/Luove/Data


Related articles: