本文整理汇总了Python中numpy.interp方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.interp方法的具体用法?Python numpy.interp怎么用?Python numpy.interp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.interp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def __init__(self,alpha_max,Tg,xi):
gamma=0.9+(0.05-xi)/(0.3+6*xi)
eta1=0.02+(0.05-xi)/(4+32*xi)
eta1=eta1 if eta1>0 else 0
eta2=1+(0.05-xi)/(0.08+1.6*xi)
eta2=eta2 if eta2>0.55 else 0.55
T=np.linspace(0,6,601)
alpha=[]
for t in T:
if t<0.1:
alpha.append(np.interp(t,[0,0.1],[0.45*alpha_max,eta2*alpha_max]))
elif t<Tg:
alpha.append(eta2*alpha_max)
elif t<5*Tg:
alpha.append((Tg/t)**gamma*eta2*alpha_max)
else:
alpha.append((eta2*0.2**gamma-eta1*(t-5*Tg))*alpha_max)
self.__spectrum={'T':T,'alpha':alpha}
示例2: spectrum_analysis
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def spectrum_analysis(model,n,spec):
"""
sepctrum analysis
params:
n: number of modes to use\n
spec: a list of tuples (period,acceleration response)
"""
freq,mode=eigen_mode(model,n)
M_=np.dot(mode.T,model.M)
M_=np.dot(M_,mode)
K_=np.dot(mode.T,model.K)
K_=np.dot(K_,mode)
C_=np.dot(mode.T,model.C)
C_=np.dot(C_,mode)
d_=[]
for (m_,k_,c_) in zip(M_.diag(),K_.diag(),C_.diag()):
sdof=SDOFSystem(m_,k_)
T=sdof.omega_d()
d_.append(np.interp(T,spec[0],spec[1]*m_))
d=np.dot(d_,mode)
#CQC
return d
示例3: nan_helper
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def nan_helper(y):
"""Helper to handle indices and logical indices of NaNs.
Input:
- y, 1d numpy array with possible NaNs
Output:
- nans, logical indices of NaNs
- index, a function, with signature indices= index(logical_indices),
to convert logical indices of NaNs to 'equivalent' indices
Example:
>>> # linear interpolation of NaNs
>>> nans, x= nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
"""
return np.isnan(y), lambda z: z.nonzero()[0]
示例4: test_control_curve_interpolated_json
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def test_control_curve_interpolated_json(use_parameters):
# this is a little hack-y, as the parameters don't provide access to their
# data once they've been initalised
if use_parameters:
model = load_model("reservoir_with_cc_param_values.json")
else:
model = load_model("reservoir_with_cc.json")
reservoir1 = model.nodes["reservoir1"]
model.setup()
path = os.path.join(os.path.dirname(__file__), "models", "control_curve.csv")
control_curve = pd.read_csv(path)["Control Curve"].values
values = [-8, -6, -4]
@assert_rec(model, reservoir1.cost)
def expected_cost(timestep, si):
# calculate expected cost manually and compare to parameter output
volume_factor = reservoir1._current_pc[si.global_id]
cc = control_curve[timestep.index]
return np.interp(volume_factor, [0.0, cc, 1.0], values[::-1])
model.run()
示例5: test_circular_control_curve_interpolated_json
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def test_circular_control_curve_interpolated_json():
# this is a little hack-y, as the parameters don't provide access to their
# data once they've been initalised
model = load_model("reservoir_with_circular_cc.json")
reservoir1 = model.nodes["reservoir1"]
model.setup()
path = os.path.join(os.path.dirname(__file__), "models", "control_curve.csv")
control_curve = pd.read_csv(path)["Control Curve"].values
values = [-8, -6, -4]
@assert_rec(model, reservoir1.cost)
def expected_cost(timestep, si):
# calculate expected cost manually and compare to parameter output
volume_factor = reservoir1._current_pc[si.global_id]
cc = control_curve[timestep.index]
return np.interp(volume_factor, [0.0, cc, 1.0], values[::-1])
model.run()
示例6: colormap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def colormap(x, m=None, M=None, center=0, colors=None):
'''color a grayscale array (currently red/blue by sign)'''
if center is None:
center = 0
if colors is None:
colors = np.array(((0, 0.7, 1),
(0, 0, 0),
(1, 0, 0)),
dtype=float)
if x.shape[-1] == 1:
x = x[..., 0]
x = scale_values(x, min=m, max=M, center=center)
y = np.empty(x.shape + (3,))
for c in xrange(3):
y[..., c] = np.interp(x, (0, 0.5, 1), colors[:, c])
return y
示例7: cor_2_1d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def cor_2_1d(cor, H, W):
bon_ceil_x, bon_ceil_y = [], []
bon_floor_x, bon_floor_y = [], []
n_cor = len(cor)
for i in range(n_cor // 2):
xys = panostretch.pano_connect_points(cor[i*2],
cor[(i*2+2) % n_cor],
z=-50, w=W, h=H)
bon_ceil_x.extend(xys[:, 0])
bon_ceil_y.extend(xys[:, 1])
for i in range(n_cor // 2):
xys = panostretch.pano_connect_points(cor[i*2+1],
cor[(i*2+3) % n_cor],
z=50, w=W, h=H)
bon_floor_x.extend(xys[:, 0])
bon_floor_y.extend(xys[:, 1])
bon_ceil_x, bon_ceil_y = sort_xy_filter_unique(bon_ceil_x, bon_ceil_y, y_small_first=True)
bon_floor_x, bon_floor_y = sort_xy_filter_unique(bon_floor_x, bon_floor_y, y_small_first=False)
bon = np.zeros((2, W))
bon[0] = np.interp(np.arange(W), bon_ceil_x, bon_ceil_y, period=W)
bon[1] = np.interp(np.arange(W), bon_floor_x, bon_floor_y, period=W)
bon = ((bon + 0.5) / H - 0.5) * np.pi
return bon
示例8: test_zero_dimensional_interpolation_point
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def test_zero_dimensional_interpolation_point(self):
x = np.linspace(0, 1, 5)
y = np.linspace(0, 1, 5)
x0 = np.array(.3)
assert_almost_equal(np.interp(x0, x, y), x0)
xp = np.array([0, 2, 4])
fp = np.array([1, -1, 1])
actual = np.interp(np.array(1), xp, fp)
assert_equal(actual, 0)
assert_(isinstance(actual, np.float64))
actual = np.interp(np.array(4.5), xp, fp, period=4)
assert_equal(actual, 0.5)
assert_(isinstance(actual, np.float64))
示例9: test_interpolate_index_values
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def test_interpolate_index_values(self):
s = Series(np.nan, index=np.sort(np.random.rand(30)))
s[::3] = np.random.randn(10)
vals = s.index.values.astype(float)
result = s.interpolate(method='index')
expected = s.copy()
bad = isna(expected.values)
good = ~bad
expected = Series(np.interp(vals[bad], vals[good],
s.values[good]),
index=s.index[bad])
assert_series_equal(result[bad], expected)
# 'values' is synonymous with 'index' for the method kwarg
other_result = s.interpolate(method='values')
assert_series_equal(other_result, result)
assert_series_equal(other_result[bad], expected)
示例10: sample_posterior
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def sample_posterior(self, x, n=1):
r"""
Generates :code:`n` samples from the estimated posterior
distribution for the input vector :code:`x`. The sampling
is performed by the inverse CDF method using the estimated
CDF obtained from the :code:`cdf` member function.
Arguments:
x(np.array): Array of shape `(n, m)` containing `n` inputs for which
to predict the conditional quantiles.
n(int): The number of samples to generate.
Returns:
Tuple (xs, fs) containing the :math: `x`-values in `xs` and corresponding
values of the posterior CDF :math: `F(x)` in `fs`.
"""
y_pred, qs = self.cdf(x)
p = np.random.rand(n)
y = np.interp(p, qs, y_pred)
return y
示例11: interp_batch
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def interp_batch(total_batch_x):
interp_batch_x = total_batch_x.copy()
N_batch = total_batch_x.shape[0]
for n in range(N_batch):
temp_idx = np.where(total_batch_x[n,0,:,1]==1)[0]
t1 = int(temp_idx[-1])
temp_idx = np.where(total_batch_x[n,0,:,2]==1)[0]
t2 = int(temp_idx[0])
if t2-t1<=1:
continue
interp_t = np.array(range(t1+1,t2))
for k in range(total_batch_x.shape[1]):
#temp_std = np.std(total_batch_x[n,k,total_batch_x[n,k,:,0]!=0,0])
temp_std1 = np.std(total_batch_x[n,k,total_batch_x[n,0,:,1]!=0,0])
temp_std2 = np.std(total_batch_x[n,k,total_batch_x[n,0,:,2]!=0,0])
x_p = [t1,t2]
f_p = [total_batch_x[n,k,t1,0],total_batch_x[n,k,t2,0]]
#interp_batch_x[n,k,t1+1:t2,0] = np.interp(interp_t,x_p,f_p)#+np.random.normal(0, temp_std, t2-t1-1)
interp_batch_x[n,k,t1+1:t2,0] = np.interp(interp_t,x_p,f_p)+np.random.normal(0, (temp_std1+temp_std2)*0.5, t2-t1-1)
return interp_batch_x
示例12: interp_batch
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def interp_batch(total_batch_x):
interp_batch_x = total_batch_x.copy()
N_batch = total_batch_x.shape[0]
for n in range(N_batch):
temp_idx = np.where(total_batch_x[n,0,:,1]==1)[0]
t1 = int(temp_idx[-1])
temp_idx = np.where(total_batch_x[n,0,:,2]==1)[0]
t2 = int(temp_idx[0])
if t2-t1<=1:
continue
interp_t = np.array(range(t1+1,t2))
for k in range(total_batch_x.shape[1]):
#temp_std = np.std(total_batch_x[n,k,total_batch_x[n,k,:,0]!=0,0])
temp_std1 = np.std(total_batch_x[n,k,total_batch_x[n,0,:,1]!=0,0])
temp_std2 = np.std(total_batch_x[n,k,total_batch_x[n,0,:,2]!=0,0])
x_p = [t1,t2]
f_p = [total_batch_x[n,k,t1,0],total_batch_x[n,k,t2,0]]
#*************************************
#interp_batch_x[n,k,t1+1:t2,0] = np.interp(interp_t,x_p,f_p)+np.random.normal(0, temp_std, t2-t1-1)
#*************************************
interp_batch_x[n,k,t1+1:t2,0] = np.interp(interp_t,x_p,f_p)+np.random.normal(0, (temp_std1+temp_std2)*0.5, t2-t1-1)
return interp_batch_x
示例13: test_complex_interp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def test_complex_interp(self):
# test complex interpolation
x = np.linspace(0, 1, 5)
y = np.linspace(0, 1, 5) + (1 + np.linspace(0, 1, 5))*1.0j
x0 = 0.3
y0 = x0 + (1+x0)*1.0j
assert_almost_equal(np.interp(x0, x, y), y0)
# test complex left and right
x0 = -1
left = 2 + 3.0j
assert_almost_equal(np.interp(x0, x, y, left=left), left)
x0 = 2.0
right = 2 + 3.0j
assert_almost_equal(np.interp(x0, x, y, right=right), right)
# test complex periodic
x = [-180, -170, -185, 185, -10, -5, 0, 365]
xp = [190, -190, 350, -350]
fp = [5+1.0j, 10+2j, 3+3j, 4+4j]
y = [7.5+1.5j, 5.+1.0j, 8.75+1.75j, 6.25+1.25j, 3.+3j, 3.25+3.25j,
3.5+3.5j, 3.75+3.75j]
assert_almost_equal(np.interp(x, xp, fp, period=360), y)
示例14: linear_interpolation
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def linear_interpolation(x, xp, fp, **kwargs):
"""Multi-dimensional linear interpolation.
Returns the multi-dimensional piecewise linear interpolant to a function with
given discrete data points (xp, fp), evaluated at x.
Note that *N and *M indicate zero or more dimensions.
Args:
x: An array of shape [*N], the x-coordinates of the interpolated values.
xp: An np.array of shape [D], the x-coordinates of the data points, must be
increasing.
fp: An np.array of shape [D, *M], the y-coordinates of the data points.
**kwargs: Keywords for np.interp.
Returns:
An array of shape [*N, *M], the interpolated values.
"""
yp = fp.reshape([fp.shape[0], -1]).transpose()
y = np.stack([np.interp(x, xp, zp, **kwargs) for zp in yp]).transpose()
return y.reshape(x.shape[:1] + fp.shape[1:]).astype(np.float32)
示例15: _convert_to_torque_from_pwm
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import interp [as 别名]
def _convert_to_torque_from_pwm(self, pwm, true_motor_velocity):
"""Convert the pwm signal to torque.
Args:
pwm: The pulse width modulation.
true_motor_velocity: The true motor velocity at the current moment. It is
used to compute the back EMF voltage and the viscous damping.
Returns:
actual_torque: The torque that needs to be applied to the motor.
observed_torque: The torque observed by the sensor.
"""
observed_torque = np.clip(
self._torque_constant *
(np.asarray(pwm) * self._voltage / self._resistance),
-OBSERVED_TORQUE_LIMIT, OBSERVED_TORQUE_LIMIT)
# Net voltage is clipped at 50V by diodes on the motor controller.
voltage_net = np.clip(
np.asarray(pwm) * self._voltage -
(self._torque_constant + self._viscous_damping) *
np.asarray(true_motor_velocity), -VOLTAGE_CLIPPING, VOLTAGE_CLIPPING)
current = voltage_net / self._resistance
current_sign = np.sign(current)
current_magnitude = np.absolute(current)
# Saturate torque based on empirical current relation.
actual_torque = np.interp(current_magnitude, self._current_table,
self._torque_table)
actual_torque = np.multiply(current_sign, actual_torque)
actual_torque = np.multiply(self._strength_ratios, actual_torque)
return actual_torque, observed_torque