python中 numpy

Python中的Numpy是什么? (What is Numpy in Python?)

Numpy is an array processing package which provides high-performance multidimensional array object and utilities to work with arrays. It is a basic package for scientific computation with python. It is a linear algebra library and is very important for data science with python since almost all of the libraries in the pyData ecosystem rely on Numpy as one of their main building blocks. It is incredibly fast, as it has bindings to C.

Numpy是一个数组处理程序包,它提供高性能的多维数组对象和实用程序来处理数组。 它是使用python进行科学计算的基本软件包。 它是一个线性代数库,对于使用python进行数据科学非常重要,因为pyData生态系统中的几乎所有库都依赖Numpy作为其主要构建模块之一。 它非常快,因为它与C具有绑定。

Some of the many features, provided by numpy are as always,

numpy提供的许多功能一如既往,

  1. N-dimensional array object

    N维数组对象

  2. Broadcasting functions

    广播功能

  3. Utilities for integrating with C / C++

    与C / C ++集成的实用程序

  4. Useful linear algebra and random number capabilities

    有用的线性代数和随机数功能

安装Numpy (Installing Numpy)

1) Using pip

1)使用点子

    pip install numpy

Installing output

安装输出

pip install numpy
Collecting numpy
Downloading https://files.pythonhosted.org/packages/60/9a/a6b3168f2194fb468dcc4cf54c8344d1f514935006c3347ede198e968cb0/numpy-1.17.4-cp37-cp37m-macosx_10_9_x86_64.whl (15.1MB)
100% |████████████████████████████████| 15.1MB 1.3MB/s
Installing collected packages: numpy
Successfully installed numpy-1.17.4

2) Using Anaconda

2)使用水蟒

    conda install numpy

numpy中的数组 (Arrays in Numpy)

Numpy's main object is the homogeneous multidimensional array. Numpy arrays are two types: vectors and matrices. vectors are strictly 1-d arrays and matrices are 2-d.

Numpy的主要对象是齐次多维数组。 numpy数组有两种类型: 向量和矩阵向量严格是一维数组, 矩阵是二维。

In Numpy dimensions are known as axes. The number of axes is rank. The below examples lists the most important attributes of a ndarray object.

在Numpy中,尺寸称为轴。 轴数为等级。 以下示例列出了ndarray对象的最重要属性。

Example:

例:

# importing package
import numpy as np
# creating array
arr = np.array([[11,12,13],[14,15,16]])
print("Array is of type {}".format(type(arr)))
print("No. of dimensions {}".format(arr.ndim))
print("shape of array:{}".format(arr.shape))
print("size of array:{}".format(arr.size))
print("type of elements in the array:{}".format(arr.dtype))

Output

输出量

Array is of type <class 'numpy.ndarray'>
No. of dimensions 2
shape of array:(2, 3)
size of array:6
type of elements in the array:int64

创建一个numpy数组 (Creating a numpy array)

Creating a numpy array is possible in multiple ways. For instance, a list or a tuple can be cast to a numpy array using the. array() method (as explained in the above example). The array transforms a sequence of the sequence into 2-d arrays, sequences of sequences into a 3-d array and so on.

创建numpy数组的方式有多种。 例如,可以使用将列表或元组强制转换为numpy数组。 array()方法 (如以上示例中所述)。 数组将序列的序列转换为2维数组,将序列的序列转换为3维数组,依此类推。

To create sequences of numbers, NumPy provides a function called arange that returns arrays instead of lists.

为了创建数字序列,NumPy提供了一个称为arange的函数,该函数返回数组而不是列表。

Syntax:

句法:

    # returns evenly spaced values within a given interval.
arange([start,] stop [,step], dtype=None)

Example:

例:

x = np.arange(10,30,5)
print(x)
# Ouput: [10 15 20 25]

The function zeros create an array full of zeros, the function ones create an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.

函数零将创建一个由零组成的数组,函数一个将创建由零组成的数组,函数空将创建一个数组,其初始内容是随机的,并且取决于内存的状态。 默认情况下,创建的数组的dtype为float64。

Example:

例:

# importing package
import numpy as np
x = np.zeros((3,4))
print("np.zeros((3,4))...")
print(x)
x = np.ones((3,4))
print("np.ones((3,4))...")
print(x)
x = np.empty((3,4))
print("np.empty((3,4))...")
print(x)
x = np.empty((1,4))
print("np.empty((1,4))...")
print(x)

Output

输出量

np.zeros((3,4))...
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
np.ones((3,4))...
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
np.empty((3,4))...
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
np.empty((1,4))...
[[1.63892563e-316 0.00000000e+000 2.11026305e-312 2.56761491e-312]]

numpy函数 (Numpy functions)

Some more function available with NumPy to create an array are,

NumPy提供了一些更多的函数来创建数组,

