-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_constructor.py
More file actions
26 lines (20 loc) · 1.17 KB
/
model_constructor.py
File metadata and controls
26 lines (20 loc) · 1.17 KB
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
import csv
import numpy as np
from sklearn.svm import LinearSVC
def get_features_weights(linearSVC: 'LinearSVC', vector_space_dict: dict, threshold=0) -> dict:
coefs = linearSVC.coef_[0]
weights = np.concatenate((coefs, linearSVC.intercept_), axis=0)
weights = np.array([w for w in weights if str(w) != '0.0'])
features = np.sort(list(vector_space_dict.keys()))
feature_weight_dict = {feature: coef for feature, coef in zip(features, coefs) if str(coef) != '0.0'}
feature_weight_dict['_intercept'] = linearSVC.intercept_[0]
feature_weight_dict['_threshold'] = threshold
feature_weight_dict['_total'] = np.absolute(weights).sum()
feature_weight_dict['_sum_benign_features'] = np.absolute(weights[weights < 0]).sum()
feature_weight_dict['_sum_malignant_features'] = np.absolute(weights[weights > 0]).sum()
return feature_weight_dict
def get_features_weights_csv(linearSVC: 'LinearSVC', vector_space_dict: dict, csv_name='feature_weight_mapping.csv'):
with open(csv_name, 'w', encoding='utf8') as f:
writer = csv.writer(f)
rows = list(get_features_weights(linearSVC, vector_space_dict).items())
writer.writerows(rows)