本文整理汇总了Python中math.nan方法的典型用法代码示例。如果您正苦于以下问题:Python math.nan方法的具体用法?Python math.nan怎么用?Python math.nan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math
的用法示例。
在下文中一共展示了math.nan方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_pattern
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def get_pattern(data):
if len(data) > 0:
mean_value = np.mean(data) * 0.7
else:
mean_value = math.nan
if math.isnan(mean_value):
return PatternAnalyser.UNKNOWN_PATTERN
indexes_under_mean_value = np.where(data > mean_value)[0] \
if mean_value < 0 \
else np.where(data < mean_value)[0]
nb_gaps = 0
for i in range(len(indexes_under_mean_value)-1):
if indexes_under_mean_value[i+1]-indexes_under_mean_value[i] > 3:
nb_gaps += 1
if nb_gaps > 1:
return "W" if mean_value < 0 else "M"
else:
return "V" if mean_value < 0 else "N"
# returns a value 0 < value < 1: the higher the stronger is the pattern
示例2: doc_f1
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def doc_f1(cls, d_list):
def single_f1(item):
docid_predicted = item['predicted_docids']
docid_predicted = set(docid_predicted)
docid_gt = [iii for i in item['evidence'] \
for ii in i \
for iii in ii \
if type(iii) == str]
docid_gt = set(docid_gt)
docid_intersect = docid_predicted & docid_gt
if len(docid_gt) == 0:
return math.nan
f1 = 2*len(docid_intersect) / (len(docid_gt) + len(docid_predicted))
return f1
score_list = map(single_f1, d_list)
score_list = [s for s in score_list if not math.isnan(s)]
return sum(score_list) / len(score_list)
示例3: test_seldom
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def test_seldom(self) -> None:
with self.assertRaisesRegex(ValueError,
r"math domain error"):
HedgeAssert(self, fl.Seldom()).evaluates({-1.0: nan})
HedgeAssert(self, fl.Seldom()).evaluates({-0.5: nan})
HedgeAssert(self, fl.Seldom()).evaluates({inf: nan})
HedgeAssert(self, fl.Seldom()).evaluates({-inf: nan})
HedgeAssert(self, fl.Seldom()) \
.has_name("seldom") \
.evaluates({0.00: 0.0,
0.25: 0.3535533905932738,
0.50: 0.5,
0.75: 0.6464466094067263,
1.00: 1.0,
nan: nan})
示例4: test_somewhat
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def test_somewhat(self) -> None:
with self.assertRaisesRegex(ValueError,
r"math domain error"):
HedgeAssert(self, fl.Somewhat()).evaluates({-1.0: nan})
HedgeAssert(self, fl.Somewhat()).evaluates({-0.5: nan})
HedgeAssert(self, fl.Somewhat()).evaluates({-inf: nan})
HedgeAssert(self, fl.Somewhat()) \
.has_name("somewhat") \
.evaluates({0.00: 0.0,
0.25: 0.5,
0.50: 0.7071067811865476,
0.75: 0.8660254037844386,
1.00: 1.0,
inf: inf,
nan: nan})
示例5: test_lambda
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def test_lambda(self) -> None:
HedgeAssert(self, fl.HedgeLambda("my_hedge", lambda x: (
2.0 * x * x if x <= 0.5 else (1.0 - 2.0 * (1.0 - x) * (1.0 - x))))) \
.has_name("my_hedge") \
.evaluates({-1.0: 2.0,
-0.5: 0.5,
0.00: 0.0,
0.25: 0.125,
0.50: 0.5,
0.75: 0.875,
1.00: 1.0,
inf: -inf,
-inf: inf,
nan: nan})
if __name__ == '__main__':
unittest.main()
示例6: test_fuzzify
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def test_fuzzify(self) -> None:
VariableAssert(self, fl.Variable(name="name",
description="description",
minimum=-1.0,
maximum=1.0,
terms=[
fl.Triangle('Low', -1.0, -1.0, 0.0),
fl.Triangle('Medium', -0.5, 0.0, 0.5),
fl.Triangle('High', 0.0, 1.0, 1.0)
])) \
.fuzzy_values(
{-1.00: "1.000/Low + 0.000/Medium + 0.000/High",
-0.50: "0.500/Low + 0.000/Medium + 0.000/High",
-0.25: "0.250/Low + 0.500/Medium + 0.000/High",
0.00: "0.000/Low + 1.000/Medium + 0.000/High",
0.25: "0.000/Low + 0.500/Medium + 0.250/High",
0.50: "0.000/Low + 0.000/Medium + 0.500/High",
0.75: "0.000/Low + 0.000/Medium + 0.750/High",
1.00: "0.000/Low + 0.000/Medium + 1.000/High",
math.nan: "nan/Low + nan/Medium + nan/High",
math.inf: "0.000/Low + 0.000/Medium + 0.000/High",
-math.inf: "0.000/Low + 0.000/Medium + 0.000/High",
})
示例7: test_highest_membership
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def test_highest_membership(self) -> None:
low, medium, high = (fl.Triangle('Low', -1.0, -.5, 0.0),
fl.Triangle('Medium', -0.5, 0.0, 0.5),
fl.Triangle('High', 0.0, .5, 1.0))
VariableAssert(self, fl.Variable(name="name",
description="description",
minimum=-1.0,
maximum=1.0,
terms=[low, medium, high])) \
.highest_memberships(
{-1.00: (0.0, None),
-0.75: (0.5, low),
-0.50: (1.0, low),
-0.25: (0.5, low),
0.00: (1.0, medium),
0.25: (0.5, medium),
0.50: (1.0, high),
0.75: (0.5, high),
1.00: (0.0, None),
math.nan: (0.0, None),
math.inf: (0.0, None),
-math.inf: (0.0, None),
})
示例8: test_fuzzy_value
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def test_fuzzy_value(self) -> None:
InputVariableAssert(self, fl.InputVariable(name="name",
description="description",
minimum=-1.0,
maximum=1.0,
terms=[
fl.Triangle('Low', -1.0, -1.0, 0.0),
fl.Triangle('Medium', -0.5, 0.0, 0.5),
fl.Triangle('High', 0.0, 1.0, 1.0)
])) \
.fuzzy_values({-1.00: "1.000/Low + 0.000/Medium + 0.000/High",
-0.50: "0.500/Low + 0.000/Medium + 0.000/High",
-0.25: "0.250/Low + 0.500/Medium + 0.000/High",
0.00: "0.000/Low + 1.000/Medium + 0.000/High",
0.25: "0.000/Low + 0.500/Medium + 0.250/High",
0.50: "0.000/Low + 0.000/Medium + 0.500/High",
0.75: "0.000/Low + 0.000/Medium + 0.750/High",
1.00: "0.000/Low + 0.000/Medium + 1.000/High",
math.nan: "nan/Low + nan/Medium + nan/High",
math.inf: "0.000/Low + 0.000/Medium + 0.000/High",
-math.inf: "0.000/Low + 0.000/Medium + 0.000/High",
})
示例9: test_division_by_zero_fails_with_float
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def test_division_by_zero_fails_with_float(self) -> None:
self.assertEqual(fl.lib.floating_point_type, float)
TermAssert(self, fl.Function.create("dbz", "0.0/x")) \
.membership_fails(0.0, ZeroDivisionError, re.escape("float division by zero")) \
.has_memberships({fl.inf: 0.0, -fl.inf: -0.0, fl.nan: fl.nan})
TermAssert(self, fl.Function.create("dbz", "inf/x")) \
.membership_fails(0.0, ZeroDivisionError, re.escape("float division by zero")) \
.has_memberships({fl.inf: fl.nan, -fl.inf: fl.nan, fl.nan: fl.nan})
TermAssert(self, fl.Function.create("dbz", ".-inf/x")) \
.membership_fails(0.0, ZeroDivisionError, re.escape("float division by zero")) \
.has_memberships({fl.inf: fl.nan, -fl.inf: fl.nan, -fl.nan: fl.nan})
TermAssert(self, fl.Function.create("dbz", "nan/x")) \
.membership_fails(0.0, ZeroDivisionError, re.escape("float division by zero")) \
.has_memberships({fl.inf: fl.nan, -fl.inf: fl.nan, -fl.nan: fl.nan})
示例10: test_str
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def test_str(self) -> None:
fl.lib.decimals = 3
self.assertEqual(fl.Op.str(0.3), "0.300")
self.assertEqual(fl.Op.str(-0.3), "-0.300")
self.assertEqual(fl.Op.str(3), "3")
self.assertEqual(fl.Op.str(3.0001), "3.000")
self.assertEqual(fl.Op.str(math.inf), "inf")
self.assertEqual(fl.Op.str(-math.inf), "-inf")
self.assertEqual(fl.Op.str(math.nan), "nan")
fl.lib.decimals = 5
self.assertEqual(fl.Op.str(0.3), "0.30000")
fl.lib.decimals = 0
self.assertEqual(fl.Op.str(0.3), "0")
fl.lib.decimals = 3
示例11: defuzzify
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def defuzzify(self, term: Term, minimum: float, maximum: float) -> float:
if not math.isfinite(minimum + maximum):
return nan
resolution = self.resolution
dx = (maximum - minimum) / resolution
counter = resolution
left = right = 0
x_left, x_right = (minimum, maximum)
left_area = right_area = 0.0
# TODO: Improve?
while counter > 0:
counter = counter - 1
if left_area <= right_area:
x_left = minimum + (left + 0.5) * dx
left_area += term.membership(x_left)
left += 1
else:
x_right = maximum - (right + 0.5) * dx
right_area += term.membership(x_right)
right += 1
# Inverse weighted average to compensate
return (left_area * x_right + right_area * x_left) / (left_area + right_area)
示例12: membership
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def membership(self, x: float) -> float:
if isnan(x):
return nan
if self.start == self.end:
return self.height * 0.0
if self.start < self.end:
if x <= self.start:
return self.height * 0.0
if x >= self.end:
return self.height * 1.0
return self.height * (x - self.start) / (self.end - self.start)
else:
if x >= self.start:
return self.height * 0.0
if x <= self.end:
return self.height * 1.0
return self.height * (self.start - x) / (self.start - self.end)
示例13: apply_mask
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def apply_mask(np_array, mask, invalid_value=math.nan):
np_array[ mask < 1 ] = invalid_value
return np_array
示例14: _predict_contexts
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def _predict_contexts(self, contexts: np.ndarray, is_predict: bool,
seeds: Optional[np.ndarray] = None, start_index: Optional[int] = None) -> List:
# Copy learning policy object
lp = deepcopy(self.lp)
# Create an empty list of predictions
predictions = [None] * len(contexts)
# For each row in the given contexts
for index, row in enumerate(contexts):
# Get random generator
lp.rng = create_rng(seeds[index])
# Calculate the distances from the historical contexts
# Row is 1D so convert it to 2D array for cdist using newaxis
# Finally, reshape to flatten the output distances list
row_2d = row[np.newaxis, :]
distances_to_row = self.distances[start_index + index]
# Find the neighbor indices within the radius
# np.where with a condition returns a tuple where the first element is an array of indices
indices = np.where(distances_to_row <= self.radius)
# If neighbors exist
if indices[0].size > 0:
prediction, exp, stats = self._get_nhood_predictions(lp, row_2d, indices, is_predict)
predictions[index] = [prediction, exp, len(indices[0]), stats]
else: # When there are no neighbors
# Random arm (or nan expectations)
prediction = self._get_no_nhood_predictions(lp, is_predict)
predictions[index] = [prediction, {}, 0, {}]
# Return the list of predictions
return predictions
示例15: _get_no_nhood_predictions
# 需要导入模块: import math [as 别名]
# 或者: from math import nan [as 别名]
def _get_no_nhood_predictions(self, lp, is_predict):
if is_predict:
# if no_nhood_prob_of_arm is None, select a random int
# else, select a non-uniform random arm
# choice returns an array, hence get zero index
rand_int = lp.rng.choice(len(self.arms), 1, p=self.no_nhood_prob_of_arm)[0]
return self.arms[rand_int]
else:
# Expectations will be nan when there are no neighbors
return self.arm_to_expectation.copy()