本文整理汇总了Python中sklearn.svm.LinearSVC.fit方法的典型用法代码示例。如果您正苦于以下问题:Python LinearSVC.fit方法的具体用法?Python LinearSVC.fit怎么用?Python LinearSVC.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.svm.LinearSVC
的用法示例。
在下文中一共展示了LinearSVC.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def train(dataset):
print "Reading dataset ..."
features = np.array(dataset.data, "int16")
labels = np.array(dataset.target, "int")
nExamples = features.shape[0]
# Compute HOGs for each image in the database
print "Extracting features for " + str(nExamples) + " training examples ... ",
sys.stdout.flush()
startTime = time.clock()
list_hog_fd = []
for feature in features:
fd = hog(
feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False
)
list_hog_fd.append(fd)
hog_features = np.array(list_hog_fd, "float64")
elapsedTime = time.clock() - startTime
print "{0:.3f}s ({1:.4f}s/example)".format(elapsedTime, elapsedTime / nExamples)
print "Training ... ",
sys.stdout.flush()
startTime = time.clock()
clf = LinearSVC()
clf.fit(hog_features, labels)
elapsedTime = time.clock() - startTime
print "{0:.3f}s".format(elapsedTime)
print "Saving model to " + MODEL_FILE
joblib.dump(clf, MODEL_FILE, compress=3)
print "Training finished ..."
示例2: linear_svc
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def linear_svc(train_bow,train_labels,test_bow,test_labels,bow_indexes):
print("Training linear svc")
svc_classifier=LinearSVC()
svc_classifier.fit(train_bow,train_labels)
print("Testing linear svc")
test(svc_classifier,"svc",test_bow,test_labels,bow_indexes)
示例3: trainClassifier
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def trainClassifier(classifier, X, y):
vectorizer = TfidfVectorizer(analyzer='char', use_idf=True, sublinear_tf=True, stop_words='english', ngram_range=(1,3), lowercase=True)
# vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(X).toarray()
if classifier == "SVC":
clf = LinearSVC()
# parameters = {'kernel':['linear', 'rbf'], 'C':[0.1, 1, 10]}
# clf = grid_search.GridSearchCV(clf, parameters)
clf.fit(X, y)
# print clf.best_params_
clf_vect = [clf, vectorizer]
f = open('svm.pkl', 'wb')
pickle.dump(clf_vect, f)
return clf, vectorizer
elif classifier == "RF":
clf = RandomForestClassifier()
clf.fit(X, y)
return clf, vectorizer
elif classifier == "MNB":
clf = MultinomialNB()
clf.fit(X, y)
return clf, vectorizer
elif classifier == "LDA":
clf = LDA()
clf.fit(X, y)
return clf, vectorizer
elif classifier == "KNN":
clf = KNeighborsClassifier()
clf.fit(X, y)
return clf, vectorizer
示例4: train
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def train(train_input, train_output, test_input, test_output):
# 训练模块
# 选择模型
# model = MultinomialNB()
# model = GaussianNB()
# model = SGDClassifier()
# model = SVC(kernel='linear') # 这个很慢
model = LinearSVC()
# model = RandomForestClassifier(max_depth=2, n_estimators=500)
# model = AdaBoostClassifier(n_estimators=500,base_estimator=DecisionTreeClassifier(max_depth=10))
# 训练 & 评测
model.fit(train_input,train_output)
pred_train = model.predict(train_input)
pred_test = model.predict(test_input)
label_size = max(train_output)+1
train_ratio = cal_accuracy(pred_train, train_output)
train_recal = cal_recall(pred_train, train_output, label_size)
# print(test_output)
print(list(pred_test))
test_ratio = cal_accuracy(pred_test, test_output)
test_recal = cal_recall(pred_test, test_output, label_size)
print('%f\t%f'%(train_ratio, test_ratio))
print('%f\t%f'%(train_recal, test_recal))
示例5: test_grid_search_correct_score_results
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def test_grid_search_correct_score_results():
# test that correct scores are used
n_splits = 3
clf = LinearSVC(random_state=0)
X, y = make_blobs(random_state=0, centers=2)
Cs = [.1, 1, 10]
for score in ['f1', 'roc_auc']:
grid_search = GridSearchCV(clf, {'C': Cs}, scoring=score, cv=n_splits)
results = grid_search.fit(X, y).cv_results_
# Test scorer names
result_keys = list(results.keys())
expected_keys = (("mean_test_score", "rank_test_score") +
tuple("split%d_test_score" % cv_i
for cv_i in range(n_splits)))
assert_true(all(in1d(expected_keys, result_keys)))
cv = StratifiedKFold(n_splits=n_splits)
n_splits = grid_search.n_splits_
for candidate_i, C in enumerate(Cs):
clf.set_params(C=C)
cv_scores = np.array(
list(grid_search.cv_results_['split%d_test_score'
% s][candidate_i]
for s in range(n_splits)))
for i, (train, test) in enumerate(cv.split(X, y)):
clf.fit(X[train], y[train])
if score == "f1":
correct_score = f1_score(y[test], clf.predict(X[test]))
elif score == "roc_auc":
dec = clf.decision_function(X[test])
correct_score = roc_auc_score(y[test], dec)
assert_almost_equal(correct_score, cv_scores[i])
示例6: __init__
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
class Classifier:
def __init__(self, ctype):
self.ctype = ctype
def train(self, data, labels):
if self.ctype == "SVM":
self.model = LinearSVC()
self.model.fit(data, labels)
elif self.ctype == "Decision":
print "Unsupported"
elif self.ctype == "Chi-Squared":
self.model_data = data
self.model_labels = labels
def predict(self, data):
if self.ctype == "SVM":
return self.model.predict(data)
elif self.ctype == "Decision":
print "Unsupported"
elif self.ctype == "Chi-Squared":
predictions = []
for sample, test_hist in enumerate(data):
#Storing the first distance by default
lowest_score = cv2.compareHist(np.array(self.model_data[0], dtype = np.float32),
np.array(test_hist, dtype = np.float32), method = 1)
predictions.append(self.model_labels[0])
#Going through the rest of data
for index, train_hist in enumerate(self.model_data):
score = cv2.compareHist(np.array(train_hist, dtype = np.float32),
np.array(test_hist, dtype = np.float32), method = 1)
if score < lowest_score:
lowest_score = score
predictions[sample] = self.model_labels[index]
return predictions
示例7: linearSVM
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def linearSVM(self):
'''
線形SVMを用いた2クラス分類
args : ->
dst : ->
param: ->
'''
# 学習データ
data_training_tmp = np.loadtxt('../../../data/statistical_data/CodeIQ_auth.txt', delimiter=' ')
data_training = [[x[0], x[1]] for x in data_training_tmp]
label_training = [int(x[2]) for x in data_training_tmp]
# 試験データ
data_test = np.loadtxt('../../../data/statistical_data/CodeIQ_auth.txt', delimiter=' ')
print np.array(data_test).shape
# 学習
estimator = LinearSVC(C=1.0)
estimator.fit(data_training, label_training)
# 予測
label_prediction = estimator.predict(data_test[:,0:2])
print(label_prediction)
print
示例8: LinearSVCStep
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
class LinearSVCStep(SklearnStep):
def __init__(self, c=1.0):
super(LinearSVCStep, self).__init__()
self._c = c
def fit_transform(self):
self._model = LinearSVC(C=self._c)
x, y = load_svmlight(self.input_path)
self._model.fit(x, y)
scores = self._model.decision_function(x)
save_numpy_txt(scores, self.output_path)
def transform(self, x=None):
if x is None:
_x, y = load_svmlight(self._test_input_path)
conf_x = self._model.decision_function(_x)
predicted_x = self._model.predict(_x)
res = np.vstack((y, conf_x, predicted_x)).T
save_numpy_txt(res, self._test_output_path)
else:
transformed_x = self._model.decision_function(x)
return transformed_x
def predict(self, x):
return self._model.predict(x)
def decision_function(self, x):
return self._model.decision_function(x)
def get_param(self):
return {'c': self._c}
示例9: __init__
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
class LinearSVM:
def __init__(self):
self.clf = LinearSVC(penalty='l2', loss='l1', dual=True, tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=None)
self.pattern ='(?u)\\b[A-Za-z]{3,}'
self.tfidf = TfidfVectorizer(sublinear_tf=False, use_idf=True, smooth_idf=True, stop_words='english', token_pattern=self.pattern, ngram_range=(1, 3))
def train(self,fileName):
print "LinearSVM Classifier is being trained"
table = pandas.read_table(fileName, sep="\t", names=["cat", "message"])
X_train = self.tfidf.fit_transform(table.message)
Y_train = []
for item in table.cat:
Y_train.append(int(item))
self.clf.fit(X_train, Y_train)
print "LinearSVM Classifier has been trained"
def classify(self,cFileName, rFileName):
table = pandas.read_table(cFileName, names=["message"])
X_test = self.tfidf.transform(table.message)
print "Data have been classified"
with open(rFileName,'w') as f:
for item in self.clf.predict(X_test).astype(str):
f.write(item+'\n')
def validate(self,fileName):
table = pandas.read_table(fileName, sep="\t", names=["cat", "message"])
X_validate = self.tfidf.transform(table.message)
Y_validated = self.clf.predict(X_validate).astype(str)
totalNum = len(table.cat)
errorCount = 0
for i in range(0,totalNum):
if int(table.cat[i])!=int(Y_validated[i]):
errorCount += 1
print "Data have been validated! Precision={}".format((totalNum-errorCount)/float(totalNum))
示例10: main
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def main():
dataset = load_cifar.load_cifar(n_train=N_TRAIN, n_test=N_TEST,
grayscale=GRAYSCALE, shuffle=False)
train_data = dataset['train_data']
train_labels = dataset['train_labels']
test_data = dataset['test_data']
test_labels = dataset['test_labels']
print train_data.shape, test_data.shape
patch_extractor = image.PatchExtractor(patch_size=(PATCH_SIZE, PATCH_SIZE),
max_patches = N_PATCHES/
len(train_data))
pp = preprocessing.Preprocessor(n_components=0.99)
fl = feature_learner.FeatureLearner(pp, patch_extractor, n_clusters=N_CENTROIDS)
fl.fit(train_data)
train = fl.transform(train_data)
m_train = mean(train, axis=0)
train -= m_train
v_train = sqrt(var(train, axis=0) + 0.01)
train /= v_train
test = fl.transform(test_data)
test -= m_train
test /= v_train
classifier = SVC(C=10.0)#, gamma=1e-3, verbose=False)
classifier.fit(train, train_labels)
print classifier.score(test, test_labels)
return
示例11: retrain_models
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def retrain_models(username):
train_x, train_y, body_x, body_y, head_x, head_y = model_retriever.retrieve_data_db(username)
b_train_x = []
b_train_y = numpy.concatenate([body_y, train_y])
for msg in (body_x + train_x):
b_train_x.append(extract_body_features(msg))
body_vec = TfidfVectorizer(norm="l2")
b_train_x = body_vec.fit_transform(b_train_x)
h_train_x = []
h_train_y = numpy.concatenate([head_y, train_y])
for msg in (head_x + train_x):
h_train_x.append(extract_header_features(msg))
head_vec = DictVectorizer()
h_train_x = head_vec.fit_transform(h_train_x)
body_model = LinearSVC(loss='l2', penalty="l2", dual=False, tol=1e-3)
head_model = RidgeClassifier(tol=1e-2, solver="lsqr")
body_model.fit(b_train_x, b_train_y)
head_model.fit(h_train_x, h_train_y)
print("Finished training models for "+username+"...")
store_models(username, body_vec, body_model, head_vec, head_model)
示例12: benchmark
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def benchmark(k, epochs):
print("*" * 80)
print("k: %d, epochs: %d\n" % (k, epochs))
#select = SelectKBest(score_func=chi2, k=k)
select = TruncatedSVD(n_components=k)
X_train_trunc = select.fit_transform(X_train, Y_train)
X_test_trunc = select.transform(X_test)
print('done truncating')
parameters = {'C': [1, 10, 100, 1000, 10000], 'class_weight': ['auto', None], 'tol':[0.001,0.0001]}
clf = LinearSVC(C=100000)
#clf = grid_search.GridSearchCV(svc, parameters)
clf.fit(X_train_trunc, Y_train)
pred = clf.predict(X_test_trunc)
if CREATE_SUBMISSION:
X_submit_trunc = select.transform(X_submit)
pred_submit = clf.predict(X_submit_trunc)
dump_csv(pred_submit, k, epochs)
score = metrics.f1_score(Y_test, pred)
print("f1-score: %0.3f" % score)
print("classification report:")
print(metrics.classification_report(Y_test, pred))
print("confusion matrix:")
print(metrics.confusion_matrix(Y_test, pred))
示例13: svm_binary_svc_probability
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def svm_binary_svc_probability(X, Y, C):
allp = np.sum(Y>0);
alln = len(Y) - allp;
nr_fold = 5;
perm = list(range(len(Y)));
random.shuffle(perm);
dec_values = np.zeros(len(Y), dtype=np.float32);
for i in range(nr_fold):
start = i * len(Y) // nr_fold;
end = (i+1) * len(Y) // nr_fold;
trainL = [perm[j] for j in range(len(Y)) if j not in range(start, end)];
testL = perm[start:end];
trainX = X[trainL,:];
trainY = Y[trainL];
p_count = np.sum(trainY>0);
n_count = len(trainY) - p_count;
if p_count==0 and n_count==0:
dec_values[start:end] = 0.0;
elif p_count > 0 and n_count == 0:
dec_values[start:end] = 1.0;
elif p_count == 0 and n_count > 0:
dec_values[start:end] = -1.0;
else :
subclf = LinearSVC(C=C, class_weight={1:allp,-1:alln});
subclf.fit(trainX, trainY);
dec_values[testL] = subclf.decision_function(X[testL,:]).ravel();
return sigmoid_train(dec_values, Y);
示例14: with_aureliens_potentials_svm
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def with_aureliens_potentials_svm(test=False):
data = load_data('train', independent=True)
data = add_kraehenbuehl_features(data)
features = [x[0] for x in data.X]
y = np.hstack(data.Y)
if test:
data_ = load_data('val', independent=True)
data_ = add_kraehenbuehl_features(data_)
features.extend([x[0] for x in data.X])
y = np.hstack([y, np.hstack(data_.Y)])
new_features_flat = np.vstack(features)
from sklearn.svm import LinearSVC
print("training svm")
svm = LinearSVC(C=.001, dual=False, class_weight='auto')
svm.fit(new_features_flat[y != 21], y[y != 21])
print(svm.score(new_features_flat[y != 21], y[y != 21]))
print("evaluating")
eval_on_pixels(data, [svm.predict(x) for x in features])
if test:
print("test data")
data_val = load_data('test', independent=True)
else:
data_val = load_data('val', independent=True)
data_val = add_kraehenbuehl_features(data_val)
features_val = [x[0] for x in data_val.X]
eval_on_pixels(data_val, [svm.predict(x) for x in features_val])
示例15: applySVMWithPCA
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import fit [as 别名]
def applySVMWithPCA():
'''
Same as the previous function, just change the file names..
'''
data = io.mmread(ROOTDIR+"TRAINDATA.mtx")
label = np.load(ROOTDIR+"label_train.npy")
testdata = io.mmread(ROOTDIR+"TESTDATA.mtx")
testLabel = np.load(ROOTDIR + "label_test.npy")
linear_svm = LinearSVC(C=1.0, class_weight=None, loss='hinge', dual=True, fit_intercept=True,
intercept_scaling=1, multi_class='ovr', penalty='l2',
random_state=None, tol=0.0001, verbose=1, max_iter=2000)
data = scale(data, with_mean=False)
linear_svm.fit(data, label)
joblib.dump(linear_svm, ROOTDIR+'originalTrain_hinge_2000.pkl')
# linear_svm = joblib.load(ROOTDIR+'originalTrain_hinge_2000.pkl')
print 'Trainning Done!'
scr = linear_svm.score(data, label)
print 'accuracy on the training set is:' + str(scr)
predLabel = linear_svm.predict(data)
calcualteRMSE(label, predLabel)
scr = linear_svm.score(testdata, testLabel)
print 'accuracy on the testing set is:' + str(scr)
predLabel = linear_svm.predict(testdata)
calcualteRMSE(testLabel, predLabel)