SlideShare a Scribd company logo
2
PythonForDataScience Cheat Sheet
NumPy Basics
Learn Python for Data Science Interactively at www.DataCamp.com
NumPy
DataCamp
Learn Python for Data Science Interactively
The NumPy library is the core library for scientific computing in
Python. It provides a high-performance multidimensional array
object, and tools for working with these arrays.
>>> import numpy as np
Use the following import convention:
Creating Arrays
>>> np.zeros((3,4)) Create an array of zeros
>>> np.ones((2,3,4),dtype=np.int16) Create an array of ones
>>> d = np.arange(10,25,5) Create an array of evenly
spaced values (step value)
>>> np.linspace(0,2,9) Create an array of evenly
spaced values (number of samples)
>>> e = np.full((2,2),7) Create a constant array
>>> f = np.eye(2) Create a 2X2 identity matrix
>>> np.random.random((2,2)) Create an array with random values
>>> np.empty((3,2)) Create an empty array
Array Mathematics
>>> g = a - b Subtraction
array([[-0.5, 0. , 0. ],
[-3. , -3. , -3. ]])
>>> np.subtract(a,b) Subtraction
>>> b + a Addition
array([[ 2.5, 4. , 6. ],
[ 5. , 7. , 9. ]])
>>> np.add(b,a) Addition
>>> a / b Division
array([[ 0.66666667, 1. , 1. ],
[ 0.25 , 0.4 , 0.5 ]])
>>> np.divide(a,b) Division
>>> a * b Multiplication
array([[ 1.5, 4. , 9. ],
[ 4. , 10. , 18. ]])
>>> np.multiply(a,b) Multiplication
>>> np.exp(b) Exponentiation
>>> np.sqrt(b) Square root
>>> np.sin(a) Print sines of an array
>>> np.cos(b) Element-wise cosine
>>> np.log(a) Element-wise natural logarithm
>>> e.dot(f) Dot product
array([[ 7., 7.],
[ 7., 7.]])
Subsetting, Slicing, Indexing
>>> a.sum() Array-wise sum
>>> a.min() Array-wise minimum value
>>> b.max(axis=0) Maximum value of an array row
>>> b.cumsum(axis=1) Cumulative sum of the elements
>>> a.mean() Mean
>>> b.median() Median
>>> a.corrcoef() Correlation coefficient
>>> np.std(b) Standard deviation
Comparison
>>> a == b Element-wise comparison
array([[False, True, True],
[False, False, False]], dtype=bool)
>>> a < 2 Element-wise comparison
array([True, False, False], dtype=bool)
>>> np.array_equal(a, b) Array-wise comparison
1 2 3
1D array 2D array 3D array
1.5 2 3
4 5 6
Array Manipulation
NumPy Arrays
axis 0
axis 1
axis 0
axis 1
axis 2
Arithmetic Operations
Transposing Array
>>> i = np.transpose(b) Permute array dimensions
>>> i.T Permute array dimensions
Changing Array Shape
>>> b.ravel() Flatten the array
>>> g.reshape(3,-2) Reshape, but don’t change data
Adding/Removing Elements
>>> h.resize((2,6)) Return a new array with shape (2,6)
>>> np.append(h,g) Append items to an array
>>> np.insert(a, 1, 5) Insert items in an array
>>> np.delete(a,[1]) Delete items from an array
Combining Arrays
>>> np.concatenate((a,d),axis=0) Concatenate arrays
array([ 1, 2, 3, 10, 15, 20])
>>> np.vstack((a,b)) Stack arrays vertically (row-wise)
array([[ 1. , 2. , 3. ],
[ 1.5, 2. , 3. ],
[ 4. , 5. , 6. ]])
>>> np.r_[e,f] Stack arrays vertically (row-wise)
>>> np.hstack((e,f)) Stack arrays horizontally (column-wise)
array([[ 7., 7., 1., 0.],
[ 7., 7., 0., 1.]])
>>> np.column_stack((a,d)) Create stacked column-wise arrays
array([[ 1, 10],
[ 2, 15],
[ 3, 20]])
>>> np.c_[a,d] Create stacked column-wise arrays
Splitting Arrays
>>> np.hsplit(a,3) Split the array horizontally at the 3rd
[array([1]),array([2]),array([3])] index
>>> np.vsplit(c,2) Split the array vertically at the 2nd index
[array([[[ 1.5, 2. , 1. ],
[ 4. , 5. , 6. ]]]),
array([[[ 3., 2., 3.],
[ 4., 5., 6.]]])]
Also see Lists
Subsetting
>>> a[2] Select the element at the 2nd index
3
>>> b[1,2] Select the element at row 1 column 2
6.0 (equivalent to b[1][2])
Slicing
>>> a[0:2] Select items at index 0 and 1
array([1, 2])
>>> b[0:2,1] Select items at rows 0 and 1 in column 1
array([ 2., 5.])
>>> b[:1] Select all items at row 0
array([[1.5, 2., 3.]]) (equivalent to b[0:1, :])
>>> c[1,...] Same as [1,:,:]
array([[[ 3., 2., 1.],
[ 4., 5., 6.]]])
>>> a[ : :-1] Reversed array a
array([3, 2, 1])
Boolean Indexing
>>> a[a<2] Select elements from a less than 2
array([1])
Fancy Indexing
>>> b[[1, 0, 1, 0],[0, 1, 2, 0]] Select elements (1,0),(0,1),(1,2)and (0,0)
array([ 4. , 2. , 6. , 1.5])
>>> b[[1, 0, 1, 0]][:,[0,1,2,0]] Select a subset of the matrix’s rows
array([[ 4. ,5. , 6. , 4. ], and columns
[ 1.5, 2. , 3. , 1.5],
[ 4. , 5. , 6. , 4. ],
[ 1.5, 2. , 3. , 1.5]])
>>> a = np.array([1,2,3])
>>> b = np.array([(1.5,2,3), (4,5,6)], dtype = float)
>>> c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]],
dtype = float)
Initial Placeholders
Aggregate Functions
>>> np.loadtxt("myfile.txt")
>>> np.genfromtxt("my_file.csv", delimiter=',')
>>> np.savetxt("myarray.txt", a, delimiter=" ")
I/O
1 2 3
1.5 2 3
4 5 6
Copying Arrays
>>> h = a.view() Create a view of the array with the same data
>>> np.copy(a) Create a copy of the array
>>> h = a.copy() Create a deep copy of the array
Saving & Loading Text Files
Saving & Loading On Disk
>>> np.save('my_array', a)
>>> np.savez('array.npz', a, b)
>>> np.load('my_array.npy')
>>> a.shape Array dimensions
>>> len(a) Length of array
>>> b.ndim Number of array dimensions
>>> e.size Number of array elements
>>> b.dtype Data type of array elements
>>> b.dtype.name Name of data type
>>> b.astype(int) Convert an array to a different type
Inspecting Your Array
>>> np.info(np.ndarray.dtype)
Asking For Help
Sorting Arrays
>>> a.sort() Sort an array
>>> c.sort(axis=0) Sort the elements of an array's axis
Data Types
>>> np.int64 Signed 64-bit integer types
>>> np.float32 Standard double-precision floating point
>>> np.complex Complex numbers represented by 128 floats
>>> np.bool Boolean type storing TRUE and FALSE values
>>> np.object Python object type
>>> np.string_ Fixed-length string type
>>> np.unicode_ Fixed-length unicode type
1 2 3
1.5 2 3
4 5 6
1.5 2 3
4 5 6
1 2 3

