scipy tricks

排列组合数

1
2
from scipy.misc import comb
comb(N, k, exact=False, repetition=False)

最优化相关

Optimization and root finding (scipy.optimize) — SciPy v0.18.1 Reference Guide

from scipy.optimize import leastsq

Minimize the sum of squares of a set of equations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
###最小二乘法试验###
import numpy as np
from scipy.optimize import leastsq

###采样点(Xi,Yi)###
Xi=np.array([8.19,2.72,6.39,8.71,4.7,2.66,3.78])
Yi=np.array([7.01,2.78,6.47,6.71,4.1,4.23,4.05])

###需要拟合的函数func及误差error###
def func(p,x):
k,b=p
return k*x+b

def error(p,x,y,s):
print s
return func(p,x)-y #x、y都是列表,故返回值也是个列表

#TEST
p0=[100,2]
#print( error(p0,Xi,Yi) )

###主函数从此开始###
s="Test the number of iteration" #试验最小二乘法函数leastsq得调用几次error函数才能找到使得均方误差之和最小的k、b
Para=leastsq(error,p0,args=(Xi,Yi,s)) #把error函数中除了p以外的参数打包到args中
k,b=Para[0]
print"k=",k,'\n',"b=",b

###绘图,看拟合效果###
import matplotlib.pyplot as plt

plt.figure(figsize=(8,6))
plt.scatter(Xi,Yi,color="red",label="Sample Point",linewidth=3) #画样本点
x=np.linspace(0,10,1000)
y=k*x+b
plt.plot(x,y,color="orange",label="Fitting Line",linewidth=2) #画拟合直线
plt.legend()
plt.show()

from scipy.optimize import linprog

1
2
3
4
5
6
7
8
Minimize a linear objective function subject to linear equality and inequality constraints.

Linear Programming is intended to solve the following problem form:
Minimize:
c^T * x
Subject to:
A_ub * x <= b_ub
A_eq * x == b_eq

from scipy.optimize import minimize

Minimization of scalar function of one or more variables.

In general, the optimization problems are of the form:

minimize f(x) subject to

1
2
g_i(x) >= 0,  i = 1,...,m
h_j(x) = 0, j = 1,...,p

where x is a vector of one or more variables. g_i(x) are the inequality constraints. h_j(x) are the equality constrains.

Optionally, the lower and upper bounds for each element in x can also be specified using the bounds argument.