import numpy和import math有什么区别 [英] What is the difference between import numpy and import math

查看:182
本文介绍了import numpy和import math有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始探索python,并尝试使用$ \ pi $进行一些计算.这是我获得$ \ pi $的方式:

I started exploring python and was trying to do some calculation with $\pi$. Here's how I got $\pi$:

import math as m
m.pi

但是有人建议使用numpy代替数学:

But someone suggested using numpy instead of math:

import numpy as np
np.pi

我的问题是,这两者之间有什么区别?在某些情况下,我们应该选择使用一种而不是另一种吗?

my question is, what is the difference between these two, and is there certain circumstances what we should choose to use one instead of another?

推荐答案

简短答案:

如果仅使用标量(而不使用列表或数组)进行简单的计算,请使用数学.

use math if you are doing simple comutations with only with scalars (and no lists or arrays).

如果要使用矩阵,数组或大型数据集进行科学计算,请使用 numpy .

Use numpy if you are doing scientific computations with matrices, arrays, or large datasets.

详细答案:

数学是标准python库的一部分.它提供了用于基本数学运算的函数以及一些常用的常量.

math is part of the standard python library. It provides functions for basic mathematical operations as well as some commonly used constants.

numpy 是面向科学计算的第三方软件包.它是python中用于数字和矢量运算的defacto软件包.结果,它提供了一些针对矢量和数组计算而优化的例程,与仅使用python列表相比,这种操作要快得多.有关更多信息,请参见 http://www.numpy.org/.

numpy on the other hand is a third party package geared towards scientific computing. It is the defacto package for numerical and vector operations in python. It provides several routines optimized for vector and array computations as a result, is a lot faster for such operations than say just using python lists. See http://www.numpy.org/ for more info.

这篇关于import numpy和import math有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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