More Related Content

What's hot

Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Andrew Ferlitsch
 
Python pandas tutorial
Python pandas tutorialPython pandas tutorial
Python pandas tutorial
HarikaReddy115
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
Priyanshu Sengar
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | Edureka
Edureka!
 
Python : Data Types
Python : Data TypesPython : Data Types
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
Jatin Miglani
 
Pandas
PandasPandas
Pandas
maikroeder
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
Python Pandas
Python PandasPython Pandas
Python Pandas
Sunil OS
 
Python Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptxPython Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptx
ShreyasLawand
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
NishantKumar1179
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
ACASH1011
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
Paras Intotech
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
Piyush rai
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxVisualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
SharmilaMore5
 
String in python use of split method
String in python use of split methodString in python use of split method
String in python use of split method
vikram mahendra
 
Pandas
PandasPandas
Pandas
Jyoti shukla
 
Data visualization in Python
Data visualization in PythonData visualization in Python
Data visualization in Python
Marc Garcia
 

What's hot (20)

Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Python pandas tutorial
Python pandas tutorialPython pandas tutorial
Python pandas tutorial
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | Edureka
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
 
Pandas
PandasPandas
Pandas
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Python Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptxPython Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptx
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxVisualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
 
String in python use of split method
String in python use of split methodString in python use of split method
String in python use of split method
 
