小编Dzu*_*yen的帖子

将列名从int转换为pandas中的string

我有一个混合列名的pandas数据帧:

1,2,3,4,5,'班级'

当我将此数据帧保存到h5file时,它表示由于混合类型,性能将受到影响.如何在pandas中将整数转换为字符串?

python pandas

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

在C++中使用vector

我遇到以下代码时出现问题,似乎无法弄清楚出了什么问题

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

double distance(int a, int b)
{
    return fabs(a-b);
}

int main()
{
    vector<int> age;
    age.push_back(10);
    age.push_back(15);

    cout<<distance(age[0],age[1]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误在于调用函数距离.

/usr/include/c++/4.6/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<int>’:
test.cpp:18:30:   instantiated from here
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:166:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:167:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:168:53: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:169:53: error: ‘int’ is not a class, …
Run Code Online (Sandbox Code Playgroud)

c++ vector

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

lambda函数中的可选参数

我有一个功能:

cost(X, model, reg = 1e-3, sparse)
Run Code Online (Sandbox Code Playgroud)

我需要在以下形式下将此函数传递给另一个函数:

f(X, model)
f(X, model, reg = reg)
Run Code Online (Sandbox Code Playgroud)

我正在使用lambda来做到这一点:

f = lambda X, model: cost(X, model, sparse = np.random.rand(10,10))
Run Code Online (Sandbox Code Playgroud)

并且python抱怨lambda得到了意想不到的参数reg.我该怎么做呢?

如果我做其他方式:

f = lambda X, model, reg: cost(X, model, reg = reg, sparse = np.random.rand(10,10))
Run Code Online (Sandbox Code Playgroud)

然后它在第一种情况下不起作用.

python lambda

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

图书馆dl在gcc中的意义

我正在检查一个makefile,看到使用的库是:

LIBS = -lcppunit -ldl
Run Code Online (Sandbox Code Playgroud)

lcppunit是单元测试库.那是什么ldl

c++ linux dynamic-library

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

使用QtCreator设置GDB

我有一个使用OpenCV和cmake的简单项目,并且有两个源文件,只有segmentation.h和segmentation.cpp.

这是cmakefile:

project(Segment)
cmake_minimum_required(VERSION 2.8)

SET(CMAKE_BUILD_TYPE Debug)
SET(CMAKE_VERBOSE_MAKEFILE true)

if(CMAKE_COMPILER_IS_GNUCXX)
    message(STATUS "GCC detected, adding compile flags")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -std=c++98 -Wall")
endif(CMAKE_COMPILER_IS_GNUCXX)

find_package(OpenCV REQUIRED)
add_executable(Lulu segmentation.cpp segmentation.h)
target_link_libraries(Lulu ${OpenCV_LIBS})
Run Code Online (Sandbox Code Playgroud)

我使用发送到cmake的参数创建了一个Debug构建:-DCMAKE_BUILD_TYPE = Debug.但是QtCreator仍然会跳过断点,并且无法正常启动gdb:

&"warning: GDB: Failed to set controlling terminal: Inappropriate ioctl for device\n"
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

c++ opencv gdb cmake qt-creator

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

编译错误cv :: gpu

我在Ubuntu 12.04上使用OpenCV master branch(3.0.0.dev)和CUDA,并尝试使用gpu代码编译以下opencv:

#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"

using namespace cv;

int main (int argc, char* argv[])
{
    try
    {
        cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
        cv::gpu::GpuMat dst, src;
        src.upload(src_host);

        cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);

        cv::Mat result_host = dst;
        cv::imshow("Result", result_host);
        cv::waitKey();
    }
    catch(const cv::Exception& ex)
    {
        std::cout << "Error: " << ex.what() << std::endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译命令是:

g++ testgpu.cpp -o test `pkg-config --cflags --libs opencv` -lopencv_gpu
Run Code Online (Sandbox Code Playgroud)

它有以下编译错误:

testgpu.cpp: In function ‘int main(int, …
Run Code Online (Sandbox Code Playgroud)

c++ opencv

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

带有notifyAll()的多个线程

我正在编写一个打印秒数的java程序,每隔5秒打印一条消息.这是一个示例输出:

0 1 2 3 4 hello 5 6 7 8 9 hello 10 11 12 13 14 hello 15 16 17 18 19 hello 
Run Code Online (Sandbox Code Playgroud)

如何删除布尔变量printMsg?是否有更好的线程设计允许这个?

目前,如果没有printMsg,程序将在1/10秒的程序中打印多个"hello",停留在5,10,15等.

class Timer {
    private int count = 0;
    private int N;
    private String msg;
    private boolean printMsg = false;

    public Timer(String s, int N) {
        msg = s;
        this.N = N;
    }

    public synchronized void printMsg() throws InterruptedException{
        while (count % N != 0 || !printMsg)
            wait();
        System.out.print(msg + " ");
        printMsg = …
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

在Python中读入索引的彩色图像

索引彩色图像是具有整数(1,2,... N)的像素的图像,并且对于每个整数,关联的颜色从给定的彩色图映射到该像素.在MATLAB中,可以通过以下方式读取索引彩色图像:

[im, colormap] = imread('indexed.png');
Run Code Online (Sandbox Code Playgroud)

我怎样才能在Python中做同样的事情?我尝试过OpenCV,scikit-image但它们都会自动转换为RGB.

python matlab image image-processing

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

从RNN小区获得权重和偏差的值

我在张量流中有以下vanilla RNN实现.如何从basicRNNCell获得权重和偏差的值?

import tensorflow as tf
import numpy as np

input_size = 5
batch_size = 2
max_length = 1

cell = tf.nn.rnn_cell.BasicRNNCell(num_units = 4)

# Batch size x time steps x features.
data = tf.placeholder(tf.float32, [None, max_length, input_size])
output, _ = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32)

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    result = sess.run([output], feed_dict={data: np.ones((batch_size, max_length, input_size))})

    print result
    print result[0].shape

    for v in tf.trainable_variables():
        print v.name
        print dir(v)
Run Code Online (Sandbox Code Playgroud)

tensorflow

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

matplotlib的x标签的分钟和秒格式

如何在下图中显示mm:ss而不是样本编号?

在此输入图像描述

我试过了:

def formatMS(miliseconds):
    return time.strftime('%M:%S', time.gmtime(miliseconds//1000))

fig, ax = plt.subplots()
plt.plot(segtime, segStrength)
labels = [formatMS(t) for t in segtime]
print labels
ax.set_xticklabels(labels)
Run Code Online (Sandbox Code Playgroud)

但现在它显示所有00:00.我该如何解决这个问题?

python matplotlib

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