1 # -*- coding: utf-8 -*- 2 import os, sys 3 path = r"D:\Program Files (x86)\libsvm-3.22\python" 4 sys.path.append(path) 5 from svmutil import * 6 7 8 y, x = svm_read_problem('data.txt') 9 prob = svm_problem(y, x) 10 param = svm_parameter('-t 1 -c 4 -b 1') 11 model = svm_train(prob, param) 12 svm_save_model('class3.model', model) 13 print("保存模型完成!") 14 model1 = svm_load_model('class3.model') 15 print("加载模型完成!") 16 #yt = [3] 17 yt = [int(y[1417])] 18 #xt = [{1: 89.0, 2: 112.0, 3: 111.0, 4: 96.0, 5: 82.0, 6: 55.0, 7: 103.0, 8: 103.0, 9: 72.0, 10: 87.0, 11: 89.0, 12: 71.0, 13: 108.0, 14: 97.0, 15: 95.0, 16: 85.0, 17: 83.0, 18: 62.0, 19: 92.0, 20: 94.0, 21: 83.0, 22: 92.0, 23: 82.0, 24: 54.0, 25: 82.0, 26: 89.0, 27: 91.0, 28: 94.0, 29: 76.0, 30: 71.0, 31: 60.0, 32: 71.0, 33: 70.0, 34: 75.0, 35: 64.0, 36: 63.0, 37: 77.0, 38: 106.0, 39: 101.0, 40: 102.0, 41: 93.0, 42: 52.0, 43: 90.0, 44: 101.0, 45: 92.0, 46: 94.0, 47: 103.0, 48: 67.0, 49: 96.0, 50: 104.0, 51: 104.0, 52: 105.0, 53: 105.0, 54: 77.0, 55: 76.0, 56: 89.0, 57: 93.0, 58: 95.0, 59: 87.0, 60: 70.0, 61: 72.0, 62: 85.0, 63: 96.0, 64: 100.0, 65: 95.0, 66: 86.0, 67: 94.0, 68: 94.0, 69: 92.0, 70: 98.0, 71: 102.0, 72: 95.0, 73: 87.0, 74: 111.0, 75: 113.0, 76: 113.0, 77: 106.0, 78: 77.0, 79: 92.0, 80: 109.0, 81: 106.0, 82: 103.0, 83: 118.0, 84: 91.0, 85: 83.0, 86: 107.0, 87: 108.0, 88: 111.0, 89: 119.0, 90: 105.0, 91: 70.0, 92: 83.0, 93: 94.0, 94: 96.0, 95: 97.0, 96: 88.0, 97: 72.0, 98: 84.0, 99: 100.0, 100: 97.0, 101: 100.0, 102: 87.0, 103: 86.0, 104: 89.0, 105: 91.0, 106: 92.0, 107: 103.0, 108: 88.0, 109: 81.0, 110: 104.0, 111: 107.0, 112: 103.0, 113: 93.0, 114: 63.0, 115: 92.0, 116: 95.0, 117: 85.0, 118: 84.0, 119: 100.0, 120: 77.0, 121: 88.0, 122: 84.0, 123: 88.0, 124: 88.0, 125: 96.0, 126: 88.0, 127: 73.0, 128: 75.0, 129: 79.0, 130: 86.0, 131: 87.0, 132: 84.0, 133: 81.0, 134: 79.0, 135: 87.0, 136: 87.0, 137: 88.0, 138: 80.0, 139: 61.0, 140: 65.0, 141: 67.0, 142: 66.0, 143: 66.0, 144: 70.0, 145: 71.0, 146: 84.0, 147: 92.0, 148: 92.0, 149: 94.0, 150: 50.0, 151: 83.0, 152: 92.0, 153: 81.0, 154: 83.0, 155: 91.0, 156: 62.0, 157: 82.0, 158: 97.0, 159: 86.0, 160: 74.0, 161: 81.0, 162: 80.0, 163: 83.0, 164: 97.0, 165: 82.0, 166: 83.0, 167: 96.0, 168: 87.0, 169: 81.0, 170: 81.0, 171: 85.0, 172: 88.0, 173: 90.0, 174: 72.0, 175: 66.0, 176: 68.0, 177: 79.0, 178: 78.0, 179: 80.0, 180: 86.0, 181: 67.0, 182: 89.0, 183: 94.0, 184: 99.0, 185: 102.0, 186: 65.0, 187: 82.0, 188: 99.0, 189: 91.0, 190: 87.0, 191: 96.0, 192: 69.0, 193: 74.0, 194: 92.0, 195: 95.0, 196: 91.0, 197: 102.0, 198: 93.0, 199: 78.0, 200: 107.0, 201: 104.0, 202: 98.0, 203: 111.0, 204: 92.0, 205: 78.0, 206: 86.0, 207: 94.0, 208: 87.0, 209: 86.0, 210: 76.0, 211: 89.0, 212: 86.0, 213: 98.0, 214: 98.0, 215: 112.0, 216: 102.0, 217: 64.0, 218: 95.0, 219: 98.0, 220: 106.0, 221: 118.0, 222: 83.0, 223: 90.0, 224: 107.0, 225: 96.0, 226: 96.0, 227: 97.0, 228: 63.0, 229: 93.0, 230: 92.0, 231: 95.0, 232: 103.0, 233: 94.0, 234: 73.0, 235: 85.0, 236: 89.0, 237: 100.0, 238: 116.0, 239: 104.0, 240: 73.0, 241: 70.0, 242: 85.0, 243: 89.0, 244: 92.0, 245: 80.0, 246: 68.0, 247: 82.0, 248: 88.0, 249: 89.0, 250: 99.0, 251: 98.0, 252: 89.0, 253: 71.0, 254: 87.0, 255: 94.0, 256: 95.0, 257: 80.0, 258: 57.0, 259: 88.0, 260: 86.0, 261: 76.0, 262: 81.0, 263: 79.0, 264: 71.0, 265: 95.0, 266: 76.0, 267: 76.0, 268: 72.0, 269: 72.0, 270: 64.0, 271: 82.0, 272: 83.0, 273: 84.0, 274: 84.0, 275: 80.0, 276: 52.0, 277: 74.0, 278: 84.0, 279: 79.0, 280: 84.0, 281: 70.0, 282: 54.0, 283: 70.0, 284: 81.0, 285: 78.0, 286: 73.0, 287: 72.0, 288: 75.0}] 19 xt=[x[1417]] 20 21 print(yt) 22 print(xt) 23 p_label, p_acc, p_val = svm_predict(yt, xt, model1, '-b 1') 24 print("标签", p_label) 25 print("p_acc", p_acc) 26 print("值", p_val)
python直接调用libsvm示例。
使用到的data.txt可以在下面获取到。
链接:https://pan.baidu.com/s/1j1sKr0vfpOUekWc_lJKiDA
提取码:szb8
来源:https://www.cnblogs.com/juluwangshier/p/12295553.html