本文整理汇总了Python中sklearn.preprocessing方法的典型用法代码示例。如果您正苦于以下问题:Python sklearn.preprocessing方法的具体用法?Python sklearn.preprocessing怎么用?Python sklearn.preprocessing使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn
的用法示例。
在下文中一共展示了sklearn.preprocessing方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encoded_1d
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def encoded_1d(samples):
""" Returns a unique label for each combination of samples """
# from sklearn.preprocessing import MultiLabelBinarizer
encoded_2d = samples.encoded_2d()
class_space = [v.n_classes for k, v in samples.items()]
offsets = np.array([1] + np.cumprod(class_space).tolist()[:-1])[None, :]
encoded_1d = (offsets * encoded_2d).sum(axis=1)
# e = MultiLabelBinarizer()
# bin_coeff = e.fit_transform(encoded_2d)
# bin_basis = (2 ** np.arange(bin_coeff.shape[1]))[None, :]
# # encoded_1d = (bin_coeff * bin_basis).sum(axis=1)
# encoded_1d = (bin_coeff * bin_basis[::-1]).sum(axis=1)
# # vt.unique_rows(sklearn.preprocessing.MultiLabelBinarizer().fit_transform(encoded_2d))
# [v.encoded_df.values for k, v in samples.items()]
# encoded_df_1d = pd.concat([v.encoded_df for k, v in samples.items()], axis=1)
return encoded_1d
示例2: __init__
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def __init__(self,
component_config: Dict[Text, Any] = None,
clf: 'sklearn.model_selection.GridSearchCV' = None,
le: Optional['sklearn.preprocessing.LabelEncoder'] = None
) -> None:
"""Construct a new intent classifier using the sklearn framework."""
from sklearn.preprocessing import LabelEncoder
super(SklearnIntentClassifier, self).__init__(component_config)
if le is not None:
self.le = le
else:
self.le = LabelEncoder()
self.clf = clf
_sklearn_numpy_warning_fix()
示例3: get_scaler
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def get_scaler(df, missing="zeros", scaler="standard", **kwargs):
"""
Fit a sklearn scaler on a Data Frame and return the scaler.
Valid options for the scaler are: standard, minmax, maxabs, robust, quantile
Missing values must be dealt with before the scaling is applied.
Valid options specified through the missing parameter are: zeros, mean, median, mode
"""
scalers = {'standard':'StandardScaler', 'minmax':'MinMaxScaler', 'maxabs':'MaxAbsScaler',\
'robust':'RobustScaler', 'quantile':'QuantileTransformer'}
s = getattr(preprocessing, scalers[scaler])
s = s(**kwargs)
df = Preprocessor.fillna(df, missing=missing)
return s.fit(df)
示例4: scale
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def scale(df, missing="zeros", scaler="robust", **kwargs):
"""
Scale values in a Data Frame using the relevant sklearn preprocessing method.
Valid options for the scaler are: standard, minmax, maxabs, robust, quantile
Missing values must be dealt with before the scaling is applied.
Valid options specified through the missing parameter are: zeros, mean, median, mode
"""
scalers = {'standard':'StandardScaler', 'minmax':'MinMaxScaler', 'maxabs':'MaxAbsScaler',\
'robust':'RobustScaler', 'quantile':'QuantileTransformer'}
s = getattr(preprocessing, scalers[scaler])
s = s(**kwargs)
df = fillna(df, method=missing)
df = pd.DataFrame(s.fit_transform(df), index=df.index, columns=df.columns)
return df
示例5: __init__
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def __init__(
self,
component_config: Optional[Dict[Text, Any]] = None,
clf: "sklearn.model_selection.GridSearchCV" = None,
le: Optional["sklearn.preprocessing.LabelEncoder"] = None,
) -> None:
"""Construct a new intent classifier using the sklearn framework."""
from sklearn.preprocessing import LabelEncoder
super().__init__(component_config)
if le is not None:
self.le = le
else:
self.le = LabelEncoder()
self.clf = clf
示例6: load
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def load(
cls,
meta: Dict[Text, Any],
model_dir: Optional[Text] = None,
model_metadata: Optional[Metadata] = None,
cached_component: Optional["SklearnIntentClassifier"] = None,
**kwargs: Any,
) -> "SklearnIntentClassifier":
from sklearn.preprocessing import LabelEncoder
classifier_file = os.path.join(model_dir, meta.get("classifier"))
encoder_file = os.path.join(model_dir, meta.get("encoder"))
if os.path.exists(classifier_file):
classifier = io_utils.json_unpickle(classifier_file)
classes = io_utils.json_unpickle(encoder_file)
encoder = LabelEncoder()
encoder.classes_ = classes
return cls(meta, classifier, encoder)
else:
return cls(meta)
示例7: __init__
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def __init__(self,
component_config=None, # type: Dict[Text, Any]
clf=None, # type: sklearn.model_selection.GridSearchCV
le=None # type: sklearn.preprocessing.LabelEncoder
):
# type: (...) -> None
"""Construct a new intent classifier using the sklearn framework."""
from sklearn.preprocessing import LabelEncoder
super(SklearnIntentClassifier, self).__init__(component_config)
if le is not None:
self.le = le
else:
self.le = LabelEncoder()
self.clf = clf
_sklearn_numpy_warning_fix()
示例8: convert_inst_scores_to_cls_scores
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def convert_inst_scores_to_cls_scores(similarity, testset0, testset1, num_identity, lbl_map):
cls_scores1 = []
for i in range(len(similarity)):
cls_score = np.zeros(num_identity)
for j in range(len(similarity[i])):
id = lbl_map[testset1[j].identity_id]
cls_score[id] = max(cls_score[id], similarity[i][j])
cls_scores1.append(cls_score)
cls_scores1 = np.array(cls_scores1)
cls_scores1 = sklearn.preprocessing.normalize(cls_scores1)
similarity = np.transpose(similarity)
cls_scores2 = []
for i in range(len(similarity)):
cls_score = np.zeros(num_identity)
for j in range(len(similarity[i])):
id = lbl_map[testset0[j].identity_id]
cls_score[id] = max(cls_score[id], similarity[i][j])
cls_scores2.append(cls_score)
cls_scores2 = np.array(cls_scores2)
cls_scores2 = sklearn.preprocessing.normalize(cls_scores2)
return cls_scores1, cls_scores2
示例9: squiggle_search2_old
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def squiggle_search2_old(squiggle,kmerhash,seqlen):
result=[]
for ref in kmerhash:
#print "ss2",ref
queryarray = sklearn.preprocessing.scale(np.array(squiggle),axis=0,with_mean=True,with_std=True,copy=True)
dist, cost, path = mlpy.dtw_subsequence(queryarray,kmerhash[ref]['Fprime'])
result.append((dist,ref,"F",path[1][0],ref,path[1][-1]))
dist, cost, path = mlpy.dtw_subsequence(queryarray,kmerhash[ref]['Rprime'])
result.append((dist,ref,"R",path[1][0],ref,path[1][-1]))
#('J02459', 41.017514495176989, 'F', 10003, 'J02459', 10198)
#distanceR,seqmatchnameR,frR,rsR,reR,qsR,qeR=sorted(result,key=lambda result: result[0])[0]
#return seqmatchnameR,distanceR,frR,rsR,reR,qsR,qeR
return sorted(result,key=lambda result: result[0])[0][1],sorted(result,key=lambda result: result[0])[0][0],sorted(result,key=lambda result: result[0])[0][2],sorted(result,key=lambda result: result[0])[0][3],sorted(result,key=lambda result: result[0])[0][4],sorted(result,key=lambda result: result[0])[0][5]
######################################################################
######################################################################
示例10: get_feature
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def get_feature(imgs, nets):
count = len(imgs)
data = mx.nd.zeros(shape = (count*2, 3, imgs[0].shape[0], imgs[0].shape[1]))
for idx, img in enumerate(imgs):
img = img[:,:,::-1] #to rgb
img = np.transpose( img, (2,0,1) )
for flipid in [0,1]:
_img = np.copy(img)
if flipid==1:
_img = _img[:,:,::-1]
_img = nd.array(_img)
data[count*flipid+idx] = _img
F = []
for net in nets:
db = mx.io.DataBatch(data=(data,))
net.model.forward(db, is_train=False)
x = net.model.get_outputs()[0].asnumpy()
embedding = x[0:count,:] + x[count:,:]
embedding = sklearn.preprocessing.normalize(embedding)
#print('emb', embedding.shape)
F.append(embedding)
F = np.concatenate(F, axis=1)
F = sklearn.preprocessing.normalize(F)
#print('F', F.shape)
return F
示例11: get_feature
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def get_feature(buffer):
global emb_size
if use_flip:
input_blob = np.zeros( (len(buffer)*2, 3, image_shape[1], image_shape[2] ) )
else:
input_blob = np.zeros( (len(buffer), 3, image_shape[1], image_shape[2] ) )
idx = 0
for item in buffer:
img = face_preprocess.read_image(item[0], mode='rgb')
img = face_preprocess.preprocess(img, bbox=None, landmark=item[1], image_size='%d,%d'%(image_shape[1], image_shape[2]))
img = np.transpose( img, (2,0,1) )
attempts = [0,1] if use_flip else [0]
for flipid in attempts:
_img = np.copy(img)
if flipid==1:
do_flip(_img)
input_blob[idx] = _img
idx+=1
data = mx.nd.array(input_blob)
db = mx.io.DataBatch(data=(data,))
net.model.forward(db, is_train=False)
_embedding = net.model.get_outputs()[0].asnumpy()
if emb_size==0:
emb_size = _embedding.shape[1]
print('set emb_size to ', emb_size)
embedding = np.zeros( (len(buffer), emb_size), dtype=np.float32 )
if use_flip:
embedding1 = _embedding[0::2]
embedding2 = _embedding[1::2]
embedding = embedding1+embedding2
else:
embedding = _embedding
embedding = sklearn.preprocessing.normalize(embedding)
return embedding
示例12: get_feature
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def get_feature(buffer):
global emb_size
if use_flip:
input_blob = np.zeros( (len(buffer)*2, 3, image_shape[1], image_shape[2] ) )
else:
input_blob = np.zeros( (len(buffer), 3, image_shape[1], image_shape[2] ) )
idx = 0
for item in buffer:
img = cv2.imread(item)[:,:,::-1] #to rgb
img = np.transpose( img, (2,0,1) )
attempts = [0,1] if use_flip else [0]
for flipid in attempts:
_img = np.copy(img)
if flipid==1:
do_flip(_img)
input_blob[idx] = _img
idx+=1
data = mx.nd.array(input_blob)
db = mx.io.DataBatch(data=(data,))
net.model.forward(db, is_train=False)
_embedding = net.model.get_outputs()[0].asnumpy()
if emb_size==0:
emb_size = _embedding.shape[1]
print('set emb_size to ', emb_size)
embedding = np.zeros( (len(buffer), emb_size), dtype=np.float32 )
if use_flip:
embedding1 = _embedding[0::2]
embedding2 = _embedding[1::2]
embedding = embedding1+embedding2
else:
embedding = _embedding
embedding = sklearn.preprocessing.normalize(embedding)
return embedding
示例13: get_feature
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def get_feature(buffer):
global emb_size
input_count = len(buffer)
if use_flip:
input_count *= 2
network_count = input_count
if input_count%ctx_num!=0:
network_count = (input_count//ctx_num+1)*ctx_num
input_blob = np.zeros( (network_count, 3, image_shape[1], image_shape[2]), dtype=np.float32)
idx = 0
for item in buffer:
img = cv2.imread(item)[:,:,::-1] #to rgb
img = np.transpose( img, (2,0,1) )
attempts = [0,1] if use_flip else [0]
for flipid in attempts:
_img = np.copy(img)
if flipid==1:
do_flip(_img)
input_blob[idx] = _img
idx+=1
data = mx.nd.array(input_blob)
db = mx.io.DataBatch(data=(data,))
net.model.forward(db, is_train=False)
_embedding = net.model.get_outputs()[0].asnumpy()
_embedding = _embedding[0:input_count]
if emb_size==0:
emb_size = _embedding.shape[1]
print('set emb_size to ', emb_size)
embedding = np.zeros( (len(buffer), emb_size), dtype=np.float32 )
if use_flip:
embedding1 = _embedding[0::2]
embedding2 = _embedding[1::2]
embedding = embedding1+embedding2
else:
embedding = _embedding
embedding = sklearn.preprocessing.normalize(embedding)
return embedding
示例14: predict_proba
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def predict_proba(self, X):
predictions = self.predict(X)
predictions = sklearn.preprocessing.scale(predictions)
predictions = 1.0 / (1.0 + np.exp(-0.5 * predictions))
return np.vstack((1.0 - predictions, predictions)).T
示例15: svc_example
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import preprocessing [as 别名]
def svc_example(n_samples = 10000, n_features = 4):
from sklearn.svm import LinearSVC
from sklearn.preprocessing import PolynomialFeatures
from sklearn.datasets import make_classification
X,Y = make_classification(n_samples, n_features)
#pp = PolynomialFeatures(degree=3)
#X = pp.fit_transform(X)
m = LinearSVC()
m.fit(X,Y)