Pandas
PandasPandas
Pandas
 
Data visualization in Python
Data visualization in PythonData visualization in Python
Data visualization in Python
 

Similar to Numpy python cheat_sheet

1 pythonbasic
1 pythonbasic1 pythonbasic
1 pythonbasic
pramod naik
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
Namgee Lee
 
14078956.ppt
14078956.ppt14078956.ppt
14078956.ppt
Sivam Chinna
 
Numpy_Cheat_Sheet.pdf
Numpy_Cheat_Sheet.pdfNumpy_Cheat_Sheet.pdf
Numpy_Cheat_Sheet.pdf
SkyNerve
 
python-cheatsheets.pdf
python-cheatsheets.pdfpython-cheatsheets.pdf
python-cheatsheets.pdf
Kalyan969491
 
python-cheatsheets that will be for coders
python-cheatsheets that will be for coderspython-cheatsheets that will be for coders
python-cheatsheets that will be for coders
sarafbisesh
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
Chan Shik Lim
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
Ramanamurthy Banda
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
Ramanamurthy Banda
 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPy
Huy Nguyen
 
Python for Data Science and Scientific Computing
Python for Data Science and Scientific ComputingPython for Data Science and Scientific Computing
Python for Data Science and Scientific Computing
Abhijit Kar Gupta
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdf
Smrati Kumar Katiyar
 
DataCamp Cheat Sheets 4 Python Users (2020)
DataCamp Cheat Sheets 4 Python Users (2020)DataCamp Cheat Sheets 4 Python Users (2020)
DataCamp Cheat Sheets 4 Python Users (2020)
EMRE AKCAOGLU
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
coolmanbalu123
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
DarellMuchoko
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
decoupled
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
Aleksandar Veselinovic
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
MahendraVusa
 

Similar to Numpy python cheat_sheet (20)

1 pythonbasic
1 pythonbasic1 pythonbasic
1 pythonbasic
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
14078956.ppt
14078956.ppt14078956.ppt
14078956.ppt
 
Numpy_Cheat_Sheet.pdf
Numpy_Cheat_Sheet.pdfNumpy_Cheat_Sheet.pdf
Numpy_Cheat_Sheet.pdf
 
python-cheatsheets.pdf
python-cheatsheets.pdfpython-cheatsheets.pdf
python-cheatsheets.pdf
 
python-cheatsheets that will be for coders
python-cheatsheets that will be for coderspython-cheatsheets that will be for coders
python-cheatsheets that will be for coders
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPy
 
Python for Data Science and Scientific Computing
Python for Data Science and Scientific ComputingPython for Data Science and Scientific Computing
Python for Data Science and Scientific Computing
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdf
 
DataCamp Cheat Sheets 4 Python Users (2020)
DataCamp Cheat Sheets 4 Python Users (2020)DataCamp Cheat Sheets 4 Python Users (2020)
DataCamp Cheat Sheets 4 Python Users (2020)
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 

More from Nishant Upadhyay

Multivariate calculus
Multivariate calculusMultivariate calculus
Multivariate calculus
Nishant Upadhyay
 
Multivariate calculus
Multivariate calculusMultivariate calculus
Multivariate calculus
Nishant Upadhyay
 
Matrices1
Matrices1Matrices1
Matrices1
Nishant Upadhyay
 
Vectors2
Vectors2Vectors2
Mathematics for machine learning calculus formulasheet
Mathematics for machine learning calculus formulasheetMathematics for machine learning calculus formulasheet
Mathematics for machine learning calculus formulasheet
Nishant Upadhyay
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascience
Nishant Upadhyay
 
Maths4ml linearalgebra-formula
Maths4ml linearalgebra-formulaMaths4ml linearalgebra-formula
Maths4ml linearalgebra-formula
Nishant Upadhyay
 
Sqlcheetsheet
SqlcheetsheetSqlcheetsheet
Sqlcheetsheet
Nishant Upadhyay
 
Sql cheat-sheet
Sql cheat-sheetSql cheat-sheet
Sql cheat-sheet
Nishant Upadhyay
 
My sql installationguide_windows
My sql installationguide_windowsMy sql installationguide_windows
My sql installationguide_windows
Nishant Upadhyay
 
Company handout
Company handoutCompany handout
Company handout
Nishant Upadhyay
 
