小编Fre*_*Ren的帖子

如何随机将数据拆分为trainset和testset?

我有一个大型数据集,并希望将其分为培训(50%)和测试集(50%).

假设我有100个示例存储输入文件,每行包含一个示例.我需要选择50行作为训练集和50行测试集.

我的想法是首先生成一个长度为100的随机列表(值范围从1到100),然后使用前50个元素作为50个训练样例的行号.与测试集相同.

这可以在Matlab中轻松实现

fid=fopen(datafile);
C = textscan(fid, '%s','delimiter', '\n');
plist=randperm(100);
for i=1:50
    trainstring = C{plist(i)};
    fprintf(train_file,trainstring);
end
for i=51:100
    teststring = C{plist(i)};
    fprintf(test_file,teststring);
end
Run Code Online (Sandbox Code Playgroud)

但是我怎么能在Python中完成这个功能呢?我是Python的新手,不知道我是否可以将整个文件读入数组,并选择某些行.

python file-io

40
推荐指数
6
解决办法
10万
查看次数

Java如何计算负数?

我使用该~操作进行位操作,我只是想知道Java如何计算负数?

我检查了Java文档:

"一元按位补码运算符"〜"反转位模式;它可以应用于任何整数类型,使每个"0"为"1",每个"1"为"0".例如,一个字节包含8位;将此运算符应用于位模式为"00000000"的值将其模式更改为"11111111"."

那么,如果int a = 60 (0011 1100),那么int c = ~a (1100 0011).

问题是,Java如何计算负数以便1100 0011 = -611100 0011计算的唯一方法-61

  1. 最高位是符号位.
  2. -2^6 + 2^1 + 2^0 = -61.

但这对我来说毫无意义.

java bit

12
推荐指数
1
解决办法
1618
查看次数

REST API如何传递大型JSON?

我正在构建一个REST API并面临这个问题:REST API如何传递非常大的JSON?

基本上,我想连接到数据库并返回训练数据.问题出在数据库中我有400,000个数据.如果我将它们包装到JSON文件并通过GET方法,服务器将抛出堆溢出异常.

我们可以用什么方法来解决这个问题?

DBTraining trainingdata = new DBTraining();
@GET
@Produces("application/json")
@Path("/{cat_id}")
public Response getAllDataById(@PathParam("cat_id") String cat_id) {
    List<TrainingData> list = new ArrayList<TrainingData>();
    try {
        list = trainingdata.getAllDataById(cat_id);
        Gson gson = new Gson();
        Type dataListType = new TypeToken<List<TrainingData>>() {
        }.getType();
        String jsonString = gson.toJson(list, dataListType);
        return Response.ok().entity(jsonString).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "GET").build();

    } catch (SQLException e) {
        logger.warn(e.getMessage());
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

rest heap-memory large-data

5
推荐指数
1
解决办法
3855
查看次数

如何实现Quartz指数退避策略?

目前我的石英工作是以这种方式触发的:

  • 首次安排工作时,工作将在5分钟后触发.
  • 该工作将被触发5次,时间间隔为2分钟.

所以,如果我在该时间点安排我的工作,这项工作将在执行 time1 + 5,time1 + 7,time1 + 9,time1 + 11,time1 + 13

  private org.quartz.Trigger makeTrigger() {
    org.quartz.ScheduleBuilder<org.quartz.SimpleTrigger> scheduleBuilder =
        org.quartz.SimpleScheduleBuilder.simpleSchedule()
            .withIntervalInMilliseconds(intervalMillis)
            .withRepeatCount(repeatCount);

    return org.quartz.TriggerBuilder.newTrigger()
        .withIdentity(key.getLeft(), key.getRight())
        .usingJobData(new org.quartz.JobDataMap(jobData))
        .startAt(dateTimeHelper.toStandardDate(startDateTime))
        .withSchedule(scheduleBuilder)
        .build();
  }
Run Code Online (Sandbox Code Playgroud)

但我需要让工作不那么激进,所以我的问题是如何设置触发器以指数时间间隔?所以这之后我安排在该时间点的作业,作业将在执行: time1 + 5,time1 + 7,time1 + 11,time1 + 19,time1 + 35

我应该在每次工作完成后重新安排工作吗?

java quartz

5
推荐指数
0
解决办法
307
查看次数

SVM 中的参数 C 和标准来查找最佳参数

SVM中的成本参数C是什么意思?我的意思是,如果C很大,是否意味着“我不能容忍错误的分类”?

在实验中寻找最佳参数时如何确定范围和步长?

顺便问一下,决定哪个参数更好的标准是什么?交叉验证的错误数量或我们从 SVM 获得的支持向量数量?

statistics svm

4
推荐指数
1
解决办法
2万
查看次数

如何使用jooq连接3个表并迭代结果?

我有COURSE,STUDENT,SCHEDULE表.

table course(id, name, ....), 
table student(id, name, ...), 
table schedule(id, c_id, s_id).
Run Code Online (Sandbox Code Playgroud)

现在我想离开连接时间表与课程和学生表.

问题(1):

在jooq中加入这3个表的最佳方法是什么?我假设它是这样的:

TableLike<?> firstjoin = sql
    .select()
    .from(Tables.SCHEUDLE)
    .leftOuterJoin(Tables.COURSE)
    .on(Tables.SCHEDULE.CID.eq(Tables.COURSE.ID))
    .asTable();

Result<?> result = sql
    .select()
    .from(firstjoin)
    .leftOuterJoin(Tables.STUDENT)
    .on(Tables.SCHEDULE.SID.eq(Tables.STUDENT.ID))
    .fetch();
Run Code Online (Sandbox Code Playgroud)

问题2):

