本文整理汇总了Python中sklearn.random_projection.GaussianRandomProjection方法的典型用法代码示例。如果您正苦于以下问题:Python random_projection.GaussianRandomProjection方法的具体用法?Python random_projection.GaussianRandomProjection怎么用?Python random_projection.GaussianRandomProjection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.random_projection
的用法示例。
在下文中一共展示了random_projection.GaussianRandomProjection方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_projection
# 需要导入模块: from sklearn import random_projection [as 别名]
# 或者: from sklearn.random_projection import GaussianRandomProjection [as 别名]
def random_projection(mat, dim=3):
"""
Projects a matrix of dimensions m x oldim to m x dim using a random
projection
"""
# include has to be here for multiprocessing problems
from sklearn import random_projection as rp
if mat.is_cuda:
device = torch.device("cuda")
else:
device = torch.device("cpu")
# project
m, oldim = mat.shape
t = rp.GaussianRandomProjection()
proj_mat = torch.tensor(t._make_random_matrix(dim, oldim))
proj_mat = proj_mat.to(device)
output = torch.matmul(mat.double(), proj_mat.t())
# check new dim
assert(output.shape[0] == m)
assert(output.shape[1] == dim)
return output
示例2: transform_bag_of_words
# 需要导入模块: from sklearn import random_projection [as 别名]
# 或者: from sklearn.random_projection import GaussianRandomProjection [as 别名]
def transform_bag_of_words(filename, n_dimensions, out_fn):
import gzip
from scipy.sparse import lil_matrix
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn import random_projection
with gzip.open(filename, 'rb') as f:
file_content = f.readlines()
entries = int(file_content[0])
words = int(file_content[1])
file_content = file_content[3:] # strip first three entries
print("building matrix...")
A = lil_matrix((entries, words))
for e in file_content:
doc, word, cnt = [int(v) for v in e.strip().split()]
A[doc - 1, word - 1] = cnt
print("normalizing matrix entries with tfidf...")
B = TfidfTransformer().fit_transform(A)
print("reducing dimensionality...")
C = random_projection.GaussianRandomProjection(
n_components=n_dimensions).fit_transform(B)
X_train, X_test = train_test_split(C)
write_output(numpy.array(X_train), numpy.array(
X_test), out_fn, 'angular')
示例3: __init__
# 需要导入模块: from sklearn import random_projection [as 别名]
# 或者: from sklearn.random_projection import GaussianRandomProjection [as 别名]
def __init__(self, **kwargs):
super().__init__()
self.estimator = sk_rp.GaussianRandomProjection(**kwargs)
示例4: __init__
# 需要导入模块: from sklearn import random_projection [as 别名]
# 或者: from sklearn.random_projection import GaussianRandomProjection [as 别名]
def __init__(self, n_components='auto', eps=0.1, random_state=None):
self._hyperparams = {
'n_components': n_components,
'eps': eps,
'random_state': random_state}
self._wrapped_model = Op(**self._hyperparams)
示例5: test_gaussian_random_projection_float32
# 需要导入模块: from sklearn import random_projection [as 别名]
# 或者: from sklearn.random_projection import GaussianRandomProjection [as 别名]
def test_gaussian_random_projection_float32(self):
rng = np.random.RandomState(42)
pt = GaussianRandomProjection(n_components=4)
X = rng.rand(10, 5)
model = pt.fit(X)
assert model.transform(X).shape[1] == 4
model_onnx = convert_sklearn(
model, "scikit-learn GaussianRandomProjection",
[("inputs", FloatTensorType([None, X.shape[1]]))],
target_opset=TARGET_OPSET)
self.assertIsNotNone(model_onnx)
dump_data_and_model(X.astype(np.float32), model,
model_onnx, basename="GaussianRandomProjection")
示例6: test_gaussian_random_projection_float64
# 需要导入模块: from sklearn import random_projection [as 别名]
# 或者: from sklearn.random_projection import GaussianRandomProjection [as 别名]
def test_gaussian_random_projection_float64(self):
rng = np.random.RandomState(42)
pt = GaussianRandomProjection(n_components=4)
X = rng.rand(10, 5).astype(np.float64)
model = pt.fit(X)
model_onnx = to_onnx(model, X[:1], dtype=np.float64,
target_opset=TARGET_OPSET)
self.assertIsNotNone(model_onnx)
dump_data_and_model(X, model,
model_onnx, basename="GaussianRandomProjection64")
示例7: test_objectmapper
# 需要导入模块: from sklearn import random_projection [as 别名]
# 或者: from sklearn.random_projection import GaussianRandomProjection [as 别名]
def test_objectmapper(self):
df = pdml.ModelFrame([])
self.assertIs(df.random_projection.GaussianRandomProjection, rp.GaussianRandomProjection)
self.assertIs(df.random_projection.SparseRandomProjection, rp.SparseRandomProjection)
self.assertIs(df.random_projection.johnson_lindenstrauss_min_dim, rp.johnson_lindenstrauss_min_dim)