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

Using Python data analysis -- numpy Foundation: array and vector calculation

編輯:Python

List of articles

  • 0 Write it at the front
  • 1 ndarray: A multidimensional array object
    • 1.1 numpy.random.randn(d0,d1, ...,dn) Function introduction
    • 1.2 numpy.random.seed() Function introduction
    • 1.3 establish ndarray
      • 1.3.1 np.array()
      • 1.3.2 np.zeros() and np.ones()
      • 1.3.3 np.empty()
      • 1.3.4 np.arange()
      • 1.3.5 Function summary
    • 1.4 ndarray Data type of
      • 1.4.1 np.astype()
  • 2 NumPy Operation of array
    • 2.1 Basic indexing and slicing
    • 2.2 Slice indices
  • 3 Boolean index
  • 4 Fancy index
  • 5 Array transpose and axis swap

0 Write it at the front

  1. ndarray, It is a fast system with vector arithmetic operation and complex broadcasting capability , And space saving multidimensional array ;
  2. Figure out the specific performance gap , Consider an array of one million integers , And an equivalent Python list .
  3. ndarray, Is a generic isomorphic data multidimensional container , in other words , All elements must be of the same type . Every array has a shape( A tuple representing the size of each dimension ) And a dtype( An object that specifies the array data type )

import numpy as np
my_arr = np.arange(1000000)
my_list = list(range(1000000))
%time for _ in range(10): my_arr2 = my_arr * 2
Wall time: 21.7 ms
%time for _ in range(10): my_list2 = [x * 2 for x in my_list]
Wall time: 855 ms
  • be based on NumPy The algorithm is more than pure Python fast 10 To 100 times ( Even faster ), And it USES less memory .

1 ndarray: A multidimensional array object

1.1 numpy.random.randn(d0,d1, …,dn) Function introduction

  • rand() function , According to the specified dimension , Generate [0,1) Data between ;
  • dn Represents each dimension
  • The return value is... Of the specified dimension array

Code :

import numpy as np
data = np.random.randn(2, 3)
print(data)

Output :

[[-0.98166625 -0.75308133 1.02950877]
[ 0.92072303 -0.98693389 -0.68642432]]

1.2 numpy.random.seed() Function introduction

  • Make random data predictable
  • When setting the same seed when , Then the random number generated each time is the same ; If not set seed, Then a different random number will be generated each time .

1.3 establish ndarray

1.3.1 np.array()

  • The easiest way to create an array is to use array function . It accepts all sequential objects ( Including other arrays ), And then return a NumPy Array .
  • Take, for example, the transformation of a list :
In [19]: data1 = [6, 7.5, 8, 0, 1]
In [20]: arr1 = np.array(data1)
In [21]: arr1
Out[21]: array([ 6. , 7.5, 8. , 0. , 1. ])
  • Nested sequence ( For example, a list that consists of a set of equally long lists ) Will be converted to a multidimensional array :
In [22]: data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
In [23]: arr2 = np.array(data2)
In [24]: arr2
Out[24]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
  • Numpy Array has two properties :ndim and shape
In [22]: data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
In [23]: arr2 = np.array(data2)
In [24]: arr2
Out[24]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [25]: arr2.ndim
Out[25]: 2
In [26]: arr2.shape
Out[26]: (2, 4)

1.3.2 np.zeros() and np.ones()

  • What needs to be passed in is , Create the specified length or shape of the array .
In [22]: data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
In [23]: arr2 = np.array(data2)
In [24]: arr2
Out[24]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])

1.3.3 np.empty()

  • empty You can create an array without any specific values , Therefore, you only need to pass in a tuple representing the shape .
In [31]: np.empty((2, 3, 2))
Out[31]:
array([[[ 0., 0.],
[ 0., 0.],
[ 0., 0.]],
[[ 0., 0.],
[ 0., 0.],
[ 0., 0.]]])

