文章目录
The difference between a Python list and a Numpy array
- Numpy gives you an enormous range of fast and efficient ways of creating arrays and manipulating numerical data inside them.
- Python list can contain different data types within a single list
- Numpy array of all the elements should be homogeneous.
Creation
Python list creation
Python list
list01 = [1.4,2,'3',[3,5,6],{2,3,4}]
print(list01)
print(type(list01))
name = 'HHVic'
print(list(name))
[1.4, 2, '3', [3, 5, 6], {2, 3, 4}]
<class 'list'>
['H', 'H', 'V', 'i', 'c']
Numpy array creation
Numpy
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
名称 | 描述 |
---|---|
object | Array or Nested array |
dtype | The data type of the array element, optional |
copy | The object needs to be copied, or not. optional |
order | Create array style, C for row, F for column, A for any direction(default) |
subok | Return an array consistent with the base class type by default |
ndmin | Specifies the minimum dimension of the array |
array01 = np.arange(1,10,2) #(start, stop, step, dtype)
print(array01)
print(type(array01))
name1 = 'HHVic'
print(np.array(name1))
[1 3 5 7 9]
<class 'numpy.ndarray'>
HHVic
Create 1 dimension and 2 dimension array
- Assignment directly
array1d = np.array([1,2,3,4])
print(array1d)
array2d= np.array([[1,2],[3,4],[5,6]])
print(array2d)
print(type(array2d))
[1 2 3 4]
[[1 2]
[3 4]
[5 6]]
<class 'numpy.ndarray'>
- USE reshape()
arrayreshape = np.arange(10).reshape(2,5,order ='C')
print(arrayreshape)
arrayreshape = np.arange(10).reshape(2,5,order ='F')
print(arrayreshape)
[[0 1 2 3 4]
[5 6 7 8 9]]
[[0 2 4 6 8]
[1 3 5 7 9]]
- Use randint()
arrayrandint = np.random.randint(0,10,(4,4))
print(arrayrandint)
[[3 5 6 1]
[8 7 7 0]
[6 8 3 9]
[0 2 3 7]]
More Numpy array functions
- Zeros
zeros= np.zeros(11, dtype = float, order ='C')
print(zeros)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
- Ones
ones = np.ones(10, dtype = float, order ='C')
print(ones)
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
- linspace()
linspaces = np.linspace(0,5,5)
print(linspaces)
[0. 1.25 2.5 3.75 5. ]
- random()
randoms = np.random.rand(2,3)
print(randoms)
randoms = np.random.randint(2,10,size=10) # produce 10 random number form 2 to 9
print(randoms)
randoms = np.random.randn(5)
print(randoms)
[[0.48395755 0.60356083 0.82227284]
[0.39616543 0.11712723 0.23434582]]
[5 7 4 9 3 8 6 4 3 7]
[ 0.74630555 0.8983705 0.96306982 1.63266755 -1.72612846]
- empty()
emptys = np.empty((2,5),dtype=int)
print(emptys)
[[ 727395495 1072161212 2064842652 1072480115 -857168134]
[1072615799 28942093 1073356648 -527735117 1073454648]]
Adding, Indexing , Assigning , Slicing, Removing
Python list
Python list
#Adding
list01 =[1.4, 2, 'SSS', '3', [3, 5, 6], {2, 3, 4}]
list01.append('abcd')
print(list01)
list01.insert(0,'ABCD')
print(list01)
list01.extend('extend1') #(sequence)
print(list01)
list01 =[1.4, 2, 'SSS', '3', [3, 5, 6], {2, 3, 4}, 'HHVic', 'H', 'H', 'V', 'i', 'c']
#Indexing and Slicing
print(list01[5])
print(list01[-1]) # The last element
print(list01[:-3])
#Assigning
list01[3]='Dream' #list[index] = elements
print(list01)
list01[1:3] = ('My','New') #list[slicing] = container
print(list01)
list02 = list01[3:9]
print(list02)
# Removing
del list01[8] #Remove the 8th index == 'H'
print(list01)
list01.remove('V') #(element)Remove the 1st element after search from first to end
print(list01)
list01.pop(9) # (index) Remove the value of the index, if no index, remove the last element
print(list01)
#list01.append('abcd')
[1.4, 2, 'SSS', '3', [3, 5, 6], {2, 3, 4}, 'abcd']
#list01.insert(0,'ABCD')
['ABCD', 1.4, 2, 'SSS', '3', [3, 5, 6], {2, 3, 4}, 'abcd']
#list01.extend('extend1')
['ABCD', 1.4, 2, 'SSS', '3', [3, 5, 6], {2, 3, 4}, 'abcd', 'e', 'x', 't', 'e', 'n', 'd', '1']
#print(list01[5])
{2, 3, 4}
#print(list01[-1])
c
#print(list01[:-3])
[1.4, 2, 'SSS', '3', [3, 5, 6], {2, 3, 4}, 'HHVic', 'H', 'H']
#list01[3]='Dream'
[1.4, 2, 'SSS', 'Dream', [3, 5, 6], {2, 3, 4}, 'HHVic', 'H', 'H', 'V', 'i', 'c']
#list01[1:3] = ('My','New')
[1.4, 'My', 'New', 'Dream', [3, 5, 6], {2, 3, 4}, 'HHVic', 'H', 'H', 'V', 'i', 'c']
#list02 = list01[3:9]
['Dream', [3, 5, 6], {2, 3, 4}, 'HHVic', 'H', 'H']
#del list01[8]
[1.4, 'My', 'New', 'Dream', [3, 5, 6], {2, 3, 4}, 'HHVic', 'H', 'V', 'i', 'c']
#list01.remove('V')
[1.4, 'My', 'New', 'Dream', [3, 5, 6], {2, 3, 4}, 'HHVic', 'H', 'i', 'c']
#list01.pop(9)
[1.4, 'My', 'New', 'Dream', [3, 5, 6], {2, 3, 4}, 'HHVic', 'H', 'i']
Numpy array
Numpy array
Adding
#Adding
#numpy.append(arr, values, axis=None)
array01 = np.array([[1,2,3],[4,5,6]])
print(array01)
array02 = np.append(array01,[[7,8,9]]) # array will be replaced to 1d array
print(array02)
print(type(array02))
array03 = np.append(array01,[[7,8,9]],axis=0) # axis =0 add to row
print(array03)
array04 = np.append(array01,[[5,5,5],[7,8,9]],axis=1) # axis =1 add to column
print(array04)
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6 7 8 9]
<class 'numpy.ndarray'>
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 3 5 5 5]
[4 5 6 7 8 9]]
#numpy.insert(arr, obj, values, axis)
array01 = np.array([[1,2,3],[4,5,6]])
print(array01)
array02 = np.insert(array01,2,[7,8,9]) #insert to the location of index 2
print(array02)
array03 = np.insert(array01,2,[7,8,9],axis=0)
print(array03)
array04 = np.insert(array01,2,[7,8],axis=1)
print(array04)
[[1 2 3]
[4 5 6]]
[1 2 7 8 9 3 4 5 6]
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 7 3]
[4 5 8 6]]
Indexing and slicing (1 dimension)
# 1 dimension indexing and slicing
array1d = np.arange(1,8)
print(array1d) # [1 2 3 4 5 6 7]
print(array1d[5:]) #6 7
print(array1d[:5]) #1 2 3 4 5
print(array1d[:-5:-1]) #7 6 5 4
print(array1d[-5::-1]) #3 2 1
print(array1d[::]) #1 2 3 4 5 6 7
print(array1d[:]) #1 2 3 4 5 6 7
Indexing and slicing (multiple dimensions)
#array[page,row_index,column_index]
array3d = np.arange(24).reshape(2,3,4)
print(array3d)
print('[1,:,:]:\n', array3d[1,:,:]) # all elements in page 1
print('[:,1,:]:\n', array3d[:,1,:]) # all 1st rows in all pages
print('[1,:,1]:\n', array3d[1,:,1]) # the 1st columns in page 1
print('*'*80)
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
[1,:,:]:
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
[:,1,:]:
[[ 4 5 6 7]
[16 17 18 19]]
[1,:,1]:
[13 17 21]
********************************************************************************
Assigning
array3d = np.arange(1,9).reshape(2,2,2)
print(array3d)
array3d[1,:,:] = 0
print(array3d)
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
[[[1 2]
[3 4]]
[[0 0]
[0 0]]]
Use condition
array3d = np.arange(1,9).reshape(2,2,2)
print(array3d)
array3d[array3d < 5] = 0
print(array3d)
[[[0 0]
[0 0]]
[[5 6]
[7 8]]]
Removing and deleting duplicate elements
numpy.delete(arr, obj, axis)
array3d = np.arange(1,9).reshape(2,2,2)
print(array3d)
array3d_del= np.delete(array3d,1,axis=0)
print(array3d_del)
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
[[[1 2]
[3 4]]]
numpy.unique(arr, return_index, return_inverse, return_counts)
1-Dimension
array1d = np.arange(10)
array1d = np.append(array1d,[5,6,7,8,9])
print(array1d)
array1d = np.unique(array1d)
print(array1d)
[0 1 2 3 4 5 6 7 8 9 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
Multiple Dimension
array1d = np.array([3,4,7,3,8,2,6,3,4,7,9,1,5,8,2])
print('array1d:',array1d)
array1dnew = np.unique(array1d)
print('array1d_unique:',array1dnew)
a1, indices_a1 = np.unique(array1d, return_index=True)
print(a1) # print the unique array, and sorted from small to big
print(indices_a1) # put the old unique elements' index into a array.
a2, indices_a2 = np.unique(array1d, return_inverse=True)
print(a2) # same as a1
print(indices_a2) # get the index of all old elements and sorted one bye one
a3, indices_a3 = np.unique(array1d, return_counts=True)
print(a3) # same as a1
print(indices_a3)
array1d: [3 4 7 3 8 2 6 3 4 7 9 1 5 8 2]
array1d_unique: [1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[11 5 0 1 12 6 2 4 10]
[1 2 3 4 5 6 7 8 9]
[2 3 6 2 7 1 5 2 3 6 8 0 4 7 1]
[1 2 3 4 5 6 7 8 9]
[1 2 3 2 1 1 2 2 1]