小编Had*_*dij的帖子

访问classification_report中的数字 - sklearn

这是一个简单的例子classification_reportsklearn

from sklearn.metrics import classification_report
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
print(classification_report(y_true, y_pred, target_names=target_names))
#             precision    recall  f1-score   support
#
#    class 0       0.50      1.00      0.67         1
#    class 1       0.00      0.00      0.00         1
#    class 2       1.00      0.67      0.80         3
#
#avg / total       0.70      0.60      0.61         5
Run Code Online (Sandbox Code Playgroud)

我希望能够访问平均/总行数.例如,我想从报告中提取f1-score,即0.61.

我怎样才能访问该号码classification_report

python classification scikit-learn

11
推荐指数
4
解决办法
3116
查看次数

R的哪个()和which.min()相当于Python

我在这里阅读了类似的主题.我认为问题不同或者至少.index()无法解决我的问题.

这是R中的一个简单代码及其答案:

x <- c(1:4, 0:5, 11)
x
#[1]  1  2  3  4  0  1  2  3  4  5 11
which(x==2)
# [1] 2 7
min(which(x==2))
# [1] 2
which.min(x)
#[1] 5
Run Code Online (Sandbox Code Playgroud)

它只返回满足条件的项的索引.

如果x是Python的输入,我如何获得符合条件的元素的indeces x==2和数组中最小的元素which.min.

x = [1,2,3,4,0,1,2,3,4,11] 
x=np.array(x)
x[x>2].index()
##'numpy.ndarray' object has no attribute 'index'
Run Code Online (Sandbox Code Playgroud)

python numpy r which

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

相当于py中R的"表"

R中,我们可以找到每个项目的频率table.这是R中的一个例子:

x <- c(1,1,1,1,2,2)
y <- c("a","a","b","a","a","b")
table(x,y)
#   y
#x   a b
#  1 3 1
#  2 1 1
Run Code Online (Sandbox Code Playgroud)

如何在python中实现它,而x和y是DataFrame?我是Python的新手,我搜索了很多,但我无法找到答案.我应该提一下,我读过这篇文章,但我无法在我的案例中实现它?

python r

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

Rand Index 函数(聚类性能评估)

据我所知,python 中没有可用于 Rand Index 的包,而对于 Adjusted Rand Index,您可以选择使用sklearn.metrics.adjusted_rand_score(labels_true, labels_pred).

我为 Rand Score 编写了代码,我将与其他人分享它作为帖子的答案。

python precision cluster-analysis unsupervised-learning

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

Python 中的 R 等价组合函数(生成 n 个元素的所有组合)

combn(x,m)R 中的函数一次生成 m 次元素 x 的所有组合。例如,

t(combn(5,2))
      [,1] [,2]
 [1,]    1    2
 [2,]    1    3
 [3,]    1    4
 [4,]    1    5
 [5,]    2    3
 [6,]    2    4
 [7,]    2    5
 [8,]    3    4
 [9,]    3    5
[10,]    4    5
Run Code Online (Sandbox Code Playgroud)

我怎么能在python中得到相同的结果?我知道scipy.misc.comb只给出结果而不是列表,我还阅读了这篇文章,它似乎有所不同,一开始需要给定的列表,而不仅仅是两个整数。

python combinations r

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

PackagesNotFoundError:当前渠道中不提供以下软件包

我正在尝试在 python 中安装 ngram,但它给出了错误。我尝试了这些代码,但它们又都给出了相同的错误:

conda install ngram
Run Code Online (Sandbox Code Playgroud)

第二:

conda config --add channels loopbio
conda config --append channels conda-forge
conda install ngram -c conda-forge
Run Code Online (Sandbox Code Playgroud)

错误是:

Solving environment: failed

PackagesNotFoundError: The following packages are not available from current channels:

  - ngram

Current channels:

  - https://conda.anaconda.org/conda-forge/win-64
  - https://conda.anaconda.org/conda-forge/noarch
  - https://conda.anaconda.org/loopbio/win-64
  - https://conda.anaconda.org/loopbio/noarch
  - https://repo.continuum.io/pkgs/main/win-64
  - https://repo.continuum.io/pkgs/main/noarch
  - https://repo.continuum.io/pkgs/free/win-64
  - https://repo.continuum.io/pkgs/free/noarch
  - https://repo.continuum.io/pkgs/r/win-64
  - https://repo.continuum.io/pkgs/r/noarch
  - https://repo.continuum.io/pkgs/pro/win-64
  - https://repo.continuum.io/pkgs/pro/noarch
  - https://repo.continuum.io/pkgs/msys2/win-64
  - https://repo.continuum.io/pkgs/msys2/noarch
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

我也试过

pip install ngram
Run Code Online (Sandbox Code Playgroud)

python n-gram anaconda conda

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

将每列除以R中的列

我有以下数据框:

df <- data.frame(type = c("planes", "trains", "automobiles"), t1 = c(4, 5, 6), t2 = c(20, 60, 24), t3 = c(100, 120, 72), t4 = c(800, 360, 144))

 df
         type t1 t2  t3  t4
1      planes  4 20 100 800
2      trains  5 60 120 360
3 automobiles  6 24  72 144
Run Code Online (Sandbox Code Playgroud)

我想编写一个函数,它接受每列中的值并将它们除以前一列,从t2/t1开始,这样我得到一个如下所示的新数据框:

new_df
         type t1 t2 t3 t4
1      planes     5  5  8
2      trains    12  2  3
3 automobiles     4  3  2
Run Code Online (Sandbox Code Playgroud)

使用扫描功能可能有一种方法可以做到这一点,但我还没有找到它.

提前致谢!

r

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

如何加入嵌套的字符串列表并获得结果作为新的字符串列表?

我有嵌套的字符串列表为:

A = [["A","B","C"],["A","B"]]
Run Code Online (Sandbox Code Playgroud)

我试图加入子列表中的字符串以获取单个字符串列表,如下所示:

B = [['ABC'],['AB']]
Run Code Online (Sandbox Code Playgroud)

要么,

B = ['ABC','AB']
Run Code Online (Sandbox Code Playgroud)

我尝试使用''.join(A)''.join(str(v) for v in A)但是它们没有用。

这是我得到的错误:

TypeError:序列项0:预期的str实例,找到列表

python string list python-3.x

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

将数字转换为data.frame中的字母

在R中,我有一些数字矩阵,我将把每个数字转换为其等效的字母;1至“ a”,2至“ b”,依此类推。

假设这是矩阵:

set.seed(1)
x <- data.frame(matrix(sample(1:16,12,T),nrow=3,ncol=4))
#  X1 X2 X3 X4
#1  5 15 16  1
#2  6  4 11  4
#3 10 15 11  3
Run Code Online (Sandbox Code Playgroud)

这是预期的输出:

#  X1 X2 X3 X4
#1  e  o  p  a
#2  f  d  k  d
#3  j  o  k  c
Run Code Online (Sandbox Code Playgroud)

我用letters[x]letters[list(x)],但它给了这个

字母错误[list(x)]:下标类型“ list”无效

string replace numbers r letters

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