1) linspace()

1)linspace()

It returns an evenly spaced numbers over a specified interval.

它在指定的间隔内返回均匀间隔的数字。

Syntax:

句法:

    linspace(start, stop, num=50, endpoint=True, restep=False, dtype=None)

Example:

例:

# importing package
import numpy as np
x = np.linspace(1,3,num=10)
print(x)

Output

输出量

[1.         1.22222222 1.44444444 1.66666667 1.88888889 2.11111111
2.33333333 2.55555556 2.77777778 3.        ]

2) eye()

2)眼睛()

It returns a 2-D array with ones on the diagonal and zeros elsewhere.

它返回一个二维数组,对角线上有一个,其他位置为零。

Syntax:

句法:

    eye(N, M=None, k=0, dtype=<class 'float'>, order='C')

Example:

例:

# importing package
import numpy as np
x = np.eye(4)
print(x)

Output

输出量

[[1. 0. 0. 0.] [0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

3) random()

3)random()

It creates an array with random numbers

它创建一个带有随机数的数组

Example:

例:

# importing package
import numpy as np
x = np.random.rand(5)
print("np.random.rand(5)...")
print(x)
x = np.random.rand(5,1)
print("np.random.rand(5,1)...")
print(x)
x = np.random.rand(5,1,3)
print("np.random.rand(5,1,3)...")
print(x)
# returns a random number
x = np.random.randn()
print("np.random.randn()...")
print(x)
# returns a 2-D array with random numbers
x = np.random.randn(2,3)
print("np.random.randn(2,3)...")
print(x)
x = np.random.randint(3)
print("np.random.randint(3)...")
print(x)
# returns a random number in between low and high
x = np.random.randint(3,100)
print("np.random.randint(3,100)...")
print(x)
# returns an array of random numbers of length 34
x = np.random.randint(3,100,34)
print("np.random.randint(3,100,34)...")
print(x)

Output

输出量

np.random.rand(5)...[0.87417146 0.77399086 0.40012698 0.37192848 0.98260636]
np.random.rand(5,1)...
[[0.712829  ]
[0.65959462]
[0.41553044]
[0.30583293]
[0.83997539]]
np.random.rand(5,1,3)...
[[[0.75920149 0.54824968 0.0547891 ]]
[[0.70911911 0.16475541 0.5350475 ]]
[[0.74052103 0.4782701  0.2682752 ]]
[[0.76906319 0.02881364 0.83366651]]
[[0.79607073 0.91568043 0.7238144 ]]]
np.random.randn()...
-0.6793254693909823
np.random.randn(2,3)...
[[ 0.66683143  0.44936287 -0.41531392]
[ 1.86320357  0.76638331 -1.92146833]]
np.random.randint(3)...
1
np.random.randint(3,100)...
53
np.random.randint(3,100,34)...
[43 92 76 39 78 83 89 87 96 59 32 74 31 77 56 53 18 45 78 21 46 10 25 86
64 29 49  4 18 19 90 17 62 29]

4) Reshape method (shape manipulation)

4)整形方法(形状处理)

An array has a shape given by the number of elements along each axis,

数组的形状由沿每个轴的元素数确定,

# importing package
import numpy as np
x = np.floor(10*np.random.random((3,4)))
print(x)
print(x.shape)

Output

输出量

[[0. 2. 9. 4.]
[0. 4. 1. 7.]
[9. 7. 6. 2.]]
(3, 4)

The shape of an array can be changes with various commands. However, the shape commands return all modified arrays but do not change the original array.

数组的形状可以通过各种命令进行更改。 但是,shape命令返回所有修改后的数组,但不更改原始数组。

# importing package
import numpy as np
x = np.floor(10*np.random.random((3,4)))
print(x)
# returns the array, flattened
print("x.ravel()...")
print(x.ravel())
# returns the array with modified shape
print("x.reshape(6,2)...")
print(x.reshape(6,2))
# returns the array , transposed
print("x.T...")
print(x.T)
print("x.T.shape...")
print(x.T.shape)
print("x.shape...")
print(x.shape)

Output

输出量

[[3. 1. 0. 6.] [3. 1. 2. 4.]
[7. 0. 0. 1.]]
x.ravel()...
[3. 1. 0. 6. 3. 1. 2. 4. 7. 0. 0. 1.]
x.reshape(6,2)...
[[3. 1.]
[0. 6.]
[3. 1.]
[2. 4.]
[7. 0.]
[0. 1.]]
x.T...
[[3. 3. 7.] [1. 1. 0.]
[0. 2. 0.]
[6. 4. 1.]]
x.T.shape...
(4, 3)
x.shape...
(3, 4)

其他方法 (Additional methods)

