我需要从群体中获得k大小的样本而无需替换,群体中的每个成员具有相关的权重(W).
Numpy的random.choices不会在没有替换的情况下执行此任务,random.sample将不会采用加权输入.
目前,这是我正在使用的:
P = np.zeros((1,Parent_number))
n=0
while n < Parent_number:
draw = random.choices(population,weights=W,k=1)
if draw not in P:
P[0,n] = draw[0]
n=n+1
P=np.asarray(sorted(P[0]))
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但它需要在数组,列表和数组之间来回切换,因此不太理想.
我正在寻找最简单易懂的解决方案,因为此代码将与其他人共享.
我有一组 PDF,需要为 PDF 域的某个部分绘制。但是,当我在 3d 绘图上绘制线条时,每个 PDF 都会有尾部,
有没有一种干净的方法来不绘制发生在我的情节限制之外的尾巴?我知道我可以将数据更改为 NaN 以达到相同的效果,但我想在 matplotlib 中执行此操作。这是我当前的解决方法代码,
`# trim the data
y = np.ones(PDF_x.shape)*PDF_x
y[y>95]= np.nan
y[y<75]= np.nan
# plot the data
fig = plt.figure()
ax = fig.gca(projection='3d')
for i in range(PDF_capacity.shape[1]):
ax.plot(life[i]*np.ones((PDF_x.shape)),y,PDF_capacity[:,i], label='parametric curve')
# set the axis limits
ax.set_ylim(75,95)
# add axis labels
ax.set_xlabel('charge cycles to failure point of 75% capacity')
ax.set_ylabel('capacity at 100 charge cycles')
ax.set_zlabel('probability')`
Run Code Online (Sandbox Code Playgroud)
修剪后,我可以制作以下情节,
使用 Latex 时如何更改 matplotlib 中的数学字体样式。例如,我有
matplotlib_font$_{latex_font}$
Run Code Online (Sandbox Code Playgroud)
其中 matplotlib_font 显示为我的 rc 文件中设置的字体,而 Latex_font 在计算机现代中。我不希望我所有的字体都脱离 Latex,即。我想将 rc 文件保留为
text.usetex : False
Run Code Online (Sandbox Code Playgroud)
但我需要我的乳胶字体来匹配我的 matplotlib 字体。
我有一个数据集,我试图适应参数(a,b,c,d),它们在真实拟合参数的+/- 5%范围内.但是,当我使用scipy.optimize.curve_fit执行此操作时,我get the error "ValueError: 'x0' is infeasible."在最小二乘包内.
如果我放松我的界限,那么optimize.curve_fit似乎可以正常工作.我也注意到我的参数更大似乎在应用边界时更灵活(即我可以使用更严格的约束来工作,但绝不会低于20%).以下代码是一个MWE,有两组边界(变量B),一个有效,另一个返回错误.
# %% import modules
import IPython as IP
IP.get_ipython().magic('reset -sf')
import matplotlib.pyplot as plt
import os as os
import numpy as np
import scipy as sp
import scipy.io as sio
plt.close('all')
#%% Load the data
capacity = np.array([1.0,9.896265560165975472e-01,9.854771784232364551e-01,9.823651452282157193e-01,9.797717842323651061e-01,9.776970954356846155e-01,9.751037344398340023e-01,9.735477178423235234e-01,9.714730290456431439e-01,9.699170124481327759e-01,9.683609958506222970e-01,9.668049792531120401e-01,9.652489626556015612e-01,9.636929460580913043e-01,9.621369294605808253e-01,9.610995850622406911e-01,9.595435684647302121e-01,9.585062240663900779e-01,9.574688796680497216e-01,9.559128630705394647e-01,9.548755186721991084e-01,9.538381742738588631e-01,9.528008298755185068e-01,9.517634854771783726e-01,9.507261410788381273e-01,9.496887966804978820e-01,9.486514522821576367e-01,9.476141078838172804e-01,9.460580912863070235e-01,9.450207468879666672e-01,9.439834024896265330e-01,9.429460580912862877e-01,9.419087136929459314e-01,9.408713692946057972e-01,9.393153526970953182e-01,9.382780082987551840e-01,9.372406639004148277e-01,9.356846473029045708e-01,9.346473029045642145e-01,9.330912863070539576e-01,9.320539419087136013e-01,9.304979253112033444e-01,9.289419087136928654e-01,9.273858921161826085e-01,9.258298755186721296e-01,9.242738589211617617e-01,9.227178423236513938e-01,9.211618257261410259e-01,9.196058091286306579e-01,9.180497925311202900e-01,9.159751037344397995e-01,9.144190871369294316e-01,9.123443983402489410e-01,9.107883817427384621e-01,9.087136929460579715e-01,9.071576763485477146e-01,9.050829875518671130e-01,9.030082987551866225e-01,9.009336099585061319e-01,8.988589211618257524e-01,8.967842323651451508e-01,8.947095435684646603e-01,8.926348547717841697e-01,8.905601659751035681e-01,8.884854771784231886e-01,8.864107883817426980e-01,8.843360995850622075e-01,8.817427385892115943e-01,8.796680497925309927e-01,8.775933609958505022e-01,8.749999999999998890e-01,8.729253112033195094e-01,8.708506224066390189e-01,8.682572614107884057e-01,8.661825726141078041e-01,8.635892116182571909e-01,8.615145228215767004e-01,8.589211618257260872e-01,8.563278008298754740e-01,8.542531120331948724e-01,8.516597510373442592e-01,8.490663900414936460e-01,8.469917012448132665e-01,8.443983402489626533e-01,8.418049792531120401e-01,8.397302904564315496e-01,8.371369294605809364e-01,8.345435684647303232e-01,8.324688796680497216e-01,8.298755186721991084e-01,8.272821576763484952e-01,8.246887966804978820e-01,8.226141078838173915e-01,8.200207468879667783e-01,8.174273858921160540e-01,8.153526970954355635e-01,8.127593360995849503e-01,8.101659751037343371e-01,8.075726141078837239e-01,8.054979253112033444e-01,8.029045643153527312e-01,8.003112033195021180e-01,7.977178423236515048e-01,7.956431535269707922e-01,7.930497925311201790e-01,7.904564315352695658e-01,7.883817427385891863e-01,7.857883817427385731e-01,7.831950207468879599e-01,7.811203319502073583e-01,7.785269709543567451e-01,7.759336099585061319e-01,7.738589211618256414e-01,7.712655601659750282e-01,7.686721991701244150e-01,7.665975103734440355e-01,7.640041493775934223e-01,7.619294605809127097e-01,7.593360995850620965e-01,7.567427385892114833e-01,7.546680497925311037e-01,7.520746887966804906e-01,7.499999999999998890e-01,7.474066390041492758e-01,7.453319502074687852e-01,7.427385892116181720e-01,7.406639004149377925e-01,7.380705394190871793e-01,7.359958506224064667e-01,7.339211618257260872e-01,7.313278008298754740e-01,7.292531120331949834e-01,7.266597510373443702e-01,7.245850622406637687e-01,7.225103734439833891e-01,7.199170124481327759e-01,7.178423236514521744e-01,7.157676348547717948e-01,7.136929460580911933e-01,7.110995850622405801e-01,7.090248962655600895e-01,7.069502074688797100e-01,7.048755186721989974e-01,7.022821576763483842e-01,7.002074688796680046e-01,6.981327800829875141e-01,6.960580912863069125e-01,6.939834024896265330e-01,6.919087136929459314e-01,6.898340248962655519e-01,6.877593360995849503e-01])
cycles = np.arange(0,151)
#%% fit the capacity data
# define the empicrial model to be fitted
def He_model(k,a,b,c,d):
return a*np.exp(b*k)+c*np.exp(d*k)
# Fit the entire data set with the function …Run Code Online (Sandbox Code Playgroud) 我试图通过Windows 8计算机上的命令提示符从Python运行Abaqus脚本.
我遇到的问题是我无法让Python脚本等到Abaqus工作完成后才能继续前进.
下面是我尝试过的一些代码,一次一个:
os.popen('abaqus job=plate ask_delete=OFF')
Run Code Online (Sandbox Code Playgroud)
os.system('abaqus job=plate ask_delete=OFF')
Run Code Online (Sandbox Code Playgroud)
os.system('abaqus job=plate ask_delete=OFF')
Run Code Online (Sandbox Code Playgroud)
subprocess.call('abaqus job=plate ask_delete=OFF',shell=True)
Run Code Online (Sandbox Code Playgroud)
subprocess.check_call('abaqus job=plate ask_delete=OFF',shell=True)
Run Code Online (Sandbox Code Playgroud)
我也试过在这样的子进程工具之后使用wait命令:
p1 = subprocess.check_call('abaqus job=plate ask_delete=OFF',shell=True)
p1.wait()
Run Code Online (Sandbox Code Playgroud)
但我得到错误:AttributeError: 'int' object has no attribute 'wait'.
我不想使用a,time.sleep()因为这会导致我的代码中有相当多的停机时间.我使用的是Python 3.5.2和Anaconda 4.2.0(64位).