Python bokeh cheat_sheet
Python bokeh cheat_sheet Python bokeh cheat_sheet
Python bokeh cheat_sheet
Nishant Upadhyay
 
Foliumcheatsheet
FoliumcheatsheetFoliumcheatsheet
Foliumcheatsheet
Nishant Upadhyay
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheet
Nishant Upadhyay
 

More from Nishant Upadhyay (14)

Multivariate calculus
Multivariate calculusMultivariate calculus
Multivariate calculus
 
Multivariate calculus
Multivariate calculusMultivariate calculus
Multivariate calculus
 
Matrices1
Matrices1Matrices1
Matrices1
 
Vectors2
Vectors2Vectors2
Vectors2
 
Mathematics for machine learning calculus formulasheet
Mathematics for machine learning calculus formulasheetMathematics for machine learning calculus formulasheet
Mathematics for machine learning calculus formulasheet
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascience
 
Maths4ml linearalgebra-formula
Maths4ml linearalgebra-formulaMaths4ml linearalgebra-formula
Maths4ml linearalgebra-formula
 
Sqlcheetsheet
SqlcheetsheetSqlcheetsheet
Sqlcheetsheet
 
Sql cheat-sheet
Sql cheat-sheetSql cheat-sheet
Sql cheat-sheet
 
My sql installationguide_windows
My sql installationguide_windowsMy sql installationguide_windows
My sql installationguide_windows
 
Company handout
Company handoutCompany handout
Company handout
 
Python bokeh cheat_sheet
Python bokeh cheat_sheet Python bokeh cheat_sheet
Python bokeh cheat_sheet
 
Foliumcheatsheet
FoliumcheatsheetFoliumcheatsheet
Foliumcheatsheet
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheet
 

Recently uploaded

一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
NABLAS株式会社
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Linda486226
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 

Recently uploaded (20)

一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 

