小编Vei*_*pse的帖子

为numpy数组添加索引

我有一个numpy数组:

prob_rf = [[0.4, 0.4, 0.4], 
           [0.5, 0.5, 0.5], 
           [0.6, 0.6, 0.6]]
Run Code Online (Sandbox Code Playgroud)

我想为每个内部数组添加一个索引号:

prob_rf = [[1, 0.4, 0.4, 0.4],
           [2, 0.5, 0.5, 0.5],
           [3, 0.6, 0.6, 0.6]]
Run Code Online (Sandbox Code Playgroud)

然后csv使用将此数组保存到文件中numpy.savetxt.

我目前这样做:

    id = [i for i in xrange(1,len(prob)+1)]
    prob_rf = np.insert(prob_rf, 0, id, axis=1)
    np.savetxt("foo.csv", prob_rf, delimiter=",", fmt='%1.1f')
Run Code Online (Sandbox Code Playgroud)

但这是输出为

[[1.0, 0.4, 0.4, 0.4], 
 [2.0, 0.5, 0.5, 0.5], 
 [3.0, 0.6, 0.6, 0.6]]
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何获得输出

[[1, 0.4, 0.4, 0.4], 
 [2, 0.5, 0.5, 0.5], 
 [3, 0.6, 0.6, 0.6]]
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

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

从给定补丁中心和补丁比例的图像中提取补丁

我有一个 size 的图像1200 x 800

我想从这个图像中提取一个方形补丁,给定一个补丁中心 500, 450和一个图像较小尺寸的补丁比例 23%

中是否有任何函数opencvskimage或任何允许我在其中执行此操作的库python

python opencv image image-processing computer-vision

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

使用numpy矢量化方程式

在此输入图像描述

在此输入图像描述

我试图将上述公式实现为矢量化形式. K=3在这里,X150x4numpy数组.mu3x4numpy数组.Gamma是一个150x3numpy数组.Sigma是一个kx4x4numpy数组.因此Sigma[k]是一个4x4numpy数组.N=150

N_k = np.sum(Gamma, axis=0)
for k in range(K): # Correct
         x_new = X - mu[k] #Correct
         a = np.dot(x_new.T, x_new) #Incorrect from here I feel
         for i in range(len(data)):
             sigma[k] = Gamma[i][k] * a
         sigma[k]=sigma[k]/N_k #totally incorrect
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

python numpy scipy python-2.7 python-3.x

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

从非返回函数打印Python

问题1:

word = 'fast'

print '"',word,'" is nice'给出输出为" fast " is nice.

我如何获得输出,"fast" is nice即我希望前后删除空格word

问题2:

def faultyPrint():
    print 'nice'
Run Code Online (Sandbox Code Playgroud)

print 'Word is', faultyPrint() 给我输出为

Word is nice
None
Run Code Online (Sandbox Code Playgroud)

我希望输出Word is niceNone删除.

我不想要输出

print 'Word is'
faultyPrint()
Run Code Online (Sandbox Code Playgroud)

因为它给了我输出

Word is
nice
Run Code Online (Sandbox Code Playgroud)

如何在不更改功能和保持相同输出格式的情况下执行此操作?

python printing

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

Matlab的Python内联函数

在Matlab中,我们可以做到

x = -10:.1:10;
f = inline('normpdf(x,3,2) + normpdf(x,-5,1)','x');
t = plot(x,f(x))
Run Code Online (Sandbox Code Playgroud)

我们有类似inlinePython的功能吗?

python matlab numpy inline

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

使用pandas读取带有numpy数组的csv

我有一个csv3列文件emotion, pixels, Usage组成的35000行如0,70 23 45 178 455,Training.

pandas.read_csv以前读过这个csv文件pd.read_csv(filename, dtype={'emotion':np.int32, 'pixels':np.int32, 'Usage':str}).

当我尝试上述内容时,它说ValueError: invalid literal for long() with base 10: '70 23 45 178 455'?我如何读取像素列作为numpy数组?

python csv numpy pandas

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

矩阵的转置

我有一个名为class1维度的numpy数组50x4.

我找到了每一列的平均值class1.mean1 = np.mean(class1, axis=0)

np.mean 回报我 mean1 = [ 5.006 3.428 1.462 0.246]

当我尝试时mean1.T,它仍然会让我回归[ 5.006 3.428 1.462 0.246]

转置的正确方法是什么?

基本上我想这样mean1.T * mean1做,我得到一个4x4矩阵

python transpose numpy matrix python-3.x

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

Numpy数组到字典

我有一个numpy阵列

[['5.1', '3.5', '1.4', '0.2', 'Setosa'],
 ['4.9', '3.0', '1.4', '0.2', 'Versicolor']]
Run Code Online (Sandbox Code Playgroud)

如何将其转换为字典

{['5.1', '3.5', '1.4', '0.2']:'Setosa', ['4.9', '3.0', '1.4', '0.2']:'Versicolor'}
Run Code Online (Sandbox Code Playgroud)

python dictionary numpy python-3.x

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

使用正则表达式删除python中括号中的内容

我有一个list:

['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
Run Code Online (Sandbox Code Playgroud)

也像字符串一样 '14147618 (100%) 6137776 (43%) 5943229 (42%) 2066613 (14%) TOTAL\n'

使用正则表达式,我该如何返回:

['14147618', '6137776, '5943229', 2066613']
Run Code Online (Sandbox Code Playgroud)

python regex list python-2.7

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

双师表现错误

我有一个HashMap叫做List<String, Intger> wordFreqMapsize234

wordFreqMap = {radiology=1, shift=2, mummy=1, empirical=1, awful=1, geoff=1, .......}
Run Code Online (Sandbox Code Playgroud)

我想计算term frequency每个单词.

term frequency = frequency of term / total number of terms

public static Map<String, Double> getTFMap (Map<String, Integer> wordFreqMap)
    {
        Map<String, Double> tfMap = new HashMap<String, Double>();
        int noOfTerms = wordFreqMap.size();
        Double tf;

        for (Entry<String, Integer> word : wordFreqMap.entrySet() ) 
        {
                tf = (double) ( word.getValue() / noOfTerms );
                tfMap.put(word.getKey(), tf );
        }
        return tfMap;
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是, …

java double

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

Python打开用户创建的文本文件

考虑到存在一个名为"roger.txt"的手动用户创建的文本文件,它包含"Tennis Roger Federer Armin"作为其内容.Pickle无法打开手动创建的文件.哪些包可以在python中打开手动创建的文件,这样当我运行代码时,我得到输出为'Tennis Roger Federer Armin'?

python file

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