Python numpy 模块,float128() 实例源码
我们从Python开源项目中,提取了以下32个代码示例,用于说明如何使用numpy.float128()。
def numpy2bifrost(dtype):
if dtype == np.int8: return _bf.BF_DTYPE_I8
elif dtype == np.int16: return _bf.BF_DTYPE_I16
elif dtype == np.int32: return _bf.BF_DTYPE_I32
elif dtype == np.uint8: return _bf.BF_DTYPE_U8
elif dtype == np.uint16: return _bf.BF_DTYPE_U16
elif dtype == np.uint32: return _bf.BF_DTYPE_U32
elif dtype == np.float16: return _bf.BF_DTYPE_F16
elif dtype == np.float32: return _bf.BF_DTYPE_F32
elif dtype == np.float64: return _bf.BF_DTYPE_F64
elif dtype == np.float128: return _bf.BF_DTYPE_F128
elif dtype == ci8: return _bf.BF_DTYPE_CI8
elif dtype == ci16: return _bf.BF_DTYPE_CI16
elif dtype == ci32: return _bf.BF_DTYPE_CI32
elif dtype == cf16: return _bf.BF_DTYPE_CF16
elif dtype == np.complex64: return _bf.BF_DTYPE_CF32
elif dtype == np.complex128: return _bf.BF_DTYPE_CF64
elif dtype == np.complex256: return _bf.BF_DTYPE_CF128
else: raise ValueError("Unsupported dtype: " + str(dtype))
def numpy2string(dtype):
if dtype == np.int8: return 'i8'
elif dtype == np.int16: return 'i16'
elif dtype == np.int32: return 'i32'
elif dtype == np.int64: return 'i64'
elif dtype == np.uint8: return 'u8'
elif dtype == np.uint16: return 'u16'
elif dtype == np.uint32: return 'u32'
elif dtype == np.uint64: return 'u64'
elif dtype == np.float16: return 'f16'
elif dtype == np.float32: return 'f32'
elif dtype == np.float64: return 'f64'
elif dtype == np.float128: return 'f128'
elif dtype == np.complex64: return 'cf32'
elif dtype == np.complex128: return 'cf64'
elif dtype == np.complex256: return 'cf128'
else: raise TypeError("Unsupported dtype: " + str(dtype))
def watts_threshold(self, m):
"""
The watts threshold function.
Input
---------------
m [number] : Number of active neigbors
Return
---------------
[float] : Probability of activation
"""
if m >= self.params["params"]["threshold"]:
return np.float128(self.params["params"]["p"])
else:
return np.float128(0.0)
return
def bond_percolation(self, m):
"""
The bond percolation response function
Input
---------------
m [number] : Number of active neigbors
Return
---------------
[float] : Probability of activation
"""
if m > 0:
return (np.float128(1.0) - np.float128((1.0 - self.params["params"]["p"]) ** m)*(1.0-np.float128(self.params["params"]["p_spontaneous"])))
else:
return np.float128(self.params["params"]["p_spontaneous"])
def json_encode(obj):
try:
serial = obj.to_json()
serial['classname'] = obj.__class__.__qualname__
return serial
except AttributeError:
pass
# Convert numpy types:
if type(obj) in [np.int8, np.int16, np.int32, np.int64]:
return int(obj)
elif type(obj) in [np.float16, np.float32, np.float64, np.float128]:
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, datetime.datetime):
return obj.isoformat()
raise TypeError('Type not serialisable')
def test_returned_dtype(self):
dtypes = [np.int16, np.int32, np.int64, np.float32, np.float64]
if hasattr(np, 'float128'):
dtypes.append(np.float128)
for dtype in dtypes:
s = Series(range(10), dtype=dtype)
group_a = ['mean', 'std', 'var', 'skew', 'kurt']
group_b = ['min', 'max']
for method in group_a + group_b:
result = getattr(s, method)()
if is_integer_dtype(dtype) and method in group_a:
self.assertTrue(
result.dtype == np.float64,
"return dtype expected from %s is np.float64, "
"got %s instead" % (method, result.dtype))
else:
self.assertTrue(
result.dtype == dtype,
"return dtype expected from %s is %s, "
"got %s instead" % (method, dtype, result.dtype))
def hashint64(x,N):
'''
Convert a bit vector to a float128
Not to be confused with `pylab.packbits`
Parameters
----------
x : boolean or binary vector
N : positive integer, number of bits in each vector
Returns
-------
int64 : integer, stored in int64, whose binary bits match x
'''
if N>63:
raise ValueError('No more than 63 bits can be safely stored in int64')
return x.dot(2**np.int64(np.arange(N)))
def hashfloat128(x,N):
'''
Convert a bit vector to a float128
Not to be confused with `pylab.packbits`
Parameters
----------
x : boolean or binary vector
N : positive integer, number of bits in each vector
Returns
-------
float128 : integer, stored in float128, whose binary bits match x
'''
x = (np.array(x)!=0)
if not x.shape[-1]==N:
raise ValueError('The last dimension of x should match the bit vector length N')
if N>63:
raise ValueError('No more than 63 bits are safe at the moment')
return x.dot(2**np.float128(np.arange(N)))
def unhashfloat128(x,N):
'''
Unpack bits from number b; inverse of `hashbits()`
Not to be confused with `pylab.unpackbits`
Parameters
----------
x : float128
integer stored in float128, whose binary bits match x
N : positive integer, number of bits in each vector
Returns
-------
b : boolean or binary vector, unpacked
'''
if not x.dtype==np.float128:
raise ValueError('Expected to unpack bit data from np.float128')
x = x.copy()
b = []
for i in range(N):
b.append(x%2)
x = np.floor(x*0.5)
b = (np.uint8(b)==1)
return b.T
def _is_class_a_primitive(cls):
'''
Check if class is a number or string including numpy numbers
:param cls: any class
:return: True if class is a primitive class, else False
'''
primitives = [
np.float16, np.float32, np.float64, np.float128,
np.int8, np.int16, np.int32, np.int64,
bool, str, np.uint8, np.uint16, np.uint32, np.uint64,
int, float
]
return cls in primitives
def softmax_cost(out,y, theta3, filt1, filt2):
eout = np.exp(out, dtype=np.float128)
probs = eout/sum(eout)
p = sum(y*probs)
cost = -np.log(p) ## (Only data loss. No regularised loss)
return cost,probs
## Returns gradient for all the paramaters in each iteration
def name_nbit2numpy(name, nbit):
if name == 'i':
if nbit == 8: return np.int8
elif nbit == 16: return np.int16
elif nbit == 32: return np.int32
elif nbit == 64: return np.int64
else: raise TypeError("Invalid signed integer type size: %i" % nbit)
elif name == 'u':
if nbit == 8: return np.uint8
elif nbit == 16: return np.uint16
elif nbit == 32: return np.uint32
elif nbit == 64: return np.uint64
else: raise TypeError("Invalid unsigned integer type size: %i" % nbit)
elif name == 'f':
if nbit == 16: return np.float16
elif nbit == 32: return np.float32
elif nbit == 64: return np.float64
elif nbit == 128: return np.float128
else: raise TypeError("Invalid floating-point type size: %i" % nbit)
elif name == 'ci':
if nbit == 8: return ci8
elif nbit == 16: return ci16
elif nbit == 32: return ci32
# elif name in set(['ci', 'cu']):
# Note: This gives integer types in place of proper complex types
# return name_nbit2numpy(name[1:], nbit*2)
elif name == 'cf':
if nbit == 16: return cf16
elif nbit == 32: return np.complex64
elif nbit == 64: return np.complex128
elif nbit == 128: return np.complex256
else: raise TypeError("Invalid complex floating-point type size: %i" %
nbit)
else:
raise TypeError("Invalid type name: " + name)
def test_16byte(self):
self.run_simple_test_shmoo(np.float128)
def _print_Float(self, expr):
super_float = super()._print_Float(expr)
if self._settings['float128']:
super_float = 'float128(%r)' % super_float
if self._settings['use_autoeye']:
return 'autoeye(%s)' % super_float
return super_float
def print_features_info(dataset):
features_and_types = dataset.dtypes
print("\n[Debug] Print the feature number: ")
numberic_feature_number = 0
not_numberic_feature_number = 0
for feature_type in features_and_types:
if feature_type == np.int16 or feature_type == np.int32 or feature_type == np.int64 or feature_type == np.float16 or feature_type == np.float32 or feature_type == np.float64 or feature_type == np.float128 or feature_type == np.double:
numberic_feature_number += 1
else:
not_numberic_feature_number += 1
print("Total feature number: {}".format(len(features_and_types)))
print("Numberic feature number: {}".format(numberic_feature_number))
print("Not numberic feature number: {}".format(not_numberic_feature_number))
print("\n[Debug] Print the feature list of the dataset: ")
print(features_and_types)
print("\n[Debug] Print the feature presence: ")
example_number = len(dataset)
features_array = list(dataset.columns.values)
for feature_name in features_array:
feature_presence_number = len(dataset[feature_name][dataset[feature_name].notnull()])
feature_presence_percentage = 100.0 * feature_presence_number / example_number
# Example: "Age: 80.1346801347% (714 / 891)"
print("{}: {}% ({} / {})".format(feature_name, feature_presence_percentage, feature_presence_number, example_number))
print("\n[Debug] For numberic features, print the feature statistics: ")
feature_statistics = dataset.describe()
print(feature_statistics)
top_k_number = 5
print("\n[Debug] For all features, print the top {} values: ".format(top_k_number))
for i in range(len(features_array)):
feature_name = features_array[i]
top_k_feature_info = dataset[feature_name].value_counts()[:top_k_number]
print("\nFeature {} and the top {} values:".format(feature_name, top_k_number))
print(top_k_feature_info)
def test_scalar():
res = x2num.makenp(1.1)
assert isinstance(res, np.ndarray) and res.shape == (1,)
res = x2num.makenp(1000000000000000000000)
assert isinstance(res, np.ndarray) and res.shape == (1,)
res = x2num.makenp(np.float16(1.00000087))
assert isinstance(res, np.ndarray) and res.shape == (1,)
res = x2num.makenp(np.float128(1.00008+9))
assert isinstance(res, np.ndarray) and res.shape == (1,)
res = x2num.makenp(np.int64(100000000000))
assert isinstance(res, np.ndarray) and res.shape == (1,)
def _float(value):
t = type(value)
floats = [np.float, np.float32, np.float64, np.float128, np.float16, float]
return bool(np.sum([t is f for f in floats]))
def test_numpy_float128(self):
x = numpy.float128(55.3)
s = Serializable.dumps(x)
y = Serializable.from_json(s)
self.assertAlmostEqual(x, y, 5)
self.assertEqual(str(x.dtype), 'float128')
self.assertEqual(str(y.dtype), 'float128')
def get_full_conditional(self, sentence, m, z, n_z, n_m_z):
prod_nom, prod_den = [] , []
words = Counter(sentence)
for key, val in words.iteritems():
for x in range(val):
quantity = self.n_z_t[:,key] + self.beta + x
prod_nom.append(quantity)
# prod_nom *= (quantity)
prod_nom = np.array(prod_nom, dtype=np.float128)
left_denominator = n_z + self.beta*self.V
for x in range(len(sentence)):
quantity = left_denominator + x
prod_den.append(quantity)
# prod_den *= (quantity)
prod_den = np.array(prod_den, dtype=np.float128)
# print "Shapes of interest:", prod_den.shape, prod_nom.shape
prodall1 = np.divide(prod_nom,prod_den)
# print "After division:", prodall.shape
prodall = np.prod(prodall1, axis=0)
# print "After multiplication", prodall.shape
# prod_nom = np.prod(prod_nom, axis=0, dtype=np.float128)
# prod_den = np.prod(prod_den, axis=0, dtype=np.float128)
# left = prod_nom/prod_den
right = (n_m_z[m,:] + self.alpha)
p_z = prodall*right
# try:
# p_z /= np.sum(p_z)
# except:
# print p_z
# print prodall1
# print prodall
p_z /= np.sum(p_z)
# except RuntimeWarning:
# print 'Exception'
# print prodall
# print right
# print self.n_z_t[:,key]
return p_z.astype(np.float64)
def test_response_function(self):
"""
Verify the response functions
"""
for node in self.solver.G.nodes():
self.assertIn("rf", self.solver.G.node[node])
self.assertIsInstance(self.solver.G.node[node]["rf"], ResponseFunction )
self.assertIs(type(self.solver.G.node[node]["rf"].resp_func(0.0)), np.float128)
self.assertIs(type(self.solver.G.node[node]["rf"].resp_func(20.0)), np.float128)
def get_probabilities_Q_possible(self, possible_configs):
"""
Get the probability Q(l;n) for each configuration in the possible_configs dictionnary.
The implementation is based on the recursive equation of the paper.
Input
----------------------
possible_configs: Dictionnary where keys are the config as a string (i.e. "0101011011") and value are sets of the parent configurations.
Return
----------------------
Dictionnary of probabilities where the key is the configuration as a string (e.g. "10101110") and the value is Q(l;n)
"""
#the master dictionnary
dict_config = {}
prob_tot = 1.0
#order by size to simplify the calculation
possible_config_ordered_by_size = self.regroup_config_by_size(possible_configs)
N = len(self.G.nodes())
#Initial configuration
initial_config = [0]*N
if self.symbolic_:
dict_config[self.list_to_string(initial_config)] = "<prod>"+";".join(["G("+str(node)+","+str(0)+")" for node in self.G.nodes()])+"</prod>"
else:
dict_config[self.list_to_string(initial_config)] = np.float128(np.product([(1.0-self.G.nodes[node]["rf"].resp_func(0)) for node in self.G.nodes()]))
prob_tot -= dict_config[self.list_to_string(initial_config)]
for size in range(1,N+1):
if size in possible_config_ordered_by_size:
for config_str in possible_config_ordered_by_size[size]:
Q_ln = self.solve_specific_configuration(config_str, dict_config, possible_configs)
dict_config[config_str] = Q_ln
prob_tot -= Q_ln
dict_config["1"*N] = prob_tot
return dict_config
def jk(D,Bs,i,j,frac,possible,ri):
Num=factorial(ri-1)
Den=factorial(N_ij(D,Bs,i,j,ri)+ri-1)
frac*=np.float128(Num)/np.float128(Den)
for k in range(0,ri):
frac*=factorial(N_ijk(D,Bs,i,j,k,ri))
if N_ijk(D,Bs,i,j,k,ri)!=0 :
possible=1
return frac,possible
def getCsvData():
dtypes = {
"int8_value": numpy.int8,
"int16_value": numpy.int16,
"int32_value": numpy.int32,
# "int64_value": numpy.int64, # OverFlowError
"uint8_value": numpy.uint8,
"uint16_value": numpy.uint16,
"uint32_value": numpy.uint32,
# "uint64_value": numpy.uint64, # OverFlowError
"float16_value": numpy.float16,
"float32_value": numpy.float32,
"float64_value": numpy.float64,
# "float128_value": numpy.float128,
"bool_value": numpy.bool_
}
delimiter = ","
encoding = "utf-8"
parse_dates = ["timestamp_value"]
path = os.path.join(os.getcwdu(), "examples/testData/test1.csv")
if not os.path.exists(path):
path = os.path.join(os.getcwdu(), "testData/test1.csv")
df = pandas.read_csv(
path,
dtype=dtypes,
delimiter=delimiter,
encoding=encoding,
parse_dates=parse_dates
)
try:
df["int64_value"] = df["int64_value"].astype(numpy.int64)
df["uint64_value"] = df["uint64_value"].astype(numpy.uint64)
except:
raise
return df
def get_worst_live_point(self):
"""
selects the lowest likelihood live point
"""
logL_array = np.array([p.logL for p in self.params])
self.worst = logL_array.argmin()
self.logLmin.value = logL_array[self.worst]
self.logLmax = np.max(logL_array)
return np.float128(self.logLmin.value)
def poisson_logpdf(k,l):
'''
Gives the log-pdf for a poisson distribution with rate l
evaluated at points k. k should be a vector of integers.
Parameters
----------
Returns
-------
'''
# k,l = map(np.float128,(k,l))
return k*slog(l)-l-np.array([scipy.special.gammaln(x+1) for x in k])
#return k*slog(l)-l-np.array([log_factorial(x) for x in k])
def gaussian_logpdf(mu,sigma,x):
'''
Non-positive standar deviations will be clipped
Parameters
----------
Returns
-------
'''
mu,sigma,x = map(np.float128,(mu,sigma,x))
x = (x-mu)/sigma
return -0.5*x*x - slog(sigma) - logsqrt2pi
def sigmoid(x,returntype=LINALGMAXFLOAT):
'''
Soft-threshold function (rescaled hyperbolic tangent)
More numerically stable version
'''
return returntype(sexp(-np.logaddexp(ZERO128,-np.float128(x))))
# Sigmoid and derivatives
def g(x,returntype=LINALGMAXFLOAT):
'''
Evaluates g(x)=log(1+exp(x)) as accurately as possible.
'''
return returntype(np.logaddexp(ZERO128,np.float128(x)))
def f(x,returntype=LINALGMAXFLOAT):
'''
evaluates f(x)=1/(1+exp(-x)) as accurately as possible
'''
return returntype(sexp(-np.logaddexp(ZERO128,-np.float128(x))))
def f1(x,returntype=LINALGMAXFLOAT):
'''
Fist derivative of sigmoid
'''
x = np.float128(x)
return sexp(\
-np.logaddexp(ZERO128,-x)\
-np.logaddexp(ZERO128,x),
returntype=returntype)
def f2(x,returntype=LINALGMAXFLOAT):
'''
Second derivative of sigmoid
(q - p) p q
'''
x = np.float128(x)
logp = -np.logaddexp(ZERO128,-x)
logq = -np.logaddexp(ZERO128, x)
p = np.exp(np.minimum(F128EMAX,logp))
q = np.exp(np.minimum(F128EMAX,logq))
return returntype((q-p)*q*p);
def sample_patch_projective(image, inv_xform_3x3, patch_shape):
""" return a warped image as a numpy array with dtype float64 of size patch_size.
if input image is not already of type float64, it will be converted """
P = skimage.transform.ProjectiveTransform(inv_xform_3x3)
# skimage clips values to range [0,1] for floating point images. do scale and unscale here.
do_scale = False
og_dtype = image.dtype
if image.dtype in (np.float32, np.float64, np.float128, np.float16):
minval = np.nanmin(image)
maxval = np.nanmax(image)
# if range is good, no need to rescale
if minval < 0.0 or maxval > 1.0:
do_scale = True
# make a copy of image so as not to corrupt original data
image = image.copy()
scale_factor = maxval - minval
image -= minval
if scale_factor != 0:
image /= scale_factor
# do the warping
patch = skimage.transform.warp(image, P, output_shape=patch_shape, mode='constant', cval=np.nan)
# revert to original type if necessary
if og_dtype != patch.dtype:
if og_dtype == np.uint8:
patch = skimage.img_as_ubyte(patch)
elif og_dtype == np.bool:
patch = skimage.img_as_bool(patch)
elif og_dtype == np.uint16:
patch = skimage.img_as_uint(patch)
elif og_dtype == np.int16:
patch = skimage.img_as_int(patch)
else:
# just to straight cast, hope for the best
patch_out = np.zeros(patch.shape, og_dtype)
np.copyto(patch_out,patch)
patch = patch_out
# unscale if necessary
if do_scale:
patch *= scale_factor
patch += minval
return patch