本文整理汇总了Python中numpy.object_函数的典型用法代码示例。如果您正苦于以下问题:Python object_函数的具体用法?Python object_怎么用?Python object_使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了object_函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
def train(fpath):
df = pd.read_csv(fpath)
df = df.drop(['DateTime'], axis=1)
df.SubId = np.object_(np.int64(df.SubId))
df.UserId = np.object_(df.UserId)
df.Rating = np.int64(df.Rating)
temp = df.UserId.value_counts()[df.UserId.value_counts() < 10].index
temp = set(temp)
remain = []
for i in df.index:
if df.UserId[i] not in temp:
remain.append(i)
df = df.loc[remain]
sf = gl.SFrame(df)
print 'finished reading in data'
training, test = gl.recommender.util.random_split_by_user(sf,
user_id='UserId',
item_id='SubId',
item_test_proportion=0.2,
random_seed=1234)
rcmder = gl.recommender.factorization_recommender.create(training,
user_id='UserId',
item_id='SubId',
target='Rating',
regularization=1e-5)
print 'finished training model'
print rcmder.evaluate(test, target='Rating')
return rcmder
示例2: train
def train(fpath):
df = pd.read_csv(fpath)
df = df.drop(['DateTime'], axis=1)
df.SubId = np.object_(np.int64(df.SubId))
df.UserId = np.object_(df.UserId)
df.Rating = np.int64(df.Rating)
temp = df.UserId.value_counts()[df.UserId.value_counts() < 10].index
temp = set(temp)
remain = []
for i in df.index:
if df.UserId[i] not in temp:
remain.append(i)
df = df.loc[remain]
sf = gl.SFrame(df)
print 'finished reading in data'
dataset, test = gl.recommender.util.random_split_by_user(sf,
user_id='UserId',
item_id='SubId',
item_test_proportion=0.2,
random_seed=2345)
training, validate = gl.recommender.util.random_split_by_user(dataset,
user_id='UserId',
item_id='SubId',
item_test_proportion=0.25,
random_seed=3456)
stype = ['jaccard', 'cosine', 'pearson']
thres = [10 ** e for e in range(-8, 1)]
res = {}
min_rmse = 99999.0
coor_min_rmse = (stype[0], thres[0])
for j in stype:
for i in thres:
rcmder = gl.recommender.item_similarity_recommender.create(training,
user_id='UserId',
item_id='SubId',
target='Rating',
threshold=i,
similarity_type=j)
res[(j, i)] = rcmder.evaluate(validate, metric='rmse',
target='Rating')['rmse_overall']
if res[(j, i)] < min_rmse:
min_rmse = res[(j, i)]
coor_min_rmse = (j, i)
print res
print 'best combination is {} with RMSE {}'.format(coor_min_rmse, min_rmse)
rcmder = gl.recommender.item_similarity_recommender.create(dataset,
user_id='UserId',
item_id='SubId',
target='Rating',
threshold=coor_min_rmse[1],
similarity_type=coor_min_rmse[0])
print 'finished training model'
print rcmder.evaluate(test, metric='rmse', target='Rating')
return rcmder
示例3: test_for_object_scalar_creation
def test_for_object_scalar_creation(self, level=rlevel):
"""Ticket #816"""
a = np.object_()
b = np.object_(3)
b2 = np.object_(3.0)
c = np.object_([4,5])
d = np.object_([None, {}, []])
assert a is None
assert type(b) is int
assert type(b2) is float
assert type(c) is np.ndarray
assert c.dtype == object
assert d.dtype == object
示例4: test_for_object_scalar_creation
def test_for_object_scalar_creation(self):
import numpy as np
import sys
a = np.object_()
b = np.object_(3)
b2 = np.object_(3.0)
c = np.object_([4, 5])
d = np.array([None])[0]
assert a is None
assert type(b) is int
assert type(b2) is float
assert type(c) is np.ndarray
assert c.dtype == object
assert type(d) is type(None)
if '__pypy__' in sys.builtin_module_names:
skip('not implemented yet')
e = np.object_([None, {}, []])
assert e.dtype == object
示例5: test_isscalar_numpy_array_scalars
def test_isscalar_numpy_array_scalars(self):
self.assertTrue(is_scalar(np.int64(1)))
self.assertTrue(is_scalar(np.float64(1.)))
self.assertTrue(is_scalar(np.int32(1)))
self.assertTrue(is_scalar(np.object_('foobar')))
self.assertTrue(is_scalar(np.str_('foobar')))
self.assertTrue(is_scalar(np.unicode_(u('foobar'))))
self.assertTrue(is_scalar(np.bytes_(b'foobar')))
self.assertTrue(is_scalar(np.datetime64('2014-01-01')))
self.assertTrue(is_scalar(np.timedelta64(1, 'h')))
示例6: test_isscalar_numpy_array_scalars
def test_isscalar_numpy_array_scalars(self):
self.assertTrue(lib.isscalar(np.int64(1)))
self.assertTrue(lib.isscalar(np.float64(1.0)))
self.assertTrue(lib.isscalar(np.int32(1)))
self.assertTrue(lib.isscalar(np.object_("foobar")))
self.assertTrue(lib.isscalar(np.str_("foobar")))
self.assertTrue(lib.isscalar(np.unicode_(u("foobar"))))
self.assertTrue(lib.isscalar(np.bytes_(b"foobar")))
self.assertTrue(lib.isscalar(np.datetime64("2014-01-01")))
self.assertTrue(lib.isscalar(np.timedelta64(1, "h")))
示例7: test_generic_roundtrip
def test_generic_roundtrip(self):
values = [
np.int_(1),
np.int32(-2),
np.float_(2.5),
np.nan,
-np.inf,
np.inf,
np.datetime64('2014-01-01'),
np.str_('foo'),
np.unicode_('bar'),
np.object_({'a': 'b'}),
np.complex_(1 - 2j)
]
for value in values:
decoded = self.roundtrip(value)
assert_equal(decoded, value)
self.assertTrue(isinstance(decoded, type(value)))
示例8: test_generic_roundtrip
def test_generic_roundtrip(self):
if self.should_skip:
return self.skip("numpy is not importable")
values = [
np.int_(1),
np.int32(-2),
np.float_(2.5),
np.nan,
-np.inf,
np.inf,
np.datetime64("2014-01-01"),
np.str_("foo"),
np.unicode_("bar"),
np.object_({"a": "b"}),
np.complex_(1 - 2j),
]
for value in values:
decoded = self.roundtrip(value)
assert_equal(decoded, value)
self.assertTrue(isinstance(decoded, type(value)))
示例9: assert_equal_none_format
def assert_equal_none_format(a, b, options=None):
# Compares a and b for equality. b is always the original. If they
# are dictionaries, a must be a structured ndarray and they must
# have the same set of keys, after which they values must all be
# compared. If they are a collection type (list, tuple, set,
# frozenset, or deque), then the compairison must be made with b
# converted to an object array. If the original is not a numpy type
# (isn't or doesn't inherit from np.generic or np.ndarray), then it
# is a matter of converting it to the appropriate numpy
# type. Otherwise, both are supposed to be numpy types. For object
# arrays, each element must be iterated over to be compared. Then,
# if it isn't a string type, then they must have the same dtype,
# shape, and all elements. If it is an empty string, then it would
# have been stored as just a null byte (recurse to do that
# comparison). If it is a bytes_ type, the dtype, shape, and
# elements must all be the same. If it is string_ type, we must
# convert to uint32 and then everything can be compared. Big longs
# and ints get written as numpy.bytes_.
if type(b) == dict or (sys.hexversion >= 0x2070000
and type(b) == collections.OrderedDict):
assert type(a) == np.ndarray
assert a.dtype.names is not None
# Determine if any of the keys could not be stored as str. If
# they all can be, then the dtype field names should be the
# keys. Otherwise, they should be 'keys' and 'values'.
all_str_keys = True
if sys.hexversion >= 0x03000000:
tp_str = str
tp_bytes = bytes
converters = {tp_str: lambda x: x,
tp_bytes: lambda x: x.decode('UTF-8'),
np.bytes_:
lambda x: bytes(x).decode('UTF-8'),
np.unicode_: lambda x: str(x)}
tp_conv = lambda x: converters[type(x)](x)
tp_conv_str = lambda x: tp_conv(x)
else:
tp_str = unicode
tp_bytes = str
converters = {tp_str: lambda x: x,
tp_bytes: lambda x: x.decode('UTF-8'),
np.bytes_:
lambda x: bytes(x).decode('UTF-8'),
np.unicode_: lambda x: unicode(x)}
tp_conv = lambda x: converters[type(x)](x)
tp_conv_str = lambda x: tp_conv(x).encode('UTF-8')
tps = tuple(converters.keys())
for k in b.keys():
if type(k) not in tps:
all_str_keys = False
break
try:
k_str = tp_conv(k)
except:
all_str_keys = False
break
if all_str_keys:
assert set(a.dtype.names) == set([tp_conv_str(k)
for k in b.keys()])
for k in b:
assert_equal_none_format(a[tp_conv_str(k)][0],
b[k], options)
else:
names = (options.dict_like_keys_name,
options.dict_like_values_name)
assert set(a.dtype.names) == set(names)
keys = a[names[0]]
values = a[names[1]]
assert_equal_none_format(keys, tuple(b.keys()), options)
assert_equal_none_format(values, tuple(b.values()), options)
elif type(b) in (list, tuple, set, frozenset, collections.deque):
assert_equal_none_format(a, np.object_(list(b)), options)
elif not isinstance(b, (np.generic, np.ndarray)):
if b is None:
# It should be np.float64([])
assert type(a) == np.ndarray
assert a.dtype == np.float64([]).dtype
assert a.shape == (0, )
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, (bytes, bytearray))) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, (bytes, bytearray))):
assert a == np.bytes_(b)
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, str)) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, unicode)):
assert_equal_none_format(a, np.unicode_(b), options)
elif (sys.hexversion >= 0x03000000 \
and type(b) == int) \
or (sys.hexversion < 0x03000000 \
and type(b) == long):
if b > 2**63 or b < -(2**63 - 1):
assert_equal_none_format(a, np.bytes_(b), options)
else:
assert_equal_none_format(a, np.int64(b), options)
else:
assert_equal_none_format(a, np.array(b)[()], options)
else:
#.........这里部分代码省略.........
示例10: assert_equal_matlab_format
def assert_equal_matlab_format(a, b, options=None):
# Compares a and b for equality. b is always the original. If they
# are dictionaries, a must be a structured ndarray and they must
# have the same set of keys, after which they values must all be
# compared. If they are a collection type (list, tuple, set,
# frozenset, or deque), then the compairison must be made with b
# converted to an object array. If the original is not a numpy type
# (isn't or doesn't inherit from np.generic or np.ndarray), then it
# is a matter of converting it to the appropriate numpy
# type. Otherwise, both are supposed to be numpy types. For object
# arrays, each element must be iterated over to be compared. Then,
# if it isn't a string type, then they must have the same dtype,
# shape, and all elements. All strings are converted to numpy.str_
# on read unless they were stored as a numpy.bytes_ due to having
# non-ASCII characters. If it is empty, it has shape (1, 0). A
# numpy.str_ has all of its strings per row compacted together. A
# numpy.bytes_ string has to have the same thing done, but then it
# needs to be converted up to UTF-32 and to numpy.str_ through
# uint32. Big longs and ints end up getting converted to UTF-16
# uint16's when written and read back as UTF-32 numpy.unicode_.
#
# In all cases, we expect things to be at least two dimensional
# arrays.
if type(b) == dict or (sys.hexversion >= 0x2070000
and type(b) == collections.OrderedDict):
assert type(a) == np.ndarray
assert a.dtype.names is not None
# Determine if any of the keys could not be stored as str. If
# they all can be, then the dtype field names should be the
# keys. Otherwise, they should be 'keys' and 'values'.
all_str_keys = True
if sys.hexversion >= 0x03000000:
tp_str = str
tp_bytes = bytes
converters = {tp_str: lambda x: x,
tp_bytes: lambda x: x.decode('UTF-8'),
np.bytes_:
lambda x: bytes(x).decode('UTF-8'),
np.unicode_: lambda x: str(x)}
tp_conv = lambda x: converters[type(x)](x)
tp_conv_str = lambda x: tp_conv(x)
else:
tp_str = unicode
tp_bytes = str
converters = {tp_str: lambda x: x,
tp_bytes: lambda x: x.decode('UTF-8'),
np.bytes_:
lambda x: bytes(x).decode('UTF-8'),
np.unicode_: lambda x: unicode(x)}
tp_conv = lambda x: converters[type(x)](x)
tp_conv_str = lambda x: tp_conv(x).encode('UTF-8')
tps = tuple(converters.keys())
for k in b.keys():
if type(k) not in tps:
all_str_keys = False
break
try:
k_str = tp_conv(k)
except:
all_str_keys = False
break
if all_str_keys:
assert set(a.dtype.names) == set([tp_conv_str(k)
for k in b.keys()])
for k in b:
assert_equal_matlab_format(a[tp_conv_str(k)][0],
b[k], options)
else:
names = (options.dict_like_keys_name,
options.dict_like_values_name)
assert set(a.dtype.names) == set(names)
keys = a[names[0]][0]
values = a[names[1]][0]
assert_equal_matlab_format(keys, tuple(b.keys()), options)
assert_equal_matlab_format(values, tuple(b.values()),
options)
elif type(b) in (list, tuple, set, frozenset, collections.deque):
assert_equal_matlab_format(a, np.object_(list(b)), options)
elif not isinstance(b, (np.generic, np.ndarray)):
if b is None:
# It should be np.zeros(shape=(0, 1), dtype='float64'))
assert type(a) == np.ndarray
assert a.dtype == np.dtype('float64')
assert a.shape == (1, 0)
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, (bytes, str, bytearray))) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, (bytes, unicode, bytearray))):
if len(b) == 0:
assert_equal(a, np.zeros(shape=(1, 0), dtype='U'),
options)
elif isinstance(b, (bytes, bytearray)):
try:
c = np.unicode_(b.decode('ASCII'))
except:
c = np.bytes_(b)
assert_equal(a, np.atleast_2d(c), options)
else:
assert_equal(a, np.atleast_2d(np.unicode_(b)), options)
#.........这里部分代码省略.........
示例11: assert_equal_none_format
def assert_equal_none_format(a, b):
# Compares a and b for equality. b is always the original. If they
# are dictionaries, a must be a structured ndarray and they must
# have the same set of keys, after which they values must all be
# compared. If they are a collection type (list, tuple, set,
# frozenset, or deque), then the compairison must be made with b
# converted to an object array. If the original is not a numpy type
# (isn't or doesn't inherit from np.generic or np.ndarray), then it
# is a matter of converting it to the appropriate numpy
# type. Otherwise, both are supposed to be numpy types. For object
# arrays, each element must be iterated over to be compared. Then,
# if it isn't a string type, then they must have the same dtype,
# shape, and all elements. If it is an empty string, then it would
# have been stored as just a null byte (recurse to do that
# comparison). If it is a bytes_ type, the dtype, shape, and
# elements must all be the same. If it is string_ type, we must
# convert to uint32 and then everything can be compared.
if type(b) == dict:
assert type(a) == np.ndarray
assert a.dtype.names is not None
assert set(a.dtype.names) == set(b.keys())
for k in b:
assert_equal_none_format(a[k][0], b[k])
elif type(b) in (list, tuple, set, frozenset, collections.deque):
assert_equal_none_format(a, np.object_(list(b)))
elif not isinstance(b, (np.generic, np.ndarray)):
if b is None:
# It should be np.float64([])
assert type(a) == np.ndarray
assert a.dtype == np.float64([]).dtype
assert a.shape == (0, )
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, (bytes, bytearray))) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, (bytes, bytearray))):
assert a == np.bytes_(b)
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, str)) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, unicode)):
assert_equal_none_format(a, np.unicode_(b))
else:
assert_equal_none_format(a, np.array(b)[()])
else:
if b.dtype.name != 'object':
if b.dtype.char in ('U', 'S'):
if b.dtype.char == 'S' and b.shape == tuple() \
and len(b) == 0:
assert_equal(a, \
np.zeros(shape=tuple(), dtype=b.dtype.char))
elif b.dtype.char == 'U':
if b.shape == tuple() and len(b) == 0:
c = np.uint32(())
else:
c = np.atleast_1d(b).view(np.uint32)
assert a.dtype == c.dtype
assert a.shape == c.shape
npt.assert_equal(a, c)
else:
assert a.dtype == b.dtype
assert a.shape == b.shape
npt.assert_equal(a, b)
else:
assert a.dtype == b.dtype
# Now, if b.shape is just all ones, then a.shape will
# just be (1,). Otherwise, we need to compare the shapes
# directly. Also, dimensions need to be squeezed before
# comparison in this case.
assert np.prod(a.shape) == np.prod(b.shape)
assert a.shape == b.shape \
or (np.prod(b.shape) == 1 and a.shape == (1,))
if np.prod(a.shape) == 1:
a = np.squeeze(a)
b = np.squeeze(b)
npt.assert_equal(a, b)
else:
assert a.dtype == b.dtype
assert a.shape == b.shape
for index, x in np.ndenumerate(a):
assert_equal_none_format(a[index], b[index])
示例12: assert_equal_matlab_format
def assert_equal_matlab_format(a, b):
# Compares a and b for equality. b is always the original. If they
# are dictionaries, a must be a structured ndarray and they must
# have the same set of keys, after which they values must all be
# compared. If they are a collection type (list, tuple, set,
# frozenset, or deque), then the compairison must be made with b
# converted to an object array. If the original is not a numpy type
# (isn't or doesn't inherit from np.generic or np.ndarray), then it
# is a matter of converting it to the appropriate numpy
# type. Otherwise, both are supposed to be numpy types. For object
# arrays, each element must be iterated over to be compared. Then,
# if it isn't a string type, then they must have the same dtype,
# shape, and all elements. All strings are converted to numpy.str_
# on read. If it is empty, it has shape (1, 0). A numpy.str_ has all
# of its strings per row compacted together. A numpy.bytes_ string
# has to have the same thing done, but then it needs to be converted
# up to UTF-32 and to numpy.str_ through uint32.
#
# In all cases, we expect things to be at least two dimensional
# arrays.
if type(b) == dict:
assert type(a) == np.ndarray
assert a.dtype.names is not None
assert set(a.dtype.names) == set(b.keys())
for k in b:
assert_equal_matlab_format(a[k][0], b[k])
elif type(b) in (list, tuple, set, frozenset, collections.deque):
assert_equal_matlab_format(a, np.object_(list(b)))
elif not isinstance(b, (np.generic, np.ndarray)):
if b is None:
# It should be np.zeros(shape=(0, 1), dtype='float64'))
assert type(a) == np.ndarray
assert a.dtype == np.dtype('float64')
assert a.shape == (1, 0)
elif (sys.hexversion >= 0x03000000 \
and isinstance(b, (bytes, str, bytearray))) \
or (sys.hexversion < 0x03000000 \
and isinstance(b, (bytes, unicode, bytearray))):
if len(b) == 0:
assert_equal(a, np.zeros(shape=(1, 0), dtype='U'))
elif isinstance(b, (bytes, bytearray)):
assert_equal(a, np.atleast_2d(np.unicode_(b.decode())))
else:
assert_equal(a, np.atleast_2d(np.unicode_(b)))
else:
assert_equal(a, np.atleast_2d(np.array(b)))
else:
if b.dtype.name != 'object':
if b.dtype.char in ('U', 'S'):
if len(b) == 0 and (b.shape == tuple() \
or b.shape == (0, )):
assert_equal(a, np.zeros(shape=(1, 0),
dtype='U'))
elif b.dtype.char == 'U':
c = np.atleast_1d(b)
c = np.atleast_2d(c.view(np.dtype('U' \
+ str(c.shape[-1]*c.dtype.itemsize//4))))
assert a.dtype == c.dtype
assert a.shape == c.shape
npt.assert_equal(a, c)
elif b.dtype.char == 'S':
c = np.atleast_1d(b)
c = c.view(np.dtype('S' \
+ str(c.shape[-1]*c.dtype.itemsize)))
c = np.uint32(c.view(np.dtype('uint8')))
c = c.view(np.dtype('U' + str(c.shape[-1])))
c = np.atleast_2d(c)
assert a.dtype == c.dtype
assert a.shape == c.shape
npt.assert_equal(a, c)
pass
else:
c = np.atleast_2d(b)
assert a.dtype == c.dtype
assert a.shape == c.shape
npt.assert_equal(a, c)
else:
c = np.atleast_2d(b)
# An empty complex number gets turned into a real
# number when it is stored.
if np.prod(c.shape) == 0 \
and b.dtype.name.startswith('complex'):
c = np.real(c)
# If it is structured, check that the field names are
# the same, in the same order, and then go through them
# one by one. Otherwise, make sure the dtypes and shapes
# are the same before comparing all values.
if b.dtype.names is None and a.dtype.names is None:
assert a.dtype == c.dtype
assert a.shape == c.shape
npt.assert_equal(a, c)
else:
assert a.dtype.names is not None
assert b.dtype.names is not None
assert set(a.dtype.names) == set(b.dtype.names)
assert a.dtype.names == b.dtype.names
a = a.flatten()
b = b.flatten()
for k in b.dtype.names:
for index, x in np.ndenumerate(a):
#.........这里部分代码省略.........
示例13: train
def train(fpath):
df = pd.read_csv(fpath)
df = df.drop(['DateTime'], axis=1)
df.SubId = np.object_(np.int64(df.SubId))
df.UserId = np.object_(df.UserId)
df.Rating = np.int64(df.Rating)
# remove users with less than 50 ratings
temp = df.UserId.value_counts()[df.UserId.value_counts() < 50].index
temp = set(temp)
remain = []
for i in df.index:
if df.UserId[i] not in temp:
remain.append(i)
df = df.loc[remain]
# remove items with less than 50 ratings
temp = df.SubId.value_counts()[df.SubId.value_counts() < 50].index
temp = set(temp)
remain = []
for i in df.index:
if df.SubId[i] not in temp:
remain.append(i)
df = df.loc[remain]
sf = gl.SFrame(df)
print 'finished reading in data'
dataset, test = gl.recommender.util.random_split_by_user(sf,
user_id='UserId',
item_id='SubId',
item_test_proportion=0.2,
random_seed=2345)
training, validate = gl.recommender.util.random_split_by_user(dataset,
user_id='UserId',
item_id='SubId',
item_test_proportion=0.25,
random_seed=3456)
numf = [2 ** e for e in range(3, 8)]
regl = [1e-6, 3e-6, 1e-5, 3e-5, 1e-4, 3e-4, 1e-3]
res = {}
min_rmse = 99999.0
coor_min_rmse = (numf[0], regl[0])
for j in numf:
for i in regl:
rcmder = gl.recommender.factorization_recommender.create(training,
user_id='UserId',
item_id='SubId',
target='Rating',
regularization=i,
num_factors=j)
res[(j, i)] = rcmder.evaluate(validate, metric='rmse',
target='Rating')['rmse_overall']
if res[(j, i)] < min_rmse:
min_rmse = res[(j, i)]
coor_min_rmse = (j, i)
print res
print 'best combination is {} with RMSE {}'.format(coor_min_rmse, min_rmse)
rcmder = gl.recommender.factorization_recommender.create(dataset,
user_id='UserId',
item_id='SubId',
target='Rating',
regularization=coor_min_rmse[1],
num_factors=coor_min_rmse[0])
print 'finished training model'
print rcmder.evaluate(test, metric='rmse', target='Rating')
return rcmder