Skip to content
Snippets Groups Projects
Commit ccd860db authored by Rawel's avatar Rawel
Browse files

refactoring

parent 3b45ca32
No related branches found
No related tags found
No related merge requests found
import os
import sys
import numpy as np
......@@ -8,6 +9,8 @@ from sklearn.preprocessing import KBinsDiscretizer
from sklearn.svm import LinearSVC
sys.setrecursionlimit(100000000)
abspath = os.path.dirname(os.path.abspath(__file__))
os.chdir(abspath)
class Svm:
......@@ -51,12 +54,12 @@ class Svm:
def evaluate_set(self, vcc_feature_vectors, unclassified_feature_vectors):
""""""
'''
"""
feature_vectors = self.preprocess(vcc_feature_vectors[0][0])
for feature_vector in vcc_feature_vectors[1:]+unclassified_feature_vectors:
feature_vectors = vstack((feature_vectors, self.preprocess(feature_vector[0])))
dump(feature_vectors, "Vectors/testing_set.joblib")
'''
"""
feature_vectors = load("Vectors/testing_set.joblib")
confidences = self.model.decision_function(feature_vectors)
......@@ -102,30 +105,29 @@ class Svm:
open("x_own", "w+").write(str(rec))
open("y_own", "w+").write(str(pre))
def train_model(self, vcc_feature_vectors_and_weigts, unclassified_feature_vectors, fs=303, c=1, weight=1):
def train_model(self, vcc_feature_vectors_and_weights, unclassified_feature_vectors, fs=303, c=1, weight=1):
labels = []
weights = []
'''
"""
vcc_feature_vectors = [x[0] for x in vcc_feature_vectors_and_weigts]
fit_vectors = [x.tocsc()[0, :303] for x in vcc_feature_vectors+unclassified_feature_vectors]
# preprocess fit
self.preprocess_fit(fit_vectors)
#dump(self.preprocessing, "Models/preprocessing.joblib")
'''
"""
# self.preprocessing = load("Models/preprocessing.joblib")
'''
"""
feature_vectors = self.preprocess(vcc_feature_vectors[0])
for feature_vector in vcc_feature_vectors[1:]+unclassified_feature_vectors:
feature_vectors = vstack((feature_vectors, self.preprocess(feature_vector)))
#dump(feature_vectors, "feature_vectors.joblib")
'''
"""
feature_vectors = load("feature_vectors.joblib")
for i, vector in enumerate(vcc_feature_vectors_and_weigts):
for i, vector in enumerate(vcc_feature_vectors_and_weights):
labels.append(1)
weights.append(vcc_feature_vectors_and_weigts[i][1] * weight)
weights.append(vcc_feature_vectors_and_weights[i][1] * weight)
for i in range(len(unclassified_feature_vectors)):
labels.append(0)
weights.append(1)
......@@ -134,7 +136,7 @@ class Svm:
self.model = LinearSVC(C=c, max_iter=100000000)
# feature selection using k best
'''
"""
print("feature selection...")
self.kbest = SelectKBest(chi2, k=int(fs))
feature_vectors_best = self.kbest.fit_transform(feature_vectors_scaled, labels)
......@@ -145,14 +147,14 @@ class Svm:
self.feature_select.fit(feature_vectors_scaled, labels)
feature_selected_vectors = self.feature_select.transform(feature_vectors_scaled)
print(feature_selected_vectors.shape)
'''
"""
'''
"""
self.model.fit(feature_vectors, labels, weights)
print("Score:", str(self.model.score(feature_vectors, labels)))
print("Done")
self.confidences = {}
'''
"""
k = 5
scores = cross_val_score(self.model, feature_vectors, labels, cv=k)
print(scores)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment