forked from farismismar/DL-CoMP-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_svm.py
143 lines (109 loc) · 3.81 KB
/
ml_svm.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 28 17:48:29 2019
@author: farismismar
"""
import os
###################################################
#os.chdir('/Users/farismismar/Desktop/DL CoMP/')
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
import random
from sklearn.metrics import roc_auc_score, accuracy_score
from scipy.io import loadmat
# Set the random seed
seed = 0
# save X and Y dimensions
mX, nX = [0,0]
mY, nY = [0,0]
y_pred = None # placeholder
model = None # the forward definition.
def initialize_wrapper(random_state):
global seed
seed = random_state
np.random.seed(random_state)
random.seed(random_state)
def predict_wrapper(RSRP, SINR_1, SINR_2, rank):
X_test = pd.DataFrame({'RSRP': RSRP,
'TBSINR_1': SINR_1,
#'TBSINR_2': SINR_2,
'rank' :rank}, index=[0])
global model
global y_pred
X_test.fillna(0, inplace=True, axis=0)
#print(X_test)
try:
y_pred = model.predict(X_test)
print('INFO: Prediction is y = {}'.format(y_pred[0]));
except:
#print('WARNING: Invalid prediction using {}'.format(X_test.values));
y_pred = np.nan
return y_pred
# returns the test error
def train_wrapper(filename): # filename='measurements.mat'
global dataset
global seed
global model
global mX
global nX
global mY
global nY
data = loadmat(filename) # this is a dict.
keys = list(data.keys())[3:] # skip the first three columns
values = list(data.values())[3:]
dataset = pd.DataFrame()
dataset = dataset.reindex(columns = keys) # create an empty dataframe
for ii in np.arange(len(values)):
v_ = np.array(values[ii])
dataset[keys[ii]] = pd.Series(v_.flatten()) # cannot add the data to this empty df.
dataset['y'] = 1*(dataset['BLER'] <= 0.1) # H-ARQ target.
dataset = dataset[['RSRP', 'TBSINR_1', 'rank', 'y']]
dataset.dropna(inplace=True, axis=0)
if os.path.exists('dataset.csv'):
dataset.to_csv('dataset.csv', index=False, mode='a', header=False) # append
else:
dataset.to_csv('dataset.csv', index=False)
# Perform a split 30-70
train, test = train_test_split(dataset, test_size=0.30, random_state=seed)
X_train = train.drop('y', axis=1)
X_test = test.drop('y', axis=1)
y_train = train['y'].values
y_test = test['y'].values
mX, nX = X_train.shape
mY = y_train.shape
nY = 1
# The hyperparameters
p = 4
Cs = [0.01, 1, 10]
kernels = ['rbf', 'linear', 'poly']
param_grid = {'C': Cs, 'kernel': kernels}
grid_search = GridSearchCV(SVC(degree=p, gamma='scale', class_weight='balanced', probability=True), param_grid, verbose=True, n_jobs=1, cv=3) # p-degree kernel.
try:
grid_result = grid_search.fit(X_train, y_train)
except:
print('WARNING: Single class classifier. Fallback.')
return [np.nan, np.nan]
# This is the best model
best_model_svm = grid_result.best_params_
print(best_model_svm)
model = grid_result.best_estimator_
svc = model
y_pred = svc.predict(X_test)
y_score = svc.predict_proba(X_test)
mu = accuracy_score(y_test, y_pred)
# Compute ROC curve and ROC area
try:
roc_auc = roc_auc_score(y_test, y_score[:,1])
except:
print('WARNING: ROC was not computed. Returning NaN');
roc_auc = np.nan
print('ROC for training is: {}'.format(roc_auc))
print('Misclassification error for training is: {:.3f}'.format(1-mu))
return [roc_auc, 1-mu] # model is valid
# Local code for debugging
#initialize_wrapper(seed)
#train_wrapper('measurements.mat')