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

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

高洛峰
高洛峰Original
2017-02-14 13:28:331351browse

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)


Result:


[2 5 6 8 3]



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


Result:


[[2 5 6 8 3]
[0 1 2 3 4]]


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)


Result:


(5,)
int32

(2, 5)
int32


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)


结果:


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



print(arr+arr1)


结果:


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


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

接下来我们看下数组索引


arr=np.arange(10)


用下标直接进行索引


print(arr[5])


结果为:


5


切片索引


print(arr[5:8])


结果为:


[5 6 7]


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


arr[5]=120
print(arr)


结果为:


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


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

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


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


结果为:


[ True False False False True]


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

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


print(arr[name=='a'])


结果为:


[0 4]


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

多条件操作


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


结果为:


[ True False False True True]
['a' 'c' 'a']


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

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

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

相关的函数方法使用

np.meshgrid 用于生成多维矩阵


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


结果为:


[[1 2 3 4]
[1 2 3 4]]
[[2 2 2 2]
[3 3 3 3]]


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

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)


结果为:


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


可以看出,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))


结果为:


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


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

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


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


结果为:


-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


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

其他的数组方法还有

数据的读取和存储

 

线性函数的常用方法


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


结果为


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


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

其他方法如下图

 

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

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


arr=np.random.random(10)
print(arr)


结果为


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


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

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


Name Description
bool Boolean type (True or False) stored in one byte
inti An integer whose size is determined by the platform (usually int32 or int64)
int8 One Byte size, -128 to 127
int16 Integer, -32768 to 32767
Statement:
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