Home > Backend Development > Python Tutorial > Basic learning of numpy in python and performing array and vector calculations

Basic learning of numpy in python and performing array and vector calculations

高洛峰
Release: 2017-02-14 13:28:33
Original
1385 people have browsed it

Preface

In python, sometimes we use arrays to operate data, which can greatly improve the data processing efficiency. Similar to R's vectorization operation, it is the trend of data operation. For simplicity, array and vector calculations can be performed using the numpy module in python.

Let’s take a look at a simple example


import numpy as np
 
data=np.array([2,5,6,8,3]) #构造一个简单的数组
 
print(data)
Copy after login


Result:


[2 5 6 8 3]
Copy after login



data1=np.array([[2,5,6,8,3],np.arange(5)]) #构建一个二维数组
 
print(data1)
Copy after login


Result:


[[2 5 6 8 3]
[0 1 2 3 4]]
Copy after login


We can also view the dimensions and data format of the array through the shape and dtype methods


print(data.shape)
print(data.dtype)
print(data1.shape)
print(data1.dtype)
Copy after login


Result:


(5,)
int32

(2, 5)
int32
Copy after login


It can be seen that data is a one-dimensional array, with 5 elements in each group. The data type is 32-bit int type

data1 is a two-dimensional array, each group has 5 elements, the data type is 32-bit int type

A better way to distinguish is to look at the print In the result, the number and position of the square brackets can indicate the dimensions of the array. One layer of square brackets represents a dimension.

Other array attribute methods include:

array.ndim The dimension of the array, the result of a one-dimensional array is 1, and the result of a two-dimensional array is 1 The print result is 2

array.size The number of elements in the array

array.itemsiz The byte size of each element in the array

Next let’s learn about the data types in the array:

Basic data types in NumPy


##int32Integer, -2 ** 31 to 2 ** 32 -1int64Integer, -2 ** 63 to 2 ** 63 - 1uint8Unsigned integer, 0 to 255uint16Unsigned integer, 0 to 255 65535uint32Unsigned integer, 0 to 2 ** 32 - 1uint64Unsigned integer, 0 to 2 ** 64 - 1float16Half-precision floating point number: 16 bits, 1 bit for sign, 5 bits for exponent, Precision 10 digitsfloat32Single precision floating point number: 32 digits, 1 sign, 8 exponent, 23 digits precisionfloat64 or floatDouble precision floating point number: 64 bits, 1 bit for sign, 11 bits for exponent, 52 bits for precisioncomplex64Complex numbers, use two 32-bit floating point numbers to represent the real part and imaginary part respectivelycomplex128 or complexComplex numbers, use two 64-bit floating point numbers respectively Points represent real and imaginary parts

基础的数组运算

数组也可以进行我们常用的加减乘除运算


arr=np.array(np.arange(10))
arr1=np.array(np.arange(1,11))
print(arr*2)
Copy after login


结果:


[ 0 2 4 6 8 10 12 14 16 18]
Copy after login



print(arr+arr1)
Copy after login


结果:


[ 1 3 5 7 9 11 13 15 17 19]
Copy after login


注意,相加两个数组长度要一样

接下来我们看下数组索引


arr=np.arange(10)
Copy after login


用下标直接进行索引


print(arr[5])
Copy after login


结果为:


5
Copy after login


切片索引


print(arr[5:8])
Copy after login


结果为:


[5 6 7]
Copy after login


可以利用索引对数据进行更改操作


arr[5]=120
print(arr)
Copy after login


结果为:


[ 0 1 2 3 4 120 6 7 8 9]
Copy after login


可以看到下标为5的数已经变成120了。

此外,数组还可以进行布尔操作


arr=np.arange(5)
name=np.array(['a','b','b','c','a'])
print(name=='a')
Copy after login


结果为:


[ True False False False True]
Copy after login


即满足条件的数据全部以True的结果输出。

接下来我们可以利用name数组设置条件后的布尔值对arr数组进行相关操作


print(arr[name=='a'])
Copy after login


结果为:


[0 4]
Copy after login


即把arr中对应于name中a相对应位置的元素打印出来。

多条件操作


result=(name='a')|(name='c')
print(result)
print(name[result])
Copy after login


结果为:


[ True False False True True]
['a' 'c' 'a']
Copy after login


接下来,我们了解下ufunc方法

用于操作单个数组的函数有如下:

用于操作两个或多个数组的方法

相关的函数方法使用

np.meshgrid 用于生成多维矩阵


a,b=np.meshgrid(np.arange(1,5),np.arange(2,4))
print(a)
print(b)
Copy after login


结果为:


[[1 2 3 4]
[1 2 3 4]]
[[2 2 2 2]
[3 3 3 3]]
Copy after login


按照数据最少的数组形成数组

np.where 是三元表达式 x if condition else y的矢量化版本


arr1=np.arange(5)
arr2=np.arange(20,25)
condition=np.array([1,0,1,0,0])
result=np.where(condition,arr1,arr2)
print(arr1)
print(arr2)
print(result)
Copy after login


结果为:


[0 1 2 3 4]
[20 21 22 23 24]
[ 0 21 2 23 24]
Copy after login


可以看出,result的结果中,条件为1的显示数组arr1的内容,条件为0的显示arr2的内容

数学统计方法

在数组中我们也可以使用数学统计方法进行计数,例如sum mean std 等


arr=np.random.randint(1,20,10)
print(arr)
print(np.mean(arr))
print(np.sum(arr))
print(np.std(arr))
Copy after login


结果为:


[19 14 8 13 13 10 10 9 19 7]
12.2
122
4.01995024845
Copy after login


具体的方法内容如下图所示:

布尔型数组的相关统计方法


arr=np.arange(-20,10)
result=(arr>5).sum()
print(arr)
print(result)
Copy after login


结果为:


-20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3
-2 -1 0 1 2 3 4 5 6 7 8 9]

4
Copy after login


可以对数据进行判断后进行个数求和

其他的数组方法还有

数据的读取和存储

线性函数的常用方法


arr=np.array([np.random.randint(1,10,5),np.random.randint(10,20,5)])
print(arr)
print(np.dot(arr,2))
Copy after login


结果为


[[ 4 6 5 1 6]
[14 16 11 10 18]]
[[ 8 12 10 2 12]
[28 32 22 20 36]]
Copy after login


dot方法可以进行矩阵相乘操作

其他方法如下图

最后我们了解下numpy中的随机数生成方法

上面的很多例子中我们已经用到了随机数生成,


arr=np.random.random(10)
print(arr)
Copy after login


结果为


[ 0.90051063 0.72818635 0.00411373 0.13154345 0.45513344 0.9700776
0.42150977 0.27728599 0.50888291 0.62288808]
Copy after login


其他形式的随机数生成方法

更多python中numpy基础学习及进行数组和矢量计算相关文章请关注PHP中文网!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
NameDescription
boolBoolean type (True or False) stored in one byte
intiAn integer whose size is determined by the platform (usually int32 or int64)
int8One Byte size, -128 to 127
int16Integer, -32768 to 32767