Be careful : Think np.empty Returns the full 0 The idea of arrays is not safe . In many cases ( As shown before ), It returns some uninitialized garbage value .

1.3.4 np.arange()

  • arange yes Python Built in functions range An array of version :
In [32]: np.arange(15)
Out[32]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

1.3.5 Function summary

because NumPy The focus is on numerical calculations , therefore , If not specified , The data types are basically the same float64( Floating point numbers ).

1.4 ndarray Data type of

I said before. ,ndarray The default is float64

But you can use dtype Make a designation :

In [33]: arr1 = np.array([1, 2, 3], dtype=np.float64)
In [34]: arr2 = np.array([1, 2, 3], dtype=np.int32)
In [35]: arr1.dtype
Out[35]: dtype('float64')
In [36]: arr2.dtype
Out[36]: dtype('int32')

1.4.1 np.astype()

  • You can go through ndarray Of astype Method explicitly takes an array from one dtype Convert to another one dtype:
In [37]: arr = np.array([1, 2, 3, 4, 5])
In [38]: arr.dtype
Out[38]: dtype('int64')
In [39]: float_arr = arr.astype(np.float64)
In [40]: float_arr.dtype
Out[40]: dtype('float64')
  • If you convert a floating point number to an integer , The decimal part will be truncated and deleted :
In [41]: arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
In [42]: arr
Out[42]: array([ 3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
In [43]: arr.astype(np.int32)
Out[43]: array([ 3, -1, -2, 0, 12, 10], dtype=int32)

2 NumPy Operation of array

  • Arrays are important , Because it makes you No need to write a loop , You can also perform batch operations on data .

  • Arrays of equal size , Any arithmetic operation between , Will be applied to the element level .

In [51]: arr = np.array([[1., 2., 3.], [4., 5., 6.]])
In [52]: arr
Out[52]:
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
In [53]: arr * arr
Out[53]:
array([[ 1., 4., 9.],
[ 16., 25., 36.]])
In [54]: arr - arr
Out[54]:
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
  • Array and scalar arithmetic operations , Scalar values are propagated to individual elements :
In [55]: 1 / arr
Out[55]:
array([[ 1. , 0.5 , 0.3333],
[ 0.25 , 0.2 , 0.1667]])
In [56]: arr ** 0.5
Out[56]:
array([[ 1. , 1.4142, 1.7321],
[ 2. , 2.2361, 2.4495]])
  • Comparisons between arrays of the same size generate Boolean arrays :
In [57]: arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])
In [58]: arr2
Out[58]:
array([[ 0., 4., 1.],
[ 7., 2., 12.]])
In [59]: arr2 > arr
Out[59]:
array([[False, True, False],
[ True, False, True]], dtype=bool)

2.1 Basic indexing and slicing

  • Simple usage is as follows :

You can find , If you assign a scalar to a slice , Then the slice content of this paragraph will change ;
And the array slice is the original array view , Any changes on the view Will be reflected in the original array On .

In [60]: arr = np.arange(10)
In [61]: arr
Out[61]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [62]: arr[5]
Out[62]: 5
In [63]: arr[5:8]
Out[63]: array([5, 6, 7])
In [64]: arr[5:8] = 12
In [65]: arr
Out[65]: array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])

If you don't want to operate on the original array , You must clearly perform the copy operation . Such as :arr[5:8].copy()

  • In a two-dimensional array , The elements at each index position are no longer scalars but one-dimensional arrays .
In [72]: arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [73]: arr2d[2]
Out[73]: array([7, 8, 9])
  • You can pass in a comma separated list of indexes to select individual elements :
In [74]: arr2d[0][2]
Out[74]: 3
In [75]: arr2d[0, 2]
Out[75]: 3

2.2 Slice indices

  • ndarry Slicing syntax for , Follow Python A one-dimensional array of lists :
  • Be careful arr[1:6] It's left closed and right open .
