📜  如何在 Windows 上安装 Numpy?

📅  最后修改于: 2022-05-13 01:57:03.933000             🧑  作者: Mango

如何在 Windows 上安装 Numpy?

Python NumPy是一个通用数组处理包,提供处理 n 维数组的工具。提供综合数学函数、线性代数例程等多种计算工具。 NumPy 提供Python的灵活性和经过良好优化的编译 C 代码的速度。其易于使用的语法使其对任何背景的程序员都具有高度的可访问性和生产力。

先决条件:

在 Windows 上安装 Numpy 唯一需要的是:

  • Python
  • PIP 或 Conda(取决于用户偏好)

在 Windows 上安装 Numpy:

对于 Conda 用户:

如果您希望通过conda 完成安装,可以使用以下命令:

conda install -c anaconda numpy

安装完成后,您将收到类似的消息

使用 conda 安装 numpy

确保遵循使用conda进行安装的最佳实践:

  • 使用环境进行安装,而不是使用以下命令在基本环境中进行安装:
conda create -n my-env
conda activate my-env

注意:如果您首选的安装方法是conda-forge,请使用以下命令:

conda config --env --add channels conda-forge

对于 PIP 用户:

喜欢使用 pip 的用户可以使用以下命令安装 NumPy:

pip install numpy

安装完成后,您将收到类似的消息:

使用 pip 安装 numpy

现在我们已经在我们的系统中成功安装了 Numpy,让我们看几个简单的例子。

示例 1:基本 Numpy 数组字符

Python3
# Python program to demonstrate
# basic array characteristics
import numpy as np
  
# Creating array object
arr = np.array( [[ 1, 2, 3],
                [ 4, 2, 5]] )
  
# Printing type of arr object
print("Array is of type: ", type(arr))
  
# Printing array dimensions (axes)
print("No. of dimensions: ", arr.ndim)
  
# Printing shape of array
print("Shape of array: ", arr.shape)
  
# Printing size (total number of elements) of array
print("Size of array: ", arr.size)
  
# Printing type of elements in array
print("Array stores elements of type: ", arr.dtype)


Python3
# Python program to demonstrate
# basic operations on single array
import numpy as np
  
a = np.array([1, 2, 5, 3])
  
# add 1 to every element
print ("Adding 1 to every element:", a+1)
  
# subtract 3 from each element
print ("Subtracting 3 from each element:", a-3)
  
# multiply each element by 10
print ("Multiplying each element by 10:", a*10)
  
# square each element
print ("Squaring each element:", a**2)
  
# modify existing array
a *= 2
print ("Doubled each element of original array:", a)
  
# transpose of array
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]])
  
print ("\nOriginal array:\n", a)
print ("Transpose of array:\n", a.T)


输出:

Array is of type:  
No. of dimensions:  2
Shape of array:  (2, 3)
Size of array:  6
Array stores elements of type:  int64

示例 2:基本 Numpy 操作

Python3

# Python program to demonstrate
# basic operations on single array
import numpy as np
  
a = np.array([1, 2, 5, 3])
  
# add 1 to every element
print ("Adding 1 to every element:", a+1)
  
# subtract 3 from each element
print ("Subtracting 3 from each element:", a-3)
  
# multiply each element by 10
print ("Multiplying each element by 10:", a*10)
  
# square each element
print ("Squaring each element:", a**2)
  
# modify existing array
a *= 2
print ("Doubled each element of original array:", a)
  
# transpose of array
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]])
  
print ("\nOriginal array:\n", a)
print ("Transpose of array:\n", a.T)

输出:

Adding 1 to every element: [2 3 6 4]
Subtracting 3 from each element: [-2 -1  2  0]
Multiplying each element by 10: [10 20 50 30]
Squaring each element: [ 1  4 25  9]
Doubled each element of original array: [ 2  4 10  6]

Original array:
 [[1 2 3]
 [3 4 5]
 [9 6 0]]
Transpose of array:
 [[1 3 9]
 [2 4 6]
 [3 5 0]]