pythonisnan_Python numpy.isnan() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def rhoA(self):

# rhoA

rhoA = pd.DataFrame(0, index=np.arange(1), columns=self.latent)

for i in range(self.lenlatent):

weights = pd.DataFrame(self.outer_weights[self.latent[i]])

weights = weights[(weights.T != 0).any()]

result = pd.DataFrame.dot(weights.T, weights)

result_ = pd.DataFrame.dot(weights, weights.T)

S = self.data_[self.Variables['measurement'][

self.Variables['latent'] == self.latent[i]]]

S = pd.DataFrame.dot(S.T, S) / S.shape[0]

numerador = (

np.dot(np.dot(weights.T, (S - np.diag(np.diag(S)))), weights))

denominador = (

(np.dot(np.dot(weights.T, (result_ - np.diag(np.diag(result_)))), weights)))

rhoA_ = ((result)**2) * (numerador / denominador)

if(np.isnan(rhoA_.values)):

rhoA[self.latent[i]] = 1

else:

rhoA[self.latent[i]] = rhoA_.values

return rhoA.T

Example 2

def SMA(Series, N, M=1):

ret = []

i = 1

length = len(Series)

# ??X????? nan ?

while i < length:

if np.isnan(Series[i]):

i += 1

else:

break

preY = Series[i] # Y'

ret.append(preY)

while i < length:

Y = (M * Series[i] + (N - M) * preY) / float(N)

ret.append(Y)

preY = Y

i += 1

return pd.Series(ret)

Example 3

def replace_missing(X):

# This is ugly, but

try:

if X.getformat()=='csr':

return X

except:

X[np.isnan(X)]=-999.0 #djajetic 05.09.2015

return X #djajetic 05.09.2015

p=len(X)

nn=len(X[0])*2

XX = np.zeros([p,nn])

for i in range(len(X)):

line = X[i]

line1 = [0 if np.isnan(x) else x for x in line]

line2 = [1 if np.isnan(x) else 0 for x in line] # indicator of missingness

XX[i] = line1 + line2

return XX

Example 4

def get(self, X):

X = np.array(X)

X_nan = np.isnan(X)

imputed = self.meanImput(X.copy())

if len(self.estimators_) > 1:

for i, estimator_ in enumerate(self.estimators_):

X_s = np.delete(imputed, i, 1)

y_nan = X_nan[:, i]

X_unk = X_s[y_nan]

result_ = []

if len(X_unk) > 0:

for unk in X_unk:

result_.append(estimator_.predict(unk))

X[y_nan, i] = result_

return X

Example 5

def treegauss_remove_row(

data_row,

tree_grid,

latent_row,

vert_ss,

edge_ss,

feat_ss, ):

# Update sufficient statistics.

for v in range(latent_row.shape[0]):

z = latent_row[v, :]

vert_ss[v, :, :] -= np.outer(z, z)

for e in range(tree_grid.shape[1]):

z1 = latent_row[tree_grid[1, e], :]

z2 = latent_row[tree_grid[2, e], :]

edge_ss[e, :, :] -= np.outer(z1, z2)

for v, x in enumerate(data_row):

if np.isnan(x):

continue

z = latent_row[v, :]

feat_ss[v] -= 1

feat_ss[v, 1] -= x

feat_ss[v, 2:] -= x * z # TODO Use central covariance.

Example 6

def test_train(self):

model, fetches_ = self._test_pipeline(tf.contrib.learn.ModeKeys.TRAIN)

predictions_, loss_, _ = fetches_

target_len = self.sequence_length + 10 + 2

max_decode_length = model.params["target.max_seq_len"]

expected_decode_len = np.minimum(target_len, max_decode_length)

np.testing.assert_array_equal(predictions_["logits"].shape, [

self.batch_size, expected_decode_len - 1,

model.target_vocab_info.total_size

])

np.testing.assert_array_equal(predictions_["losses"].shape,

[self.batch_size, expected_decode_len - 1])

np.testing.assert_array_equal(predictions_["predicted_ids"].shape,

[self.batch_size, expected_decode_len - 1])

self.assertFalse(np.isnan(loss_))

Example 7

def information_ratio(algorithm_returns, benchmark_returns):

