我必须为此模型函数实现最小二乘拟合算法
class MyScreen: UIViewController , RPBroadcastActivityViewControllerDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)
}
let broadcastPickerView = RPSystemBroadcastPickerView(frame: CGRect(x:0, y: 0, width: btnRS.frame.width, height: btnRS.frame.height))
broadcastPickerView.translatesAutoresizingMaskIntoConstraints = false
if let button = broadcastPickerView.subviews.first as? UIButton {
button.imageView?.tintColor = UIColor.white
}
btnRS.addSubview(broadcastPickerView)
broadcastPickerView.tintColor = .yellow
broadcastPickerView.backgroundColor = .clear
broadcastPickerView.showsMicrophoneButton = true
btnRS.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
我发现的方法是定义函数以计算残差并将其传递给scipy.optimize.leastsq或lmfit。但是,当参数是矢量而不是单个值时,我无法使用多维数据。
Y = a_0 * e^(a_1*x_1+a_2*x_2+...+a_n*x_n)
我收到此错误。
ValueError:设置具有序列的数组元素。
您能从这里为我指出正确的做法吗?
答案 0 :(得分:-1)
我认为类似的事情应该可以完成:
def residual(variables,X,y):
a_0 = variables[0]
a = variables[1:]
return (y - a_0 * np.exp(X.dot(a)))**2
X = np.random.randn(100,5)
y = np.random.randint(low=0,high=2,size=100)
a = np.random.randn(X.shape[1]+1)
a[0] = 1
res = scipy.optimize.leastsq(residual,a,args=(X,y))
致谢