In [88]: arr
Out[88]: array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])
In [89]: arr[1:6]
Out[89]: array([ 1, 2, 3, 4, 64])
  • For the previous two dimensional array arr2d, The slicing method is slightly different :
  • It can be seen that , Here is along the 0 Axis sliced , That is to say, select arr2d The first two lines .
In [90]: arr2d
Out[90]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [91]: arr2d[:2]
Out[91]:
array([[1, 2, 3],
[4, 5, 6]])
  • Pass in multiple slices :
In [92]: arr2d[:2, 1:]
Out[92]:
array([[2, 3],
[5, 6]])
  • Mix integer indexes and slices , You can get slices of lower dimensions :
In [93]: arr2d[1, :2]
Out[93]: array([4, 5])
In [94]: arr2d[:2, 2]
Out[94]: array([3, 6])
  • Only The colon : I'm picking the whole axis .
In [95]: arr2d[:, :1]
Out[95]:
array([[1],
[4],
[7]])

3 Boolean index

  • Example : First define an array to store names (7 A name ), Then initialize a 7 Array of rows , Each line represents a name .
In [98]: names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
In [99]: data = np.random.randn(7, 4)
In [100]: names
Out[100]:
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'],
dtype='<U4')
In [101]: data
Out[101]:
array([[ 0.0929, 0.2817, 0.769 , 1.2464],
[ 1.0072, -1.2962, 0.275 , 0.2289],
[ 1.3529, 0.8864, -2.0016, -0.3718],
[ 1.669 , -0.4386, -0.5397, 0.477 ],
[ 3.2489, -1.0212, -0.5771, 0.1241],
[ 0.3026, 0.5238, 0.0009, 1.3438],
[-0.7135, -0.8312, -2.3702, -1.8608]])
  • Now the need is , according to Bob Corresponding to the index in the array , Select the data of the corresponding row of the corresponding random data .
  • You can see ,names == 'Bob' Will generate a bool An array of types .
In [102]: names == 'Bob'
Out[102]: array([ True, False, False, True, False, False, False], dtype=bool)
  • The code is as follows :
In [103]: data[names == 'Bob']
Out[103]:
array([[ 0.0929, 0.2817, 0.769 , 1.2464],
[ 1.669 , -0.4386, -0.5397, 0.477 ]])
  • Selected names == 'Bob’ The line of , And indexed the columns :
In [104]: data[names == 'Bob', 2:]
Out[104]:
array([[ 0.769 , 1.2464],
[-0.5397, 0.477 ]])
In [105]: data[names == 'Bob', 3]
Out[105]: array([ 1.2464, 0.477 ])
  • Choosing two of these three names requires applying multiple Boolean conditions in combination , Use &( and )、|( or ) A Boolean arithmetic operator such as :
In [110]: mask = (names == 'Bob') | (names == 'Will')
In [111]: mask
Out[111]: array([ True, False, True, True, True, False, False], dtype=bool)
In [112]: data[mask]
Out[112]:
array([[ 0.0929, 0.2817, 0.769 , 1.2464],
[ 1.3529, 0.8864, -2.0016, -0.3718],
[ 1.669 , -0.4386, -0.5397, 0.477 ],
[ 3.2489, -1.0212, -0.5771, 0.1241]])
  • Setting values through Boolean arrays is a frequently used technique . In order to data All the negative values in 0, We only need :
In [113]: data[data < 0] = 0
In [114]: data
Out[114]:
array([[ 0.0929, 0.2817, 0.769 , 1.2464],
[ 1.0072, 0. , 0.275 , 0.2289],
[ 1.3529, 0.8864, 0. , 0. ],
[ 1.669 , 0. , 0. , 0.477 ],
[ 3.2489, 0. , 0. , 0.1241],
[ 0.3026, 0.5238, 0.0009, 1.3438],
[ 0. , 0. , 0. , 0. ]])
  • Set... Through a one-dimensional Boolean array The value of an entire row or column is also very simple :