Numpy python cheat_sheet

  • 1. 2 PythonForDataScience Cheat Sheet NumPy Basics Learn Python for Data Science Interactively at www.DataCamp.com NumPy DataCamp Learn Python for Data Science Interactively The NumPy library is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. >>> import numpy as np Use the following import convention: Creating Arrays >>> np.zeros((3,4)) Create an array of zeros >>> np.ones((2,3,4),dtype=np.int16) Create an array of ones >>> d = np.arange(10,25,5) Create an array of evenly spaced values (step value) >>> np.linspace(0,2,9) Create an array of evenly spaced values (number of samples) >>> e = np.full((2,2),7) Create a constant array >>> f = np.eye(2) Create a 2X2 identity matrix >>> np.random.random((2,2)) Create an array with random values >>> np.empty((3,2)) Create an empty array Array Mathematics >>> g = a - b Subtraction array([[-0.5, 0. , 0. ], [-3. , -3. , -3. ]]) >>> np.subtract(a,b) Subtraction >>> b + a Addition array([[ 2.5, 4. , 6. ], [ 5. , 7. , 9. ]]) >>> np.add(b,a) Addition >>> a / b Division array([[ 0.66666667, 1. , 1. ], [ 0.25 , 0.4 , 0.5 ]]) >>> np.divide(a,b) Division >>> a * b Multiplication array([[ 1.5, 4. , 9. ], [ 4. , 10. , 18. ]]) >>> np.multiply(a,b) Multiplication >>> np.exp(b) Exponentiation >>> np.sqrt(b) Square root >>> np.sin(a) Print sines of an array >>> np.cos(b) Element-wise cosine >>> np.log(a) Element-wise natural logarithm >>> e.dot(f) Dot product array([[ 7., 7.], [ 7., 7.]]) Subsetting, Slicing, Indexing >>> a.sum() Array-wise sum >>> a.min() Array-wise minimum value >>> b.max(axis=0) Maximum value of an array row >>> b.cumsum(axis=1) Cumulative sum of the elements >>> a.mean() Mean >>> b.median() Median >>> a.corrcoef() Correlation coefficient >>> np.std(b) Standard deviation Comparison >>> a == b Element-wise comparison array([[False, True, True], [False, False, False]], dtype=bool) >>> a < 2 Element-wise comparison array([True, False, False], dtype=bool) >>> np.array_equal(a, b) Array-wise comparison 1 2 3 1D array 2D array 3D array 1.5 2 3 4 5 6 Array Manipulation NumPy Arrays axis 0 axis 1 axis 0 axis 1 axis 2 Arithmetic Operations Transposing Array >>> i = np.transpose(b) Permute array dimensions >>> i.T Permute array dimensions Changing Array Shape >>> b.ravel() Flatten the array >>> g.reshape(3,-2) Reshape, but don’t change data Adding/Removing Elements >>> h.resize((2,6)) Return a new array with shape (2,6) >>> np.append(h,g) Append items to an array >>> np.insert(a, 1, 5) Insert items in an array >>> np.delete(a,[1]) Delete items from an array Combining Arrays >>> np.concatenate((a,d),axis=0) Concatenate arrays array([ 1, 2, 3, 10, 15, 20]) >>> np.vstack((a,b)) Stack arrays vertically (row-wise) array([[ 1. , 2. , 3. ], [ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]]) >>> np.r_[e,f] Stack arrays vertically (row-wise) >>> np.hstack((e,f)) Stack arrays horizontally (column-wise) array([[ 7., 7., 1., 0.], [ 7., 7., 0., 1.]]) >>> np.column_stack((a,d)) Create stacked column-wise arrays array([[ 1, 10], [ 2, 15], [ 3, 20]]) >>> np.c_[a,d] Create stacked column-wise arrays Splitting Arrays >>> np.hsplit(a,3) Split the array horizontally at the 3rd [array([1]),array([2]),array([3])] index >>> np.vsplit(c,2) Split the array vertically at the 2nd index [array([[[ 1.5, 2. , 1. ], [ 4. , 5. , 6. ]]]), array([[[ 3., 2., 3.], [ 4., 5., 6.]]])] Also see Lists Subsetting >>> a[2] Select the element at the 2nd index 3 >>> b[1,2] Select the element at row 1 column 2 6.0 (equivalent to b[1][2]) Slicing >>> a[0:2] Select items at index 0 and 1 array([1, 2]) >>> b[0:2,1] Select items at rows 0 and 1 in column 1 array([ 2., 5.]) >>> b[:1] Select all items at row 0 array([[1.5, 2., 3.]]) (equivalent to b[0:1, :]) >>> c[1,...] Same as [1,:,:] array([[[ 3., 2., 1.], [ 4., 5., 6.]]]) >>> a[ : :-1] Reversed array a array([3, 2, 1]) Boolean Indexing >>> a[a<2] Select elements from a less than 2 array([1]) Fancy Indexing >>> b[[1, 0, 1, 0],[0, 1, 2, 0]] Select elements (1,0),(0,1),(1,2)and (0,0) array([ 4. , 2. , 6. , 1.5]) >>> b[[1, 0, 1, 0]][:,[0,1,2,0]] Select a subset of the matrix’s rows array([[ 4. ,5. , 6. , 4. ], and columns [ 1.5, 2. , 3. , 1.5], [ 4. , 5. , 6. , 4. ], [ 1.5, 2. , 3. , 1.5]]) >>> a = np.array([1,2,3]) >>> b = np.array([(1.5,2,3), (4,5,6)], dtype = float) >>> c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype = float) Initial Placeholders Aggregate Functions >>> np.loadtxt("myfile.txt") >>> np.genfromtxt("my_file.csv", delimiter=',') >>> np.savetxt("myarray.txt", a, delimiter=" ") I/O 1 2 3 1.5 2 3 4 5 6 Copying Arrays >>> h = a.view() Create a view of the array with the same data >>> np.copy(a) Create a copy of the array >>> h = a.copy() Create a deep copy of the array Saving & Loading Text Files Saving & Loading On Disk >>> np.save('my_array', a) >>> np.savez('array.npz', a, b) >>> np.load('my_array.npy') >>> a.shape Array dimensions >>> len(a) Length of array >>> b.ndim Number of array dimensions >>> e.size Number of array elements >>> b.dtype Data type of array elements >>> b.dtype.name Name of data type >>> b.astype(int) Convert an array to a different type Inspecting Your Array >>> np.info(np.ndarray.dtype) Asking For Help Sorting Arrays >>> a.sort() Sort an array >>> c.sort(axis=0) Sort the elements of an array's axis Data Types >>> np.int64 Signed 64-bit integer types >>> np.float32 Standard double-precision floating point >>> np.complex Complex numbers represented by 128 floats >>> np.bool Boolean type storing TRUE and FALSE values >>> np.object Python object type >>> np.string_ Fixed-length string type >>> np.unicode_ Fixed-length unicode type 1 2 3 1.5 2 3 4 5 6 1.5 2 3 4 5 6 1 2 3