"""

http://en.wikipedia.org/wiki/Information_ratio

Args:

algorithm_returns (np.array-like):

All returns during algorithm lifetime.

benchmark_returns (np.array-like):

All benchmark returns during algo lifetime.

Returns:

float. Information ratio.

"""

relative_returns = algorithm_returns - benchmark_returns

relative_deviation = relative_returns.std(ddof=1)

if zp_math.tolerant_equals(relative_deviation, 0) or \

np.isnan(relative_deviation):

return 0.0

return np.mean(relative_returns) / relative_deviation

Example 8

def raw_data_gen(self):

for dt, series in self.data.iterrows():

for sid, price in series.iteritems():

# Skip SIDs that can not be forward filled

if np.isnan(price) and \

sid not in self.started_sids:

continue

self.started_sids.add(sid)

event = {

'dt': dt,

'sid': sid,

'price': price,

# Just chose something large

# if no volume available.

'volume': 1e9,

}

yield event

Example 9

def test_nan_filter_dataframe(self):

dates = pd.date_range('1/1/2000', periods=2, freq='B', tz='UTC')

df = pd.DataFrame(np.random.randn(2, 2),

index=dates,

columns=[4, 5])

# should be filtered

df.loc[dates[0], 4] = np.nan

# should not be filtered, should have been ffilled

df.loc[dates[1], 5] = np.nan

source = DataFrameSource(df)

event = next(source)

self.assertEqual(5, event.sid)

event = next(source)

self.assertEqual(4, event.sid)

event = next(source)

self.assertEqual(5, event.sid)

self.assertFalse(np.isnan(event.price))

Example 10

def df_type_to_str(i):

'''

Convert into simple datatypes from pandas/numpy types

'''

if isinstance(i, np.bool_):

return bool(i)

if isinstance(i, np.int_):

return int(i)

if isinstance(i, np.float):

if np.isnan(i):

return 'NaN'

elif np.isinf(i):

return str(i)

return float(i)

if isinstance(i, np.uint):

return int(i)

if type(i) == bytes:

return i.decode('UTF-8')

if isinstance(i, (tuple, list)):

return str(i)

if i is pd.NaT: # not identified as a float null

return 'NaN'

return str(i)

Example 11

def calc_reward(self, action=0, state=None, **kw ):

"""Calculate the reward for the specified transition."""

eps1, eps2 = self.eps_values_for_actions[action]

if state is None:

state = self.observe()

if self.logspace:

T1, T2, T1s, T2s, V, E = 10**state

else:

T1, T2, T1s, T2s, V, E = state

# the reward function penalizes treatment because of side-effects

reward = -0.1*V - 2e4*eps1**2 - 2e3*eps2**2 + 1e3*E

# Constrain reward to be within specified range

if np.isnan(reward):

reward = -self.reward_bound

elif reward > self.reward_bound:

reward = self.reward_bound

elif reward < -self.reward_bound:

reward = -self.reward_bound

return reward

Example 12

def to_rgb(img):

"""

Converts the given array into a RGB image. If the number of channels is not

3 the array is tiled such that it has 3 channels. Finally, the values are

rescaled to [0,255)

:param img: the array to convert [nx, ny, channels]

:returns img: the rgb image [nx, ny, 3]

"""

img = np.atleast_3d(img)

channels = img.shape[2]

if channels < 3:

img = np.tile(img, 3)

img[np.isnan(img)] = 0

img -= np.amin(img)

img /= np.amax(img)

img *= 255

return img

Example 13

def map(self, data):

data = data[self.fieldName]

colors = np.empty((len(data), 4))

default = np.array(fn.colorTuple(self['Default'])) / 255.

colors[:] = default

for v in self.param('Values'):

mask = data == v.maskValue

c = np.array(fn.colorTuple(v.value())) / 255.

colors[mask] = c

#scaled = np.clip((data-self['Min']) / (self['Max']-self['Min']), 0, 1)

#cmap = self.value()

#colors = cmap.map(scaled, mode='float')

#mask = np.isnan(data) | np.isinf(data)

#nanColor = self['NaN']

#nanColor = (nanColor.red()/255., nanColor.gree

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值