如何从python中numpy lib的均值方法中删除科学记数法 [英] how to remove e scientific notation from mean method of numpy lib in python

查看:70
本文介绍了如何从python中numpy lib的均值方法中删除科学记数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 python 和 numpy 库的新手.我正在对我的自定义数据集进行 PCA.我从 Pandas 计算数据帧每一行的平均值,但我得到以下结果作为平均值数组:

I'm new to python and numpy library.I'm doing PCA on my custom dataset. I calculate the mean of each row of my dataframe from pandas but I get below result as mean array:

[   7.433148e+46
    7.433148e+47
    7.433148e+47
    7.433148e+46
    7.433148e+46
    7.433148e+46
    7.433148e+46
    7.433148e+45
    7.433148e+47]

我的代码是:

   np.set_printoptions(precision=6)
   np.set_printoptions(suppress=False)
   df['mean']=df.mean(axis=1)
   mean_vector = np.array(df.iloc[:,15],dtype=np.float64)

  print('Mean Vector:\n', mean_vector)

这个数字是什么意思?我应该如何从数字中删除 e?

what's the meaning of this numbers? and how should I remove e from the number?

任何帮助真的很感激,提前致谢.

Any help really appreciate, Thanks in advance.

推荐答案

这些大数字是否现实?如果是,您希望如何显示它们?

Are these large numbers realistic, and, if so how do you want to display them?

复制并粘贴您的问题:

In [1]: x=np.array([7.433148e+46,7.433148e+47])

默认的 numpy 显示添加了几个小数点.

The default numpy display adds a few decimal pts.

In [2]: x
Out[2]: array([  7.43314800e+46,   7.43314800e+47])

改变精度不会有太大变化

changing precision doesn't change much

In [5]: np.set_printoptions(precision=6)
In [6]: np.set_printoptions(suppress=True)

In [7]: x
Out[7]: array([  7.433148e+46,   7.433148e+47])

suppress 的作用较小.它抑制小浮点值,而不是大浮点值

suppress does less. It supresses small floating point values, not large ones

suppress : bool, optional
Whether or not suppress printing of small floating point values using       
scientific notation (default False).

这些数字之一的默认 python 显示 - 也是科学的:

The default python display for one of these numbers - also scientific:

In [8]: x[0]
Out[8]: 7.4331480000000002e+46

使用格式化命令,我们可以将其显示在 46 多个字符的荣耀(或血腥细节)中:

With a formatting command we can display it in it's 46+ character glory (or gory detail):

In [9]: '%f'%x[0]
Out[9]: '74331480000000001782664341808476383296708673536.000000'

如果这是一个真正的价值,我更愿意看到科学计数法.

If that was a real value I'd prefer to see the scientific notation.

In [11]: '%.6g'%x[0]
Out[11]: '7.43315e+46'

为了说明 suppress 的作用,打印这个数组的逆:

To illustrate what suppress does, print the inverse of this array:

In [12]: 1/x
Out[12]: array([ 0.,  0.])

In [13]: np.set_printoptions(suppress=False)

In [14]: 1/x
Out[14]: array([  1.345325e-47,   1.345325e-48])

================

===============

我对 pandas 不太熟悉,但我想知道您的 mean 计算是否有意义.pandasdf.iloc[:,15] 打印什么?对于如此大的均值,原始数据必须具有相似大小的值.源如何显示它们?我想知道您的大多数值是否都是较小的正常值,而您是否有一些过大的值(异常值)会扭曲"平均值.

I'm not that familiar with pandas, but I wonder if your mean calculation makes sense. What does pandas print for df.iloc[:,15]? For the mean to be this large, the original data has to have values of similar size. How does the source display them? I wonder if most of your values are smaller, normal values, and your have a few excessively large ones (outliers) that 'distort' the mean.

我认为您可以使用 values 简化数组提取:

I think you can simplify the array extraction with values:

mean_vector = np.array(df.iloc[:,15],dtype=np.float64)
mean_vector = df.iloc[:,15].values

这篇关于如何从python中numpy lib的均值方法中删除科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