本文整理汇总了Python中sklearn.svm.LinearSVC.transform方法的典型用法代码示例。如果您正苦于以下问题:Python LinearSVC.transform方法的具体用法?Python LinearSVC.transform怎么用?Python LinearSVC.transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.svm.LinearSVC
的用法示例。
在下文中一共展示了LinearSVC.transform方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createSub
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def createSub(clf, traindata, labels, testdata):
sub = 1
labels = np.asarray(map(int,labels))
niter = 10
auc_list = []
mean_auc = 0.0; itr = 0
if sub == 1:
xtrain = traindata#[train]
xtest = testdata#[test]
ytrain = labels#[train]
predsorig = np.asarray([0] * testdata.shape[0]) #np.copy(ytest)
labelsP = []
for i in range(len(labels)):
if labels[i] > 0:
labelsP.append(1)
else:
labelsP.append(0)
labelsP = np.asarray(labelsP)
ytrainP = labelsP
lsvc = LinearSVC(C=0.01, penalty="l1", dual=False, verbose = 2)
print xtrain.shape, ytrainP.shape
lsvc.fit(xtrain, ytrainP)
xtrainP = lsvc.transform(xtrain)
xtestP = lsvc.transform(xtest)
print xtrain.shape, xtest.shape
print xtrainP.shape, xtest.shape
clf.fit(xtrainP,ytrainP)
predsP = clf.predict(xtestP)
preds_ = clf.predict(xtrainP)
print sum(preds_), sum(ytrainP), sum(abs(preds_-ytrainP))
nztrain = np.where(ytrainP > 0)[0]
nztest = np.where(predsP == 1)[0]
nztrain0 = np.where(ytrainP == 0)[0]
nztest0 = np.where(predsP == 0)[0]
xtrainP = xtrain[nztrain]
xtestP = xtest[nztest]
ytrain0 = ytrain[nztrain0]
ytrain1 = ytrain[nztrain]
clf.fit(xtrainP,ytrain1)
preds = clf.predict(xtestP)
predsorig[nztest] = preds
predsorig[nztest0] = 0
np.savetxt('predictions.csv',predsorig ,delimiter = ',', fmt = '%d')
示例2: l1FeatureSelection
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def l1FeatureSelection():
X = np.array(trainingData, dtype=float)
X1 = np.array(testData, dtype=float)
y = np.array(trainingDataLabels, dtype=float)
model = LinearSVC(C=0.01, penalty="l1", dual=False)
newX = model.fit_transform(X, y)
newX1 = model.transform(X1)
return (newX, newX1)
示例3: main
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def main():
file = gzip.open("../saves/saved_texts.gz", 'r')
texts, texts_data = pickle.load(file)
file.close()
price_data, rating_data, category_data = get_labels(texts_data)
y = tfidf_process_text(texts)
svm_price = LinearSVC(C=4, dual=False)
svm_price.fit(y, price_data)
y_trans_price = svm_price.transform(y, threshold = "2*mean").toarray()
svm_rating = LinearSVC(C=4, dual=False)
svm_rating.fit(y, rating_data)
y_trans_rating = svm_rating.transform(y, threshold = "2*mean").toarray()
svm_category = LinearSVC(C=4, dual=False)
svm_category.fit(y, category_data)
y_trans_category = svm_category.transform(y, threshold = "2*mean").toarray()
y_trans = np.hstack([y_trans_category, y_trans_price, y_trans_rating])
objectlm_covariance(y_trans + 1e-12, "../saves/svm", metric="euclidean")
示例4: L1LinearSVC
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
class L1LinearSVC(LinearSVC):
def fit(self, X, y):
self.transformer_ = LinearSVC(penalty="l1", dual=False, tol=1e-3)
X = self.transformer_.fit_transform(X, y)
return LinearSVC.fit(self, X, y)
def predict(self, X):
X = self.transformer_.transform(X)
return LinearSVC.predict(self, X)
示例5: L1LinearSVC
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
class L1LinearSVC(LinearSVC):
def fit(self,X,y):
#The smaller C , the stronger the regularization.
#The more regularization, the more sparsity.
self.transformer_ = LinearSVC(penalty="l1",dual=False,tol=1e-3)
X = self.transformer_.fit_transform(X,y)
return LinearSVC.fit(self,X,y)
def predict(self,X):
X = self.transformer_.transform(X)
return LinearSVC.predict(self,X)
示例6: L1LinearSVC
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
class L1LinearSVC(LinearSVC): # Creating new class L1LinearSVC with two methods, fit and predict
def fit(self, X, y): # This method acts on itself with X and y
self.transformer_ = LinearSVC(penalty="l1",
dual=False, tol=1e-3) # This is changing all the defaults for LinearSVC
X = self.transformer_.fit_transform(X, y) # Assigning X with the new parameters for LinearSVC performing fit_transform operation
return LinearSVC.fit(self, X, y) # Returns the fit with the new X with the default LinearSVC parameters
def predict(self, X): # Predicts the outcome based on the test dataset X
X = self.transformer_.transform(X) # Perform a transform on X using the updated defaults for LinearSVC
return LinearSVC.predict(self, X) # returns the predicted score on the transformed data X
示例7: featureSelection
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def featureSelection(X_train,X_test,X_val,y_train,log,tech,C):
if (tech == 'VarTh'):
sel = VarianceThreshold(threshold=0.01)
X_train_new = sel.fit_transform(X_train.todense())
X_test_new = sel.transform(X_test.todense())
X_val_new = sel.transform(X_val.todense())
if (log):
X_train_new = np.log(X_train_new+1)
X_test_new = np.log(X_test_new+1)
X_val_new = np.log(X_val_new+1)
if (tech == 'LinearSVC'):
mod = LinearSVC(C=C, penalty="l1", dual=False)
X_train_new = mod.fit_transform(X_train.todense(), y_train)
X_test_new = mod.transform(X_test.todense())
X_val_new = mod.transform(X_val.todense())
if (log):
X_train_new = np.log(X_train_new+1)
X_test_new = np.log(X_test_new+1)
X_val_new = np.log(X_val_new+1)
return X_train_new, X_test_new , X_val_new
示例8: baseline_model
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def baseline_model(X_train,y_train,X_test,y_test):
feature_selection = LinearSVC(C=10, penalty='l1', dual=False)
X_train_new = feature_selection.fit_transform(X_train, y_train)
X_test_new = feature_selection.transform(X_test)
print X_train_new.shape
svm = LinearSVC(C=1)
svm.fit(X_train_new, y_train)
predicted = svm.predict(X_test_new)
return predicted
示例9: __init__
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
class L1LinearSVC:
def __init__(self, k):
self.k = k
self.y = None
def fit(self, X, y):
self.svc = LinearSVC(C=self.k, penalty="l1",
dual=False, tol=1e-3)
self.svc.fit(X, y)
return self
def transform(self, X):
X = self.svc.transform(X)
return X
示例10: svm_cla_sklearn_feat_sel
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def svm_cla_sklearn_feat_sel(features_train, features_test, labels_train, labels_test):
from sklearn.feature_selection import SelectPercentile, SelectKBest, f_classif, RFECV
from sklearn.cross_validation import StratifiedKFold
from sklearn.metrics import zero_one_loss
features_train = sp.array(features_train, dtype = 'uint8')
features_test = sp.array(features_test, dtype = 'uint8')
print "zscore features"
tic = time.time()
features_train, mean_f, std_f = features_preprocessing(features_train)
features_test, mean_f, std_f = features_preprocessing(features_test, mean_f, std_f)
print "time taken to zscore data is:", round(time.time() - tic) , "seconds"
featSize = np.shape(features_train)
selector = LinearSVC(C=0.0007, penalty="l1", dual=False).fit(features_train, labels_train)
print 'Starting with %d samp, %d feats, keeping %d' % (featSize[0], featSize[1], (np.shape(selector.transform(features_train)))[1])
print 'classifying'
features_train = selector.transform(features_train)
features_test = selector.transform(features_test)
#import ipdb; ipdb.set_trace()
mem = Memory(cachedir='tmp')
classif_RBF2 = mem.cache(classif_RBF)
c = l_c[0]
Parallel(n_jobs=8)(delayed(classif_RBF2)(features_train, features_test, labels_train, labels_test, g, c) for g in l_g)
#import ipdb; ipdb.set_trace()
print "Starting CONTROL classification for c = ", c
tic = time.time()
clf = SVC(C=c)
clf.fit(features_train, labels_train) #[:1960][:]
score = clf.score(features_test, labels_test) #[:13841][:]
print "selected CONTROL score for c = ", c, "is: ", score
print "time taken:", time.time() - tic, "seconds"
示例11: baseline_model
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def baseline_model(X_train,y_train,X_test,y_test):
#dimension reduction
feature_selection = LinearSVC(C=1, penalty="l1", dual=False)
X_train_reduced = feature_selection.fit_transform(X_train, y_train)
X_test_reduced = feature_selection.transform(X_test)
#metrics learning
ml = LMNN(k=4,min_iter=50,max_iter=1000, learn_rate=1e-7)
ml.fit(X_train_reduced,y_train)
X_train_new = ml.transform(X_train_reduced)
X_test_new = ml.transform(X_test_reduced)
neigh = KNeighborsClassifier(n_neighbors=4)
neigh.fit(X_train_new, y_train)
predicted = neigh.predict(X_test_new)
#pickle.dump(ml, open('dist_metrics', 'w'))
return predicted
示例12: baseline_model
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def baseline_model(X_train,y_train,X_test,y_test):
print X_train.shape
feature_selection = LinearSVC(C=1, penalty="l1", dual=False)
X_train_new = feature_selection.fit_transform(X_train, y_train)
X_test_new = feature_selection.transform(X_test)
print X_train_new.shape
print X_test_new.shape
F = RandomForestClassifier(n_estimators=300,
criterion='gini',
min_samples_split=8,
min_samples_leaf=3, max_features='auto',
max_leaf_nodes=4)
F.fit(X_train_new,y_train)
predicted = F.predict(X_test_new)
return predicted
示例13: main
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def main():
initData()
disp('Start')
if LOAD:
X = load(DATA_NAME)
else:
dataList = simpleMultiply(get('tweet'), MULTIPLIER) + simpleMultiply(get('tweet_test'), MULTIPLIER) # Full Train + Test
# dataList = simpleMultiply(get('tweet')[:BREAK_POINT], MULTIPLIER) + simpleMultiply(get('tweet')[BREAK_POINT:], MULTIPLIER) # Up to BP Train + From BP Test
X = vectorizeText(dataList, 20000)
if SAVE_DATA:
save(X, DATA_NAME)
disp(X.shape)
disp('Vectorized')
# Data
trainData = X[:BREAK_POINT*5]
testData = X[BREAK_POINT*5:]
# trainData = trainData.todense()
# testData = testData.todense()
i = 'k'
classes = multiply(get('%s_raw' % i), MULTIPLIER)
t0 = time()
svc = LinearSVC(penalty='l1', dual=False)
svc.fit(trainData, classes)
disp('Train time: %d seconds' % (time() - t0))
save(svc, 'SVC_%s_all' % i, 'pickles/svc/4')
transArr = svc.transform(X, '3.25*mean')
save(transArr, 'X_%s_all' % i, 'pickles/svc/4')
disp(transArr.shape)
print('End')
示例14: LinearSVC
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
MODEL_NAME = 'model_16_random_forest_calibrated_feature_selection'
MODE = 'cv' # cv|submission|holdout
# import data
train, labels, test, _, _ = utils.load_data()
# transform counts to TFIDF features
tfidf = feature_extraction.text.TfidfTransformer(smooth_idf=False)
train = np.append(train, tfidf.fit_transform(train).toarray(), axis=1)
test = np.append(test, tfidf.transform(test).toarray(), axis=1)
# feature selection
feat_selector = LinearSVC(C=0.095, penalty='l1', dual=False)
train = feat_selector.fit_transform(train, labels)
test = feat_selector.transform(test)
print train.shape
# encode labels
lbl_enc = preprocessing.LabelEncoder()
labels = lbl_enc.fit_transform(labels)
# train classifier
clf = ensemble.ExtraTreesClassifier(n_jobs=3, n_estimators=600, max_features=20, min_samples_split=3,
bootstrap=False, verbose=3, random_state=23)
if MODE == 'cv':
scores, predictions = utils.make_blender_cv(clf, train, labels, calibrate=True)
开发者ID:ShrikanthRamanathan,项目名称:kaggle_otto,代码行数:32,代码来源:random_forest_calibrated_feature_selection.py
示例15: trainer
# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import transform [as 别名]
def trainer(traindata, labels, testdata, regression_type, lsvcC=0.01, logregC=1.0):
labels = np.asarray(map(int,labels))
xtrain = traindata#[train]
xtest = testdata#[test]
ytrain = labels#[train]
predsorig_train = np.asarray([0] *traindata.shape[0]) #np.copy(ytest)
predsorig_test = np.asarray([0] * testdata.shape[0]) #np.copy(ytest)
labelsP = np.asarray(map(lambda x: 1 if x > 0 else 0,labels))
ytrainP = labelsP
#http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html
lsvc = LinearSVC(C=lsvcC, penalty="l1", dual=False, verbose = 2)
lsvc.fit(xtrain, ytrainP)
xtrainP = lsvc.transform(xtrain)
xtestP = lsvc.transform(xtest)
clf = lm.LogisticRegression(penalty='l2', dual=True, tol=0.0001,
C=logregC, fit_intercept=True, intercept_scaling=1.0,
class_weight=None, random_state=None)
#http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html
clf2 = ens.GradientBoostingRegressor(loss='quantile', alpha=0.5,
n_estimators=250, max_depth=3,
learning_rate=.1, min_samples_leaf=9,
min_samples_split=9)
#http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingRegressor.html
#=Regression 1================================
clf.fit(xtrainP,ytrainP)
predsP = clf.predict(xtestP)
#=============================================
nztrain = np.where(ytrainP > 0)[0]
nztest = np.where(predsP == 1)[0]
nztrain0 = np.where(ytrainP == 0)[0]
nztest0 = np.where(predsP == 0)[0]
xtrainP = xtrain[nztrain]
xtestP = xtest[nztest]
ytrain0 = ytrain[nztrain0]
ytrain1 = ytrain[nztrain]
#=Regression 2================================
if regression_type=="logistic":
print "logistic regression"
clf.fit(xtrainP,ytrain1)
preds_train= clf.predict(xtrainP)
preds_test = clf.predict(xtestP)
predsorig_train[nztrain]= preds_train
predsorig_test[nztest] = preds_test
#=============================================
elif regression_type=="quantile":
print "quantile regression"
clf2.fit(xtrainP,ytrain1)
preds_train= clf2.predict(xtrainP)
preds_test= clf2.predict(xtestP)
predsorig_train[nztrain] = np.asarray(map(int,preds_train))
predsorig_test[nztest] = np.asarray(map(int,preds_test))
#=============================================
else:
print "error: wrong regression type"
return
#print np.sum(predsorig)
#predsorig[nztest0] = 0
#print np.sum(predsorig)
return predsorig_train, predsorig_test