为了熟悉全局优化方法,特别是shgo优化器,scipy.optimize v1.3.0我尝试在具有给定平均值的约束下最小化向量的方差var(x):x = [x1,...,xN]0 <= xi <= 1x
import numpy as np
from scipy.optimize import shgo
# Constraint
avg = 0.5 # Given average value of x
cons = {'type': 'eq', 'fun': lambda x: np.mean(x)-avg}
# Minimize the variance of x under the given constraint
res = shgo(lambda x: np.var(x), bounds=6*[(0, 1)], constraints=cons)
Run Code Online (Sandbox Code Playgroud)
该shgo方法在这个问题上失败了:
>>> res
fun: 0.0
message: 'Failed to find a feasible minimiser point. Lowest sampling point …Run Code Online (Sandbox Code Playgroud) import random
myfile = open('numbers.txt', 'w')
file_size = random.randint(4,7)
for count in range(file_size):
numbers = random.randint(5,19)
myfile.write(str(numbers) + '\n')
myfile.close()
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所拥有的,但我需要数字是所有赔率和“file_size”告诉代码的正确赔率。每个整数必须是随机的,介于 5 和 19 之间。
我正在使用Anaconda 2.7,我的fill_between()尝试也变得毫无结果.我不确定我是否错过了一个包或者我的绘图语法是否抛出python ...
这是我的代码:
from scipy import stats
import matplotlib.pyplot as plt
from numpy import linspace
alpha_A = 11
beta_A = 41
alpha_B = 3
beta_B = 3
x = linspace(0,1,num = 1000)
postA = stats.beta(alpha_A, beta_A).pdf(x)
postB = stats.beta(alpha_B, beta_B).pdf(x)
plt.figure(2, figsize = (6,4))
plt.plot(postA, color = 'r', label = "A: Beta(" + str(alpha_A) + ',' + str(beta_A) + ')')
plt.plot(postB, color = 'b',label = "B: Beta(" + str(alpha_B) + ',' + str(beta_B) + ')')
plt.legend(loc = …Run Code Online (Sandbox Code Playgroud) class Player:
# __slots__ = ['name','age','gsnum']
def __init__(self,name,age,gsnum):
self.name = name
self.age = age
self.gsnum = gsnum
Run Code Online (Sandbox Code Playgroud)
我定义了这个类,有三个属性
当__slots__用于节省内存时:
我得到36个一个实例(sys.getsizeof())如果没有使用__slots__,我仍然得到36作为内存大小,
这有什么问题?32位Python 2.7
我无法理解以下代码段-
if cv2.waitKey(0) & 0xFF == ord('q'):
break
Run Code Online (Sandbox Code Playgroud)
在此代码中-
1 import numpy as np
2 import cv2
3
4 cap = cv2.VideoCapture(0)
5
6 while(True):
7 # Capture frame-by-frame
8 ret, frame = cap.read()
9
10 # Our operations on the frame come here
11 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
12
13 # Display the resulting frame
14 cv2.imshow('frame',gray)
15 if cv2.waitKey(1) & 0xFF == ord('q'):
16 break
17
18 # When everything done, release the capture
19 cap.release()
20 cv2.destroyAllWindows() …Run Code Online (Sandbox Code Playgroud) python ×4
matplotlib ×1
opencv ×1
optimization ×1
plot ×1
python-2.7 ×1
python-3.x ×1
scipy ×1
shgo ×1
slots ×1