1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| from sklearn.preprocessing import StandardScaler, MinMaxScaler from sklearn.model_selection import KFold, train_test_split import numpy as np from libsvm.svmutil import *
import read
def classify(param_str="-t 2 -c 1", scaler=None): X_array, y_array = read.getdata() X_train, X_val, y_train, y_val = train_test_split(X_array, y_array, test_size=0.3, random_state=42)
if scaler is not None: X_train = scaler.fit_transform(X_train) X_val = scaler.fit_transform(X_val) prob = svm_problem(y_train.tolist(), X_train.tolist())
param = svm_parameter(param_str)
model = svm_train(prob, param)
print("\nvalid acc: ") labels, acc, vals = svm_predict(y_val.tolist(), X_val.tolist(), model)
print("train acc: ") labels, acc, vals = svm_predict(y_train.tolist(), X_train.tolist(), model)
if __name__ == "__main__":
scaler = StandardScaler()
classify(scaler=scaler)
|