In [115]: data[names != 'Joe'] = 7
In [116]: data
Out[116]:
array([[ 7. , 7. , 7. , 7. ],
[ 1.0072, 0. , 0.275 , 0.2289],
[ 7. , 7. , 7. , 7. ],
[ 7. , 7. , 7. , 7. ],
[ 7. , 7. , 7. , 7. ],
[ 0.3026, 0.5238, 0.0009, 1.3438],
[ 0. , 0. , 0. , 0. ]])

4 Fancy index

Fancy index , Is to use the passed in integer array for indexing .

In [117]: arr = np.empty((8, 4))
In [118]: for i in range(8):
.....: arr[i] = i
In [119]: arr
Out[119]:
array([[ 0., 0., 0., 0.],
[ 1., 1., 1., 1.],
[ 2., 2., 2., 2.],
[ 3., 3., 3., 3.],
[ 4., 4., 4., 4.],
[ 5., 5., 5., 5.],
[ 6., 6., 6., 6.],
[ 7., 7., 7., 7.]])
  • To select a subset of rows in a particular order , Simply pass in a list of integers or integers to specify the order ndarray that will do :
In [120]: arr[[4, 3, 0, 6]]
Out[120]:
array([[ 4., 4., 4., 4.],
[ 3., 3., 3., 3.],
[ 0., 0., 0., 0.],
[ 6., 6., 6., 6.]])
  • Using a negative index will pick a row from the end :
In [121]: arr[[-3, -5, -7]]
Out[121]:
array([[ 5., 5., 5., 5.],
[ 3., 3., 3., 3.],
[ 1., 1., 1., 1.]])
  • If you pass in multiple index arrays , It returns a one-dimensional array .

  • I'm going to pick the element (1,0)、(5,3)、(7,1) and (2,2). No matter how many dimensions the array has , Fancy indexes are always one-dimensional .

In [122]: arr = np.arange(32).reshape((8, 4))
In [123]: arr
Out[123]:
array([[ 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]])
In [124]: arr[[1, 5, 7, 2], [0, 3, 1, 2]]
Out[124]: array([ 4, 23, 29, 10])

5 Array transpose and axis swap

  • Transpose returns a view of the source data .
  • Not only does the array have transpose Method , There's a special one T attribute .
In [126]: arr = np.arange(15).reshape((3, 5))
In [127]: arr
Out[127]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
In [128]: arr.T
Out[128]:
array([[ 0, 5, 10],
[ 1, 6, 11],
[ 2, 7, 12],
[ 3, 8, 13],
[ 4, 9, 14]])
  • When you do matrix calculations , This operation is often required . For example, using np.dot Compute the inner product of a matrix .
In [129]: arr = np.random.randn(6, 3)
In [130]: arr
Out[130]:
array([[-0.8608, 0.5601, -1.2659],
[ 0.1198, -1.0635, 0.3329],
[-2.3594, -0.1995, -1.542 ],
[-0.9707, -1.307 , 0.2863],
[ 0.378 , -0.7539, 0.3313],
[ 1.3497, 0.0699, 0.2467]])
In [131]: np.dot(arr.T, arr)
Out[131]:
array([[ 9.2291, 0.9394, 4.948 ],
[ 0.9394, 3.7662, -1.3622],
[ 4.948 , -1.3622, 4.3437]])
  • Simple transpose can be used .T, It's just an axis swap .
  • ndarray One more swapaxes Method , It needs to accept a pair of axis Numbers . It is also a view of the returned source data .
In [135]: arr
Out[135]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
In [136]: arr.swapaxes(1, 2)
Out[136]:
array([[[ 0, 4],
[ 1, 5],
[ 2, 6],
[ 3, 7]],
[[ 8, 12],
[ 9, 13],
[10, 14],
[11, 15]]])

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