nan

Python NaN JSON encoder

人走茶凉 提交于 2019-12-03 22:18:38
The default behavior for the JSON encoder is to convert NaNs to 'NaN', e.g. json.dumps(np.NaN) results in 'NaN'. How can I change this 'NaN' value to 'null'? I have tried to subclass the JSONEncoder and implement the default() method as follows: from json import JSONEncoder, dumps import numpy as np class NanConverter(JSONEncoder): def default(self, obj): try: _ = iter(obj) except TypeError: if isinstance(obj, float) and np.isnan(obj): return "null" return JSONEncoder.default(self, obj) >>> d = {'a': 1, 'b': 2, 'c': 3, 'e': np.nan, 'f': [1, np.nan, 3]} >>> dumps(d, cls=NanConverter) '{"a": 1,

Scikit NaN or infinity error message

两盒软妹~` 提交于 2019-12-03 21:41:18
I'm importing some data from a csv file. The file has nan values flagged with text 'NA'. I import the data with: X = genfromtxt(data, delimiter=',', dtype=float, skip_header=1) I the use this code to replace nan with a previosly calculated column mean. inds = np.where(np.isnan(X)) X[inds]=np.take(col_mean,inds[1]) I then run a couple of checks and get empty arrays: np.where(np.isnan(X)) np.where(np.isinf(X)) Finally I run a scikit classifier: RF = ensemble.RandomForestClassifier(n_estimators=100,n_jobs=-1,verbose=2) RF.fit(X, y) and get the following error: File "C:\Users\m&g\Anaconda\lib\site

Why would Microsoft want NOT to fix the wrong implementations of Equals and GetHashCode with NaN? [closed]

你离开我真会死。 提交于 2019-12-03 16:58:22
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . In the .NET Framework, the implementation ( override ) of Equals(object) and GetHashCode() for floating-point types ( System.Double

What's the difference between nan, NaN and NAN

巧了我就是萌 提交于 2019-12-03 15:54:13
问题 In numpy there are nan , NaN and NAN . What's the sense of having all three, do they differ or any of these can be used interchangeably? 回答1: >>> numpy.nan is numpy.NaN is numpy.NAN True It's just convenient. They're exactly the same. 回答2: Different operating systems and programming languages may have different string representations of NaN: nan NaN NaN% NAN NaNQ NaNS qNaN sNaN 1.#SNAN 1.#QNAN -1.#IND I think having all three is just a convenience. They are the same. >>> np.nan nan >>> np.NaN

R can't convert NaN to NA

牧云@^-^@ 提交于 2019-12-03 14:39:53
I have a data frame with several factor columns containing NaN 's that I would like to convert to NA 's (the NaN seems to be a problem for using linear regression objects to predict on new data). > tester1 <- c("2", "2", "3", "4", "2", "3", NaN) > tester1 [1] "2" "2" "3" "4" "2" "3" "NaN" > tester1[is.nan(tester1)] = NA > tester1 [1] "2" "2" "3" "4" "2" "3" "NaN" > tester1[is.nan(tester1)] = "NA" > tester1 [1] "2" "2" "3" "4" "2" "3" "NaN" Here's the problem: Your vector is character in mode, so of course it's "not a number". That last element got interpreted as the string "NaN". Using is.nan

Serialize NaN values into JSON as nulls in JSON.NET

╄→гoц情女王★ 提交于 2019-12-03 13:07:31
Most Json parsers don't serialize NaN, because in Javascript, NaN is not a constant. Json.Net, however, does serialize NaN values into NaN, which means it outputs invalid Json; attempting to deserialize this Json will fail with most parsers. (We're deserializing in WebKit.) We have hacked the Json.Net code to output null values when passed NaN, but this seems like a poor solution. Douglas Crockford (once) recommended using nulls in place of NaNs: http://www.json.org/json.ppt (Look at slide 16) Clearly this won't work in all cases, but it would be ok for our purposes. We'd just rather not have

How to merge two dataframe in pandas to replace nan

99封情书 提交于 2019-12-03 12:40:50
问题 I want to do this in pandas: I have 2 dataframes, A and B, I want to replace only NaN of A with B values. A 2014-04-17 12:59:00 146.06250 146.0625 145.93750 145.93750 2014-04-17 13:00:00 145.90625 145.9375 145.87500 145.90625 2014-04-17 13:01:00 145.90625 NaN 145.90625 NaN 2014-04-17 13:02:00 NaN NaN 145.93750 145.96875 B 2014-04-17 12:59:00 146 2/32 146 2/32 145 30/32 145 30/32 2014-04-17 13:00:00 145 29/32 145 30/32 145 28/32 145 29/32 2014-04-17 13:01:00 145 29/32 146 145 29/32 147 2014-04

Find all NaN elements inside an Array

蓝咒 提交于 2019-12-03 12:32:24
问题 Is there a command in MATLAB that allows me to find all NaN (Not-a-Number) elements inside an array? 回答1: As noted, the best answer is isnan() (though +1 for woodchips' meta-answer). A more complete example of how to use it with logical indexing: >> a = [1 nan;nan 2] a = 1 NaN NaN 2 >> %replace nan's with 0's >> a(isnan(a))=0 a = 1 0 0 2 isnan(a) returns a logical array, an array of true & false the same size as a, with "true" every place there is a nan, which can be used to index into a. 回答2

How to distinguish different types of NaN float in Python

徘徊边缘 提交于 2019-12-03 12:10:59
I'm writing Python 2.6 code that interfaces with NI TestStand 4.2 via COM in Windows. I want to make a "NAN" value for a variable, but if I pass it float('nan') , TestStand displays it as IND . Apparently TestStand distinguishes between floating point "IND" and "NAN" values. According to TestStand help : IND corresponds to Signaling NaN in Visual C++, while NAN corresponds to QuietNaN That implies that Python's float('nan') is effectively a Signaling NaN when passed through COM. However, from what I've read about Signaling NaN , it seems that Signaling NaN is a bit "exotic" and Quiet NaN is

In Scala, why is NaN not being picked up by pattern matching?

霸气de小男生 提交于 2019-12-03 10:35:31
问题 My method is as follows def myMethod(myDouble: Double): Double = myDouble match { case Double.NaN => ... case _ => ... } The IntelliJ debugger is showing NaN but this is not being picked up in my pattern matching. Are there possible cases I am omitting 回答1: It is a general rule how 64-bit floating point numbers are compared according to IEEE 754 (not Scala or even Java related, see NaN): double n1 = Double.NaN; double n2 = Double.NaN; System.out.println(n1 == n2); //false The idea is that NaN