# importing package
import numpy as np
x = np.floor(10*np.random.random((3,4)))
print(x)
#Return the maximum value in an array
print("x.max():", x.max())
# Return the minimum value in a array
print("x.min():", x.min())
# Return the index of max value in an array
print("x.argmax():", x.argmax())
# Return the index of min value in an array
print("x.argmin():", x.argmin())

Output

输出量

[[4. 0. 5. 2.] [8. 5. 9. 7.]
[9. 3. 5. 5.]]
x.max(): 9.0
x.min(): 0.0
x.argmax(): 6
x.argmin(): 1

翻译自: https://www.includehelp.com/python/numpy.aspx

python中 numpy

python中 numpy_Python中的Numpy相关推荐

  1. [转载] python中 numpy_Python中的Numpy

    参考链接: Python中的numpy.apply_over_axes python中 numpy Python中的Numpy是什么? (What is Numpy in Python?) Numpy ...

  2. python数据分析numpy_Python数据分析之numpy学习

    Python模块中的numpy,这是一个处理数组的强大模块,而该模块也是其他数据分析模块(如pandas和scipy)的核心. 接下面将从这5个方面来介绍numpy模块的内容: 1)数组的创建 2)有 ...

  3. python创建列向量_关于Numpy中的行向量和列向量详解

    关于Numpy中的行向量和列向量详解 行向量 方式1 import numpy as np b=np.array([1,2,3]).reshape((1,-1)) print(b,b.shape) 结 ...

  4. Python语言编程学习:numpy中的array格式数据切片与pandas中的dataframe格式数据切片、相互转换

    Python语言编程学习:numpy中的array格式数据切片与pandas中的dataframe格式数据切片.相互转换 目录 numpy中的array格式数据切片与pandas中的dataframe ...

  5. Python中出现:AttributeError: module 'numpy' has no attribute 'dtype'问题解决

    QUESTION:Python中出现:AttributeError: module 'numpy' has no attribute 'dtype'问题解决 ANWSER: 这个问题可是困扰了我一天的 ...

  6. python numpy和pandas数据处理_python中添加数据分析工具numpy和pandas

    python中添加数据分析工具numpy和pandas 最近要对一系列数据做同比比较,需要用到numpy和pandas来计算,不过使用python安装numpy和pandas因为linux环境没有外网 ...

  7. Python数据分析 找出数组中每行(或每列)中指定的百分位数 numpy.percentile()

    [小白从小学Python.C.Java] [Python-计算机等级考试二级] [Python-数据分析] Python数据分析 找出数组中每行(或每列) 中指定的百分位数 numpy.percent ...

  8. python 随机选择list或numpy.ndarray中n个元素

    python 随机选择list或numpy.ndarray中n个元素 1. 从一个list中随机选取一个元素 random.choice(data) import random data = ['a' ...

  9. python数据分析(四)——numpy中的nan和数据的填充

    系列文章: python数据分析(一)--numpy数组的创建 python数据分析(二)--numpy数组的计算 python数据分析(三)--numpy读取本地数据和索引 python数据分析(五 ...

最新文章

  1. 解决IE无法查看源文件问题
  2. 【junit】junit4单元测试eclipse
  3. Linux启动报错UNEXPECTED INCONSISTENCY解决方法
  4. 记一次ElasticSearch 更改 mapping 字段类型的过程
  5. python records库_Python Records库使用举例
  6. PostgreSQL在何处处理 sql查询之五十四
  7. springboot17 集成SpringSecurity
  8. python 函数积累
  9. js 处理 cookie的存储与删除
  10. 一些常用路由协议默认的AD值
  11. 关于用Sql Server 2008 搭建一个多评委多客户端的比赛打分平台的整体构想
  12. Atitit webservice发现机制 WS-Discovery标准的规范attilax总结
  13. 计算机网络原理大题汇总
  14. 二路归并排序和多路归并排序
  15. 无法通过百度联盟申请的常见原因
  16. 盘点前端开发常用的几款编辑器
  17. UE4学习笔记(3)——World Composition无缝拼接地图实现
  18. JS复制input内容
  19. 算式最大值 (思维题)
  20. Spring Data JPA REST Query QueryDSL

热门文章

  1. poj 2031Building a Space Station(几何判断+Kruskal最小生成树)
  2. android 绘图软件,安卓最强大的绘图软件 妙笔生花最新评测
  3. 信息量、熵、交叉熵、KL散度、JS散度杂谈
  4. css 向左白色箭头,带CSS的工具提示左侧的箭头
  5. python批量下载文件教程_Python抓包菜鸟教程:批量下载图片的方法,电脑和手机都能用...
  6. java中同时两人提交数据_如何一起发送JSON请求和发布表单数据请求?
  7. 问题 I: 连通块计数
  8. centos 6 安装zabbix 3.0
  9. 【转】unity地形插件T4M使用帮助
  10. Python调用微博API获取微博内容