当我得到结果时,将结果拆分为Student对象和Course对象的最佳方法是什么?我的意思是因为类型是结果?,有没有什么方法可以将结果映射到学生,课程实体而不是繁琐地做这样的事情:

for(Record r: result){
   Student s = new Student(r.filed(), r.filed()...);
   Course c = new Course(r.filed(), r.filed()....)
}
Run Code Online (Sandbox Code Playgroud)

java mapping join jooq

4
推荐指数
1
解决办法
3574
查看次数

如何将Class列表作为参数传递?

我试图有一个handleException方法,它可以采用异常对象和可接受的异常类列表来检查异常是否可以接受并且可以重试.

void handleException(Exception e, String... acceptableExceptionNames)
      throws MyException {

    boolean isRetryable = false;

    for(String acceptableExceptionName: acceptableExceptionNames) {
      try {
        if (Class.forName(acceptableExceptionName).isInstance(e)) {
          isRetryable = true;
          break;
        }
      } catch (ClassNotFoundException e1) {
        continue;
      }
    }

    if (isRetryable) {
      // log retryable
    } else {
      // log error
    }

    throw new MyException(isRetryable, "Failed");
  }
Run Code Online (Sandbox Code Playgroud)

我传入的参数是a String... classNames而不是Class<? extends Exception> classes,因为如果我做这样的事情:

void handleException(
    Exception e,
    Class<? extends Exception>... acceptableExceptions)
      throws MyException {
    for (Class acceptableException : acceptableExceptions) …
Run Code Online (Sandbox Code Playgroud)

java exception

3
推荐指数
1
解决办法
257
查看次数

matlab如何将图像转90度?

我有100个灰色图像(256*256像素),它们存储在faces.mat文件中.所以在faces.mat文件中,有100行和65536(256*256)列.每行代表一个图像.

现在我想重塑面部矩阵并显示图像.

我用:

for i=1:N    
    imagesc(reshape(faces(i,:)'),256,256));
    colormap gray;
end
Run Code Online (Sandbox Code Playgroud)

但我发现我的图像变成了90度! 在此输入图像描述

有人能告诉我如何正确转动图像吗?当我把它们变成face.mat时,我处理的图像是否错误?下面是我写的将图像存储到矩阵的代码.

function ImageGenerate
Files = dir(strcat('D:\face\','*.tiff'));
LengthFiles = length(Files);
faces = [];
for i = 1:LengthFiles;
    Img = imread(strcat('D:\face\',Files(i).name));
    temp = im2double(Img);
    [row, col] = size(temp);
    vector = [];
    for i =1 : row
        for j = 1:col
            vector = [vector temp(i,j)];
        end
    end
    faces = [faces;vector];
end
save('faces2.mat','faces');
Run Code Online (Sandbox Code Playgroud)

matlab image-processing reshape

2
推荐指数
1
解决办法
7543
查看次数

浏览器中的"跨源请求已阻止:同源策略"错误

当我尝试将json文件发送到我的服务器时,我收到此错误.

在我的服务器端,代码是:

    @POST
    @Path("updatedata")
    @Produces("text/plain")
    @Consumes("application/json")
    public Response UpdateData(String info) {
        Gson gson = new Gson();
        List<Data> list = gson.fromJson(info, new TypeToken<List<Data>>() {
        }.getType());

        int is_success = 0;
        try {
            is_success += trainingdata.updateData(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String returnjson = "{\"raw\":\"" + list.size() + "\",\"success\":\"" + is_success + "\"}";
        return Response.ok().entity(returnjson).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "POST").build();
    }
Run Code Online (Sandbox Code Playgroud)

我可以通过RESTClient成功更新我的数据 - 一个Chrome插件.

但是当我构建前端并尝试通过jaascript调用API时,Firefox显示:跨源请求被阻止:同源策略.... Chrome显示:XMLHttpRequest无法加载...没有'Access-Control-Allow-Origin '标头出现在请求的资源上.因此不允许原点'...'访问

我写了这样的javascript:

var json = JSON.stringify(array);

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://myurl:4080/updatedata", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(json);

xhr.onload …
Run Code Online (Sandbox Code Playgroud)

javascript rest cors

2
推荐指数
2
解决办法
2万
查看次数

如何在 Linux 上卸载 perl

我刚刚在 linux 上安装了 perl,参考http://learn.perl.org/installing/unix_linux.html

使用命令: curl -L https://raw.githubusercontent.com/ranguard/installing-perl/master/scripts/install_perl_on_nix.sh | bash

有谁知道如何删除perl?谢谢。

linux perl uninstallation

1
推荐指数
1
解决办法
2万
查看次数

shell中有没有忽略转义字符的方法?

在 C# 中,有一个逐字字符串,因此,

string c = "hello \t world";               // hello     world
string d = @"hello \t world";              // hello \t world
Run Code Online (Sandbox Code Playgroud)

我是shell脚本新手,shell中有类似的方法吗?

因为我有很多名称为“Apparel & Accessories > Clothing > Activewear”的文件夹,所以我想知道是否有一种简单的方法可以处理转义字符而无需编写这么多。

test.sh

director="Apparel & Accessories > Clothing > Activewear"
# any action to escape spaces, &, >   ???
hadoop fs -ls $director
Run Code Online (Sandbox Code Playgroud)

shell escaping

1
推荐指数
1
解决办法
9972
查看次数

在Pig中计算行数的有效方法是什么?

在Pig中,获得计数的有效方法是什么?我们可以做一个GROUP ALL,但这只给了1个reducer.当数据量非常大时,比如n太字节,我们能以某种方式尝试多个减速器吗?

  dataCount = FOREACH (GROUP data ALL) GENERATE 
    'count' as metric,
    COUNT(dataCount) as value;
Run Code Online (Sandbox Code Playgroud)

apache-pig reducers

0
推荐指数
1
解决办法
9869
查看次数