当数组第三列的元素小于特定数量时,我想删除数组的行.例如:
a=np.array([[2331.13,1944.88,23.1379,7,3.18339,0.482105],
[8168.44,1904.70,19.5025,265,4.12642,0.0376510],
[7389.36,1983.97,14.3581,3937,6.04109,0.713416],
[1765.18,1944.29,22.5495,35,2.30717,0.794432],
[2319.33,1946.68,22.4300,25,3.63676,0.0210690],
[785.666,2090.69,14.7940,1095,2.52823,0.999842],
[4071.24,2186.92,22.6616,31,2.79309,0.0312501],
[7082.51,2191.69,23.0122,19,2.53166,0.687001]])
Run Code Online (Sandbox Code Playgroud)
我想删除满足以下条件的行:
a[:,2]<15.0
Run Code Online (Sandbox Code Playgroud)
干杯.
如何从包含n元素的列表中随机选择一个数字,n而不选择列表中的相同元素两次。我写了一个代码来选择列表中元素的序列号,但是速度很慢:
>>>redshift=np.array([0.92,0.17,0.51,1.33,....,0.41,0.82])
>>>redshift.shape
(1225,)
exclude=[]
k=0
ng=1225
while (k < ng):
flag1=0
sq=random.randint(0, ng)
while (flag1<1):
if sq in exclude:
flag1=1
sq=random.randint(0, ng)
else:
print sq
exclude.append(sq)
flag1=0
z=redshift[sq]
k+=1
Run Code Online (Sandbox Code Playgroud)
它不会选择列表中元素的所有序列号。
为了优化我的代码速度,这对我的 MCMC 的速度非常重要,我想用 cython 替换我的 python 代码的一些瓶颈。由于我正在使用一个巨大的二维数组,我需要根据二维数组的一列对数据进行分箱,然后根据第一列中的分箱在所有其他列中找到每个分箱中的平均值,我曾经使用过这个蟒蛇代码:
import numpy as np
d = np.random.random((10**5, 3))
#binning data again based on first column
bins = np.linspace(ndata[0,0], ndata[-1,0], 10)
#compute the mean in each bin for different input parameters
digitized = np.digitize(ndata[:,0], bins)
r= np.array([ndata[digitized == i,0].mean() for i in range(1, len(bins))])
p= np.array([ndata[digitized == i,1].mean() for i in range(1, len(bins))])
q= np.array([ndata[digitized == i,2].mean() for i in range(1, len(bins))])
Run Code Online (Sandbox Code Playgroud)
我怎样才能cython通过使用另一个代码而不是代码将代码加速至少两个数量级numpy.digitize?
根据我之前没有得到任何答案的问题,我试图解决我在我的情节中添加colorbar而不是图例的问题.有几个问题我还没解决. 更新:
这是我的代码:
import numpy as np
import pylab as plt
from matplotlib import rc,rcParams
rc('text',usetex=True)
rcParams.update({'font.size':10})
import matplotlib.cm as cm
from matplotlib.ticker import NullFormatter
import matplotlib as mpl
def plot(Z_s,CWL,filter_id,spectral_type,model_mag,mag,plot_name):
f= ['U38','B','V','R','I','MB420','MB464','MB485','MB518','MB571','MB604','MB646','MB696','MB753','MB815','MB856','MB914']
wavetable=CWL/(1+Z_s)
dd=model_mag-mag
nplist=['E', 'Sbc', 'Scd', 'Irr', 'SB3', 'SB2']
minimum,maximum=(0.,16.)
Z = [[0,0],[0,0]]
levels = list(np.linspace(0, 1, len(f)))
NUM_COLORS = len(f)
cm = plt.get_cmap('gist_rainbow')
mycolor=[]
for i in range(NUM_COLORS):
mycolor.append( cm(1.*i/NUM_COLORS)) # color will now be an RGBA tuple
mymap …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用给定的示例来实现这个 POMDP 求解器来解决我的决策问题,并且我按照存储库中的文档在头文件中构建了不同的相关类和函数
class SimpleState: public State {
public:
int position;
int context;
int time;
SimpleState();
SimpleState(int _position, int _context, int _time) :
rat_position(_position),
context(_context),
time(_time) {
}
SimpleState(int _state_id);
~SimpleState();
std::string text() const;
};
SimpleState::SimpleState() {
}
class StarMazeProblem : public DSPOMDP,
public MDP {
protected:
std::vector<std::vector<std::vector<State> > > transition_probabilities_; //state, action, [state, weight]
mutable MemoryPool<SimpleState> memory_pool_;
std::vector<SimpleState*> states_;
mutable std::vector<ValuedAction> mdp_policy_;
public:
enum {CENTER = 0, RIGHT = 1, LEFT = 2};
public:
StarMazeProblem(); …Run Code Online (Sandbox Code Playgroud)