- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的数据(x 和 y 列是相关的): https://www.dropbox.com/s/b61a7enhoa0p57p/Simple1.csv
我需要的是用折线拟合数据。执行此操作的 Matlab 代码是:
spline_fit.m:
function [score, params] = spline_fit (points, x, y)
min_f = min(x)-1;
max_f = max(x);
points = [min_f points max_f];
params = zeros(length(points)-1, 2);
score = 0;
for i = 1:length(points)-1
in = (x > points(i)) & (x <= points(i+1));
if sum(in) > 2
p = polyfit(x(in), y(in), 1);
pred = p(1)*x(in) + p(2);
score = score + norm(pred - y(in));
params(i, :) = p;
else
params(i, :) = nan;
end
end
test.m:
%Find the parameters
r = [100,250,400];
p = fminsearch('spline_fit', r, [], x, y)
[score, param] = spline_fit(p, x, y)
%Plot the result
y1 = zeros(size(x));
p1 = [-inf, p, inf];
for i = 1:size(param, 1)
in = (x > p1(i)) & (x <= p1(i+1));
y1(in) = x(in)*param(i,1) + param(i,2);
end
[x1, I] = sort(x);
y1 = y1(I);
plot(x,y,'x',x1,y1,'k','LineWidth', 2)
这确实工作正常,产生了以下优化:[102.9842, 191.0006, 421.9912]
我在 R 中实现了同样的想法:
library(pracma);
spline_fit <- function(x, xx, yy) {
min_f = min(xx)-1;
max_f = max(xx);
points = c(min_f, x, max_f)
params = array(0, c(length(points)-1, 2));
score = 0;
for( i in 1:length(points)-1)
{
inn <- (xx > points[i]) & (xx <= points[i+1]);
if (sum(inn) > 2)
{
p <- polyfit(xx[inn], yy[inn], 1);
pred <- p[1]*xx[inn] + p[2];
score <- score + norm(as.matrix(pred - yy[inn]),"F");
params[i,] <- p;
}
else
params[i,] <- NA;
}
score
}
但是我得到了非常糟糕的结果:
> fminsearch(spline_fit,c(100,250,400), xx = Simple1$x, yy = Simple1$y)
$xval
[1] 100.1667 250.0000 400.0000
$fval
[1] 4452.761
$niter
[1] 2
如您所见,它在 2 次迭代后停止并且没有产生好的点。
对于解决此问题的任何帮助,我将非常高兴。
此外,如果有人知道如何使用任何免费库在 C# 中实现它,那就更好了。我知道从哪里获得 polyfit,但不知道 fminsearch。
最佳答案
这里的问题是似然面的表现非常糟糕——存在多个最小值和不连续的跳跃——这将使您使用不同的优化器获得的结果几乎是任意的。我承认 MATLAB 的优化器非常强大,但我要说的是,优化器是否会在这种情况下达到全局最小值几乎是一个偶然的问题(以及你从哪里开始),除非你使用某种形式的随机全局优化例如模拟退火。
我选择使用 R 的内置优化器(默认使用 Nelder-Mead)而不是 pracma
包中的 fminsearch
。
spline_fit <- function(x, xx = Simple1$x, yy=Simple1$y) {
min_f = min(xx)-1
max_f = max(xx)
points = c(min_f, x, max_f)
params = array(0, c(length(points)-1, 2))
score = 0
for( i in 1:(length(points)-1))
{
inn <- (xx > points[i]) & (xx <= points[i+1]);
if (sum(inn) > 2)
{
p <- polyfit(xx[inn], yy[inn], 1);
pred <- p[1]*xx[inn] + p[2];
score <- score + norm(as.matrix(pred - yy[inn]),"F");
params[i,] <- p;
}
else
params[i,] <- NA;
}
score
}
library(pracma) ## for polyfit
Simple1 <- read.csv("Simple1.csv")
opt1 <- optim(fn=spline_fit,c(100,250,400), xx = Simple1$x, yy = Simple1$y)
## [1] 102.4365 201.5835 422.2503
这比 fminsearch
结果更好,但与 MATLAB 结果仍然不同,并且比它们更差:
## Matlab results:
matlab_fit <- c(102.9842, 191.0006, 421.9912)
spline_fit(matlab_fit, xx = Simple1$x, yy = Simple1$y)
## 3724.3
opt1$val
## 3755.5 (worse)
bbmle
包提供了一组实验性的/没有很好记录的工具来探索优化表面:
library(bbmle)
ss <- slice2D(fun=spline_fit,opt1$par,nt=51)
library(lattice)
optim
估计参数周围的二维“切片”。圆圈显示最佳拟合(实心)和每个切片内的最小值(空心)。
png("splom1.png")
print(splom(ss))
dev.off()
matlab 和优化拟合之间的“切片”显示表面非常粗糙:
ss2 <- bbmle:::slicetrans(matlab_fit,opt1$par,spline_fit)
png("slice1.png")
print(plot(ss2))
dev.off()
关于r - R 中的 fminsearch 比 Matlab 中的差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20033993/
我的应用程序上有一个抽屉式菜单,它在桌面上运行良好,但在任何移动设备上我都看到一个丑陋的卡顿。 在 header 中,我有一个 bool 值,在单击汉堡包时将其设置为 true/false,这会将 o
在CLRS书中,自上而下的heapify构建堆的复杂度为O(n)。也可以通过反复调用插入来建立堆,其最坏情况下的复杂度为nlg(n)。 我的问题是:对于后一种方法性能较差的原因,是否有任何见解? 我问
我在所有层和输出上使用 sigmoid,得到的最终错误率为 0.00012,但是当我使用理论上更好的 Relu 时,我得到了最差的结果。谁能解释为什么会发生这种情况?我正在使用一个非常简单的 2 层实
我想计算有多少人(百分比)在我的测试中表现比我差。 这是我想要的结果: student | vak | resultaat | percentielscore ---------+-------
令人惊讶的是,使用 PLINQ 并没有在我创建的一个小测试用例上产生好处;事实上,它比通常的 LINQ 还要糟糕。 测试代码如下: int repeatedCount = 10000000;
我正在开发一个高度基于 map 的应用程序,并且我正在使用 MBXMapKit 框架(基于 MapKit 构建)以便在我的 MapView 中显示自定义 Mapbox map 图 block 而不是默
这个问题在这里已经有了答案: Is it always better to use 'DbContext' instead of 'ObjectContext'? (1 个回答) 关闭 9 年前。
我正在尝试使用 FFmpeg 进行一些复杂的视频转码(例如连接多个文件)。为此,我一直在尝试使用 filter_complex,但我注意到我之前使用普通视频过滤器看到的质量略有下降。 为了仔细检查,我
我是 R 中并行计算的新手,想使用并行包来加速我的计算(这比下面的示例更复杂)。但是,与通常的 lapply 函数相比,使用 mclapply 函数的计算时间更长。 我在我的笔记本电脑上安装了一个全新
我正在尝试使用 BERT 解决文档排名问题。我的任务很简单。我必须对输入文档进行相似度排名。这里唯一的问题是我没有标签——所以它更像是一个定性分析。 我正在尝试一系列文档表示技术——主要是 word2
如何计算两点的差?例如:(5,7) - (2,3) = (3,4) using point = boost::geometry::model::point point p1 (2, 3); point
我是 ARKit 的新手,在检查了一些示例代码后,如 https://developer.apple.com/sample-code/wwdc/2017/PlacingObjects.zip我想知道是
社区。 我正在编写一些机器学习代码,将一些数据分类。 我尝试了不同的方法,但是当我使用SVM时,我遇到了这个问题。 我有一组简单的数据(3 个类别,6 个特征),当我使用具有固定参数(C=10、gam
我只是在查看不同问题的答案以了解更多信息。我看到一个answer这表示在 php 中编写 是不好的做法 for($i=0;$i
我正在编写一个界面,我必须在其中启动 4 个 http 请求才能获取一些信息。 我用两种方式实现了接口(interface): 使用顺序 file_get_contents。 使用多 curl 。 我
我想用随机数来愚弄一下,如果 haskell 中的随机生成器是否均匀分布,因此我在几次尝试后写了下面的程序(生成的列表导致堆栈溢出)。 module Main where import System.
我在 Tensorflow 中构建了一个 LSTM 分类器(使用 Python),现在我正在做一系列基准测试来衡量执行性能。基准测试代码加载在训练期间保存的模型并针对大量输入执行它。我有一个 Pyth
不久前,我重构了单元格渲染器组件以实现性能提升(我有一个巨大的表格)。我从功能性无状态组件重构为 PureComponent。例如: import React from 'react'; import
当我改变缓冲区的大小时,我得到了无法从 BufferedReader 解释的奇怪结果。 我曾强烈期望性能会随着缓冲区大小的增加而逐渐增加, yield 递减设置相当快,此后性能或多或少会持平。但看起来
我正在尝试为 1000 个正面+负面标签的 IMDB 评论 (txt_sentoken) 和 Java 的 weka API 构建一个基于朴素贝叶斯的分类器。 由于我不知道 StringToWordV
我是一名优秀的程序员,十分优秀!