本文整理汇总了Python中sklearn.svm.SVC.decision_function方法的典型用法代码示例。如果您正苦于以下问题:Python SVC.decision_function方法的具体用法?Python SVC.decision_function怎么用?Python SVC.decision_function使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.svm.SVC
的用法示例。
在下文中一共展示了SVC.decision_function方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_qbc
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def test_qbc(self):
mean_1 = np.array([-2, 0])
mean_2 = np.array([2, 0])
cov = np.array([[1, 0], [0, 1]])
X_1 = np.random.multivariate_normal(mean_1, cov, 100)
X_2 = np.random.multivariate_normal(mean_2, cov, 200)
X = np.vstack([X_1, X_2])
y = np.ones(X.shape[0])
y[101:] = -1
# shuffle data
p = np.random.permutation(X.shape[0])
X = X[p]
y = y[p]
y = ObstructedY(y)
y.query(np.random.randint(0, X.shape[0], 50))
model = SVC(C=1, kernel='linear')
model.fit(X[y.known], y[y.known])
pick, _ = query_by_bagging(X, y, current_model=None, base_model=model,
batch_size=50, rng=self.rng, n_bags=5, method='entropy')
mean_picked_dist = np.abs(model.decision_function(X[pick])).mean()
not_picked = [i for i in xrange(X.shape[0]) if i not in set(pick)]
mean_unpicked_dist = np.abs(model.decision_function(X[not_picked])).mean()
self.assertTrue(mean_picked_dist < mean_unpicked_dist)
示例2: plot_decision_threshold
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def plot_decision_threshold():
from mglearn.datasets import make_blobs
from sklearn.svm import SVC
try:
from sklearn.model_selection import train_test_split
except:
from sklearn.cross_validation import train_test_split
X, y = make_blobs(n_samples=(400, 50), centers=2, cluster_std=[7.0, 2],
random_state=22)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
fig, axes = plt.subplots(2, 3, figsize=(15, 8))
plt.suptitle("decision_threshold")
axes[0, 0].set_title("training data")
axes[0, 0].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
svc = SVC(gamma=.05).fit(X_train, y_train)
axes[0, 1].set_title("decision with threshold 0")
axes[0, 1].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
plot_2d_scores(svc, X_train, function="decision_function", alpha=.7,
ax=axes[0, 1])
plot_2d_separator(svc, X_train, linewidth=3, ax=axes[0, 1])
axes[0, 2].set_title("decision with threshold -0.8")
axes[0, 2].scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm)
plot_2d_separator(svc, X_train, linewidth=3, ax=axes[0, 2], threshold=-.8)
plot_2d_scores(svc, X_train, function="decision_function", alpha=.7,
ax=axes[0, 2])
axes[1, 0].set_visible(False)
mask = np.abs(X_train[:, 1] - 7) < 5
bla = np.sum(mask)
line = np.linspace(X_train.min(), X_train.max(), 100)
axes[1, 1].set_title("Cross-section with threshold 0")
axes[1, 1].plot(line, svc.decision_function(np.c_[line, 10 * np.ones(100)]), c='k')
contour = (svc.decision_function(np.c_[line, 10 * np.ones(100)]) > 0).reshape(1, -1).repeat(10, axis=0)
axes[1, 1].contourf(line, np.linspace(-1.5, 1.5, 10), contour, alpha=0.2, cmap=cm)
axes[1, 1].scatter(X_train[mask, 0], np.zeros(bla), c=y_train[mask], cmap=cm, alpha=.1, s=100)
axes[1, 1].set_xlim(X_train.min(), X_train.max())
axes[1, 1].set_ylim(-1.5, 1.5)
axes[1, 1].set_xticks(())
axes[1, 1].set_ylabel("Decision value")
contour2 = (svc.decision_function(np.c_[line, 10 * np.ones(100)]) > -.8).reshape(1, -1).repeat(10, axis=0)
axes[1, 2].set_title("Cross-section with threshold -0.8")
axes[1, 2].contourf(line, np.linspace(-1.5, 1.5, 10), contour2, alpha=0.2, cmap=cm)
axes[1, 2].scatter(X_train[mask, 0], np.zeros(bla), c=y_train[mask], cmap=cm, alpha=.1, s=100)
axes[1, 2].plot(line, svc.decision_function(np.c_[line, 10 * np.ones(100)]), c='k')
axes[1, 2].set_xlim(X_train.min(), X_train.max())
axes[1, 2].set_ylim(-1.5, 1.5)
axes[1, 2].set_xticks(())
axes[1, 2].set_ylabel("Decision value")
示例3: plot_decision_threshold
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def plot_decision_threshold():
from mglearn.datasets import make_blobs
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
X, y = make_blobs(n_samples=(400, 50), centers=2, cluster_std=[7.0, 2],
random_state=22)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
fig, axes = plt.subplots(2, 3, figsize=(15, 8), subplot_kw={'xticks': (), 'yticks': ()})
plt.suptitle("decision_threshold")
axes[0, 0].set_title("training data")
discrete_scatter(X_train[:, 0], X_train[:, 1], y_train, ax=axes[0, 0])
svc = SVC(gamma=.05).fit(X_train, y_train)
axes[0, 1].set_title("decision with threshold 0")
discrete_scatter(X_train[:, 0], X_train[:, 1], y_train, ax=axes[0, 1])
plot_2d_scores(svc, X_train, function="decision_function", alpha=.7,
ax=axes[0, 1], cm=ReBl)
plot_2d_separator(svc, X_train, linewidth=3, ax=axes[0, 1])
axes[0, 2].set_title("decision with threshold -0.8")
discrete_scatter(X_train[:, 0], X_train[:, 1], y_train, ax=axes[0, 2])
plot_2d_separator(svc, X_train, linewidth=3, ax=axes[0, 2], threshold=-.8)
plot_2d_scores(svc, X_train, function="decision_function", alpha=.7,
ax=axes[0, 2], cm=ReBl)
axes[1, 0].set_axis_off()
mask = np.abs(X_train[:, 1] - 7) < 5
bla = np.sum(mask)
line = np.linspace(X_train.min(), X_train.max(), 100)
axes[1, 1].set_title("Cross-section with threshold 0")
axes[1, 1].plot(line, svc.decision_function(np.c_[line, 10 * np.ones(100)]), c='k')
dec = svc.decision_function(np.c_[line, 10 * np.ones(100)])
contour = (dec > 0).reshape(1, -1).repeat(10, axis=0)
axes[1, 1].contourf(line, np.linspace(-1.5, 1.5, 10), contour, alpha=0.4, cmap=cm)
discrete_scatter(X_train[mask, 0], np.zeros(bla), y_train[mask], ax=axes[1, 1])
axes[1, 1].set_xlim(X_train.min(), X_train.max())
axes[1, 1].set_ylim(-1.5, 1.5)
axes[1, 1].set_xticks(())
axes[1, 1].set_ylabel("Decision value")
contour2 = (dec > -.8).reshape(1, -1).repeat(10, axis=0)
axes[1, 2].set_title("Cross-section with threshold -0.8")
axes[1, 2].contourf(line, np.linspace(-1.5, 1.5, 10), contour2, alpha=0.4, cmap=cm)
discrete_scatter(X_train[mask, 0], np.zeros(bla), y_train[mask], alpha=.1, ax=axes[1, 2])
axes[1, 2].plot(line, svc.decision_function(np.c_[line, 10 * np.ones(100)]), c='k')
axes[1, 2].set_xlim(X_train.min(), X_train.max())
axes[1, 2].set_ylim(-1.5, 1.5)
axes[1, 2].set_xticks(())
axes[1, 2].set_ylabel("Decision value")
axes[1, 0].legend(['negative class', 'positive class'])
示例4: main
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def main():
tweets_fname = 'your_tweets.txt'
labels_fname = 'your_labels.txt'
# fill in with your own tweets and labels
dictionary = get_words(tweets_fname)
feature_vectors = get_features(tweets_fname, dictionary)
labels = get_vectors(labels_fname)
first_few_features = feature_vectors[:560]
first_few_labels = labels[:560]
last_few_features = feature_vectors[560:]
last_few_labels = labels[560:]
k = 5
# this finds how accurate your measure is
# metrics = [ "accuracy", "f1_score", "auroc", "precision",
# "sensitivity", "specificity" ]
# for metric in metrics :
# print str(metric) + ": "
# c, gamma = get_rbf(first_few_features, first_few_labels,
# k, metric)
# print "best c: " + str(c) + ", best gamma: " + str(gamma)
# rbf_clf = SVC(kernel='rbf', C=c, gamma=gamma)
# rbf_clf.fit(first_few_features, first_few_labels)
# perf, lower, upper = get_confidence_interval(rbf_clf, last_70_features,
# last_70_labels, metric)
# print "peformance: " + str(perf) + ", lower: " + str(lower) + ", upper: " + str(upper)
X_test = get_features('your_other_tweets.txt', dictionary)
clf = SVC(kernel='rbf', gamma=0.01, C=100, probability=True)
clf.fit(feature_vectors,labels)
y_pred = np.sign(clf.decision_function(X_test))
print(y_pred)
示例5: MIKernelSVM
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
class MIKernelSVM(object):
def __init__(self, **parameters):
svm_params = {'kernel' : 'precomputed'}
if 'C' in parameters:
svm_params['C'] = parameters.pop('C')
self.estimator = SVC(**svm_params)
# Get kernel name and pass remaining parameters to kernel
mi_kernel_name = parameters.pop('kernel')
self.mi_kernel = kernel.by_name(mi_kernel_name, **parameters)
def fit(self, X, y):
X = map(np.asarray, X)
self.fit_data = X
self.gram_matrix = self.mi_kernel(X, X)
self.estimator.fit(self.gram_matrix, y)
return self
def predict(self, X=None):
if X is None:
gram_matrix = self.gram_matrix
else:
X = map(np.asarray, X)
gram_matrix = self.mi_kernel(X, self.fit_data)
return self.estimator.decision_function(gram_matrix)
示例6: svmDecisionPlot
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def svmDecisionPlot(XTrain, yTrain, XTest, yTest, kernel):
plt.figure(figsize=(7, 5))
cmap_light = ListedColormap(["#AAAAFF", "#AAFFAA", "#FFAAAA"])
cmap_bold = ListedColormap(["#0000FF", "#00FF00", "#FF0000"])
plt.scatter(XTest[:, 0], XTest[:, 1], c=yTest, zorder=10, cmap=cmap_bold)
plt.axis('tight')
x_min = XTest[:, 0].min()-2
x_max = XTest[:, 0].max()+2
y_min = XTest[:, 1].min()-2
y_max = XTest[:, 1].max()+2
XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
if (kernel == 'linear'):
clf = SVC(kernel='linear')
clf.fit(XTrain[:,0:2], yTrain)
else:
clf = SVC(kernel='rbf', C=1.0, gamma=0.0)
clf.fit(XTrain[:,0:2], yTrain)
Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])
# Put the result into a color plot
Z = Z.reshape(XX.shape)
plt.pcolormesh(XX, YY, Z > 0, cmap=cmap_light)
plt.contour(XX, YY, Z, colors=['k', 'k', 'k'], linestyles=['--', '-', '--'], levels=[-.5, 0, .5])
plt.xlabel("Radius")
plt.ylabel("Perimeter")
plt.title(kernel + " SVM")
plt.show()
示例7: SVM
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
class SVM(object):
def __init__(self, **parameters):
svm_params = {'kernel' : 'precomputed'}
if 'C' in parameters:
svm_params['C'] = parameters.pop('C')
self.estimator = SVC(**svm_params)
# Get kernel name and pass remaining parameters to kernel
kernel_name = parameters.pop('kernel')
self.kernel = kernel.by_name(kernel_name, **parameters)
def fit(self, X, y):
X = np.asarray(X)
self.fit_data = X
# X is a list of arrays so applying asarray function to everything in that list
# If you passed in a list of lists, if each bag is an array the asarray funciton just returns it
# but it converts a list of lists to a numpy array.
self.gram_matrix = self.kernel(X, X)
self.estimator.fit(self.gram_matrix, y)
return self
def predict(self, X=None):
if X is None:
gram_matrix = self.gram_matrix
else:
X = np.asarray(X)
gram_matrix = self.kernel(X, self.fit_data)
return self.estimator.decision_function(gram_matrix)
示例8: evaluate_bow
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def evaluate_bow(data, n_folds, stemmer=NullStemmer()):
file_name = '.cache/%s_bow_%s.json.bz2' % (subreddit, stemmer)
if os.path.exists(file_name):
print('Found a cached copy of %s' % file_name)
with bz2.BZ2File(file_name, 'r') as fp:
data = json.load(fp)
feature_matrix = np.asarray(data['feature_matrix'])
label_vector = np.asarray(data['label_vector'])
else:
print('Generating feature matrix for %s' % file_name)
feature_matrix, label_vector = bow.generate_feature_matrix(data, stemmer)
with bz2.BZ2File(file_name, 'w') as fp:
json.dump({
'feature_matrix': feature_matrix.tolist(),
'label_vector': label_vector.tolist(),
}, fp)
kf = StratifiedKFold(label_vector, n_folds=n_folds)
scores = []
y_test = []
for index, (train, test) in enumerate(kf):
classifier = SVC(kernel='linear', C=0.8)
classifier.fit(feature_matrix[train], label_vector[train])
scores.append(classifier.decision_function(feature_matrix[test]))
y_test.append(label_vector[test])
# Convert from list to single array
scores = np.concatenate(scores)
y_test = np.concatenate(y_test)
return scores, y_test
示例9: runSVM_T_One_vs_All
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def runSVM_T_One_vs_All(distances, labels, all_trainingIndices, targetTestingIndice):
# Construct base kernels
baseKernels = []
for i in range(len(distances)):
distance = distances[i]
distance = distance ** 2
trainingDistances = sliceArray(distance, all_trainingIndices)
# Define kernel parameters
gramma0 = 1.0 / np.mean(trainingDistances)
kernel_params = [gramma0 *(2 ** index) for index in range(-3, 2, 1)]
# Construct base kernels & pre-learned classifier
baseKernel = constructBaseKernels(["rbf", "lap", "isd","id"], kernel_params, distance)
baseKernels += baseKernel
# Update base kernels, value is duplicated within the same domain
for baseKernel in baseKernels:
baseKernel[np.ix_([i for i in range(195)], [i for i in range(195)])] = 2 * baseKernel[np.ix_([i for i in range(195)], [i for i in range(195)])]
baseKernel[np.ix_([i for i in range(195, 1101, 1)],[i for i in range(195, 1101, 1)])] = 2 * baseKernel[np.ix_([i for i in range(195, 1101, 1)], [i for i in range(195, 1101, 1)])]
scores = []
for classNum in range(labels.shape[1]):
thisClassLabels = labels[::, classNum]
TrainingLabels = [thisClassLabels[index] for index in all_trainingIndices]
testingLabels = [thisClassLabels[index] for index in targetTestingIndice]
# pre-learned classifiers' score
finalTestScores = np.zeros((len(targetTestingIndice))).reshape((1, len(targetTestingIndice)))
for m in range(len(baseKernels)):
baseKernel = baseKernels[m]
Ktrain = sliceArray(baseKernel, all_trainingIndices)
Ktest = baseKernel[np.ix_(targetTestingIndice ,all_trainingIndices)]
clf = SVC(kernel="precomputed")
clf.fit(Ktrain, TrainingLabels)
dv = clf.decision_function(Ktest)
finalTestScores = np.vstack((finalTestScores, dv.reshape((1, len(targetTestingIndice)))))
# Fuse final scores together
finalTestScores = finalTestScores[1:]
tempFinalTestScores = 1.0 / (1 + math.e **(-finalTestScores))
finalTestScores = np.mean(tempFinalTestScores, axis = 0)
scores.append(finalTestScores)
# Find the label with the largest score
scores = np.vstack(scores)
ranks = np.argmax(scores, axis=0)
labelSet = ["birthday", "parade", "picnic", "show", "sports", "wedding"]
predictLabels = [labelSet[i] for i in ranks]
return predictLabels
示例10: graphs_parameters_svm
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def graphs_parameters_svm(filename):
#reading the features and target variables from the file
features, target = read_data(filename)
# to normalize the data by subtracting mean and dividing by standard deviation
scaler = StandardScaler()
features = scaler.fit_transform(features)
#setting the ranges of gamma and C
C_2d_range = [1, 1e2, 1e4, 1e5]
gamma_2d_range = [1e-1, 1, 1e1, 1e2]
#classifiers will contain list of all the models for various ranges of C and gamma
classifiers = []
for C in C_2d_range:
for gamma in gamma_2d_range:
clf = SVC(kernel='rbf', C=C, gamma=gamma)
clf.fit(features, target)
classifiers.append((C, gamma, clf))
target = [int(y) for y in target]
pl.figure(figsize=(12, 10))
# construct a mesh
h = .02
x = np.array(features, dtype=float)
y = np.array(target, dtype= int)
x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
#Plotting Support vectors
for (k, (C, gamma, clf)) in enumerate(classifiers):
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
pl.subplot(len(C_2d_range), len(gamma_2d_range), k + 1)
pl.title("gamma 10^%d, C 10^%d" % (np.log10(gamma), np.log10(C)),size='medium')
pl.contourf(xx, yy, Z, cmap=pl.cm.Paired)
pl.scatter(clf.support_vectors_[:, 0],clf.support_vectors_[:, 1], c=y[clf.support_], cmap=pl.cm.Paired)
pl.xticks(())
pl.yticks(())
pl.axis('tight')
pl.show()
pl.figure(figsize=(12, 10))
#plotting decision boundary
for (k, (C, gamma, clf)) in enumerate(classifiers):
# evaluate decision function in a grid
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# visualize decision function for these parameters
pl.subplot(len(C_2d_range), len(gamma_2d_range), k + 1)
pl.title("gamma 10^%d, C 10^%d" % (np.log10(gamma), np.log10(C)),
size='medium')
# visualize parameter's effect on decision function
pl.pcolormesh(xx, yy, -Z, cmap=pl.cm.jet)
pl.scatter(features[:, 0], features[:, 1], c=target, cmap=pl.cm.jet)
pl.xticks(())
pl.yticks(())
pl.axis('tight')
pl.show()
optimal_model = grid_search(clf, x, y)
print " the optimal parameters are (C gamma):(", optimal_model.C,optimal_model._gamma, ")"
示例11: runSVM_AT
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def runSVM_AT(distances, labels, auxiliaryIndices, targetTrainingIndice, targetTestingIndice):
all_trainingIndices = auxiliaryIndices + targetTrainingIndice
# Construct base kernels
baseKernels = []
for i in range(len(distances)):
distance = distances[i]
distance = distance ** 2
trainingDistances = sliceArray(distance, all_trainingIndices)
# Define kernel parameters
gramma0 = 1.0 / np.mean(trainingDistances)
kernel_params = [gramma0 *(2 ** index) for index in range(-3, 2, 1)]
# Construct base kernels & pre-learned classifier
baseKernel = constructBaseKernels(["rbf", "lap", "isd","id"], kernel_params, distance)
baseKernels += baseKernel
# Update base kernels, value is duplicated within the same domain
for baseKernel in baseKernels:
baseKernel[np.ix_([i for i in range(195)], [i for i in range(195)])] = 2 * baseKernel[np.ix_([i for i in range(195)], [i for i in range(195)])]
baseKernel[np.ix_([i for i in range(195, 1101, 1)],[i for i in range(195, 1101, 1)])] = 2 * baseKernel[np.ix_([i for i in range(195, 1101, 1)], [i for i in range(195, 1101, 1)])]
all_trainingIndices = auxiliaryIndices + targetTrainingIndice
aps = []
for classNum in range(labels.shape[1]):
thisClassLabels = labels[::, classNum]
TrainingLabels = [thisClassLabels[index] for index in all_trainingIndices]
testingLabels = [thisClassLabels[index] for index in targetTestingIndice]
# pre-learned classifiers' score
finalTestScores = np.zeros((len(targetTestingIndice))).reshape((1, len(targetTestingIndice)))
for m in range(len(baseKernels)):
baseKernel = baseKernels[m]
Ktrain = sliceArray(baseKernel, all_trainingIndices)
Ktest = baseKernel[np.ix_(targetTestingIndice ,all_trainingIndices)]
clf = SVC(kernel="precomputed")
clf.fit(Ktrain, TrainingLabels)
dv = clf.decision_function(Ktest)
finalTestScores = np.vstack((finalTestScores, dv.reshape((1, len(targetTestingIndice)))))
# Fuse final scores together
finalTestScores = finalTestScores[1:]
tempFinalTestScores = 1.0 / (1 + math.e **(-finalTestScores))
finalTestScores = np.mean(tempFinalTestScores, axis = 0)
AP = util.averagePrecision(finalTestScores, testingLabels)
aps.append(AP)
return aps
示例12: train_model
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def train_model(self):
# fts = self.generate_features()
# np.savetxt('features.txt', fts)
fts = np.loadtxt('features.txt')
labels = self.generate_labels(fts)
baseline = np.sum(labels)/float(fts.shape[0])
print "Baseline: ", baseline
rf = RandomForestClassifier(n_estimators=500, max_features='sqrt', bootstrap=True, min_samples_leaf=4) # chosen with x-validation
svm = SVC(C=100, gamma=0.25) # chosen with x-validation
rf_scores = []
rf_true_positives = []
rf_false_positives = []
rf_false_negatives = []
svm_scores = []
svm_true_positives = []
svm_false_positives = []
svm_false_negatives = []
for i in range(5):
train_ft, test_ft, train_label, test_label = cross_validation.train_test_split(fts, labels, test_size=0.2, random_state=i)
# remove first ft since it's just uid, used to generate labels
train_ft = train_ft[:,1:8]
test_ft = test_ft[:,1:8]
rf = rf.fit(train_ft, train_label)
rf_scores.append(rf.score(test_ft, test_label))
rf_predictions = rf.predict(test_ft)
svm_train_ft = train_ft[:,0:2]
svm_test_ft = test_ft[:,0:2]
svm.fit(svm_train_ft, train_label)
svm_scores.append(svm.score(svm_test_ft, test_label))
svm_predictions = svm.predict(svm_test_ft)
roc_svm_score = svm.decision_function(svm_test_ft)
print roc_svm_score
fpr, tpr, thresholds = roc_curve(test_label, roc_svm_score)
roc_auc = auc(fpr, tpr)
print "FPR", fpr, "TPR:", tpr, "ROC_AUC:", roc_auc
print "Feature importances", rf.feature_importances_
avg_rf_score = sum(rf_scores) / float(len(rf_scores))
avg_svm_score = sum(svm_scores) / float(len(svm_scores))
print "avg RF accuracy:", avg_rf_score, "avg SVM accuracy:", avg_svm_score
示例13: svm_learner
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
def svm_learner(budget):
accuracy = []
data = csv_reader('resources/pool.csv')
testset = csv_reader('resources/testSet.csv')
true_labels = oracle.read_mat()
used = {}
# do nothing about model until reasonable training subset achieved
[row, col] = data.shape
preds = np.zeros(row)
selected = []
labels = []
query = 0
# query each point until get one with label 1
while 1:
r = compound.next_compound(data)
r_str = np.array_str(np.char.mod('%d', r))
if r_str[1: (len(r_str) - 1)] not in used:
r_label = oracle.oracle2(r, data)
query += 1
used[r_str[1: (len(r_str) - 1)]] = r_label
selected.append(r.tolist())
labels.append(r_label)
accuracy.append(error.generalization_error(preds, true_labels))
if np.sum(labels) == 1 and len(labels) > 1:
accuracy.pop()
break
x = np.array(selected)
y = np.array(labels)
clf = SVC(kernel='linear')
clf.fit(x, y)
preds = clf.predict(data)
accuracy.append(error.generalization_error(preds, true_labels))
num = 2543 - len(used)
i = 0
while i < num and query < budget:
r = compound.next_compound(data)
r_str = np.array_str(np.char.mod('%d', r))
if r_str[1: (len(r_str) - 1)] not in used:
i += 1
distance = clf.decision_function(r)
if np.abs(distance[0]) <= 0.78:
x = np.vstack([x, r])
r_label = oracle.oracle2(r, data)
y = np.hstack([y.tolist(), r_label])
query += 1
clf.fit(x, y)
preds = clf.predict(testset)
accuracy.append(error.test_error(preds, true_labels))
plt.plot(accuracy)
plt.show()
print f1_score(preds, true_labels[0:250])
return
示例14: SVM
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
class SVM(DMCClassifier):
def __init__(self, X: csr_matrix, Y: np.array, tune_parameters=False):
super().__init__(X, Y, tune_parameters)
if tune_parameters:
self.param_dist_random = {'shrinking': [True, False],
'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
'degree': sp_randint(2, 5)}
self.clf = SVC(kernel='rbf', shrinking=True)
def predict_proba(self, X: csr_matrix) -> np.array:
return self.clf.decision_function(X)
示例15: __init__
# 需要导入模块: from sklearn.svm import SVC [as 别名]
# 或者: from sklearn.svm.SVC import decision_function [as 别名]
class ClassifierSVM:
def __init__(self, useOne = False, trainingSamples = 10000):
self.useOne = useOne
self.classifier = SVC(kernel='linear')
self.trainingSamples = trainingSamples
def fit(self, X, y):
print(str(X.shape))
self.classifier.fit(X[:self.trainingSamples], y[:self.trainingSamples])
return self
def transform(self, X, y=None):
return self.classifier.decision_function(X)