程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python data analysis] common operations of numpy (I)

編輯:Python

One 、 Import Numpy

# Import numpy
import numpy as np

Two 、 One dimension and multidimensional array

1. Create array ( A one-dimensional 、 Multidimensional )

# Create array array
data1=[6,7,8]
np.array(data1) # Create a one-dimensional array 

array([6, 7, 8])

# Create multidimensional arrays 
data2=[1,2,3,4],[5,6,7,8]
arr1=np.array(data2)
print(arr1)

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

3、 ... and . Data type and type conversion

#dtype data type 
arr1.dtype

dtype(‘int32’)

arr1.shape #shape attribute 

(2, 4)

arr2=np.array(data2,dtype=np.int64)
print(arr2)
print(arr2.dtype)

[[1 2 3 4]
[5 6 7 8]]
int64

#float
data3=[6,7.5,8,9]
arr3=np.array(data3)
print(arr3)
print(arr3.dtype)

[6. 7.5 8. 9. ]
float64

# astype Type conversion 
arr3.astype(np.int64)

array([6, 7, 8, 9], dtype=int64)

# ones Generate all 1 Array of 
np.ones(10)

array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

#zeros Generate all 0 Array of 
np.zeros(10)

array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

# Generate multidimensional arrays , An array of three rows and six columns 
np.zeros((3,6))

array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])

3、 ... and 、 Operations between arrays and scalars

nd=np.array([1,2,3,4])
print(nd)

[1 2 3 4]

nd*2

array([2, 4, 6, 8])


nd*nd

array([ 1, 4, 9, 16])

nd+nd

array([2, 4, 6, 8])

# Take the bottom 
1/nd

array([1. , 0.5 , 0.33333333, 0.25 ])

Four 、 Basic indexing and slicing

# (1)arage Method :range Array version of function 
np.arange(10)

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

nd1=np.arange(1,10,2) # The starting value of the array is 1, The key for 10, In steps of 2
print(nd1)

[1 3 5 7 9]

# (2) Get some elements by index 
print(nd1[3])

7

print(nd1[2:5]) # The obtained index is 2,3,4, barring 5

[5 7 9]

# (3) Assign values by index 
nd1[2:5]=10
print(nd1)

[ 1 3 10 10 10]

# Multidimensional arrays 
data2=[[1,2,3,4],[5,6,7,8]]
nd=np.array(data2)
print(nd)

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

nd[0] # Representative bank index , The first row of a two-dimensional array 

array([1, 2, 3, 4])

nd[1]

array([5, 6, 7, 8])

nd[0,3] # That's the first row , The elements of the fourth column 

4

5、 ... and 、 Fancy index

The fancy index is a Numpy The term , Refers to the use of an array of integers for indexing

#(1)reshape
nd=np.arange(32)
print(nd)

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31]

# reshape
nd1=np.reshape(nd,(8,4))
print (nd1)

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]
[24 25 26 27]
[28 29 30 31]]

# General index 
nd1[0]

array([0, 1, 2, 3])

#(2) Select a specific subset of rows 
# Fancy index , Pass in an array 
# Get four lines , Each number represents a line 
nd1[[4,6,3,1]]

array([[16, 17, 18, 19],
[24, 25, 26, 27],
[12, 13, 14, 15],
[ 4, 5, 6, 7]])

nd1[[1,5,7,2]]

array([[ 4, 5, 6, 7],
[20, 21, 22, 23],
[28, 29, 30, 31],
[ 8, 9, 10, 11]])

#(3) Select multiple rows 、 Elements at the intersection of multiple columns 
nd1[[1,5,7,2],[0,3,1,2]] # The first value is row index 1, The column index is 0 The elements of , And so on . What you actually get is some points 

array([ 4, 23, 29, 10])

#(4) There are two ways to get a rectangular area 
# Method 1 :
nd1[[1,5,7,2]] [:,[0,3,1,2]]

array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])

# Method 2 :np.ix_
nd1[np.ix_([1,5,7,2],[0,3,1,2])]

array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])

6、 ... and 、 The generic function (ufunc)

# ufunc
arr=np.arange(10)
print(arr)

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

np.sqrt(arr) # Square root 

array([0. , 1. , 1.41421356, 1.73205081, 2. ,
2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])

B=np.arange(3)
print(B)

[0 1 2]

C=np.array([2,-1,4])
print(C)

[ 2 -1 4]

np.add(B,C)

array([2, 0, 6])

#maximum
np.maximum(B,C) # take B,C The same one index Maximum of ,

array([2, 1, 4])


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved