Getting Started with NumPy
What is NumPy?
NumPy (Numerical Python) is a Python package that is used in processing multidimensional arrays. It comes in handy when performing mathematical and statistical Python operations.?
It is compatible with other libraries like pandas, Matplotlib and scikit-learn making programming in data science much easier. This walkthrough focuses primarily on NumPy arrays.
Getting Started with NumPy
To get started with NumPy, the first step would be to get it. You would need Python and pip (a package management system for Python) installed to install and use NumPy. You can install Python from https://www.python.org/downloads (This version should come with pip installed).
To install NumPy, open your terminal and type the following command:
This should install NumPy on your computer.?
Once installed, you have to import NumPy into your file to use it. You typically have to do this with all external Python packages. You can import it using the command below, np is usually the alias used to reference NumPy in Python.?
NumPy Arrays
NumPy arrays are fundamental to performing any operation using NumPy. NumPy arrays can be multidimensional. To create a NumPy array, you can instantiate one from a list. NumPy array types are of type numpy.ndarray.
Multidimensional arrays are equally as easy to create as one-dimensional arrays. Below is an example of a two-dimensional array.
领英推荐
Ones
NumPy ones are used to return a new array of a given shape and type, filled with ones. You can call this function to return both one dimensional or multidimensional arrays; all you need to do is specify the dimension in the parenthesis of the function. The code snippets below show the use of the NumPy ones function to create arrays containing ones as floats.
Zeros
Just like returning arrays of ones, the NumPy zeros function allows you to return a new array of a given shape and type, filled with zeros. Code snippets below show how to create and use the NumPy zeros function.
Arange
The NumPy arange function is used to return evenly spaced values within a given interval. The code snippet below shows how you can use the NumPy arange function to return evenly spaced values with a given range and also by specifying the exact spacing you want between these values.
Linspace
The NumPy linspace function is used to return evenly spaced values between a start and stop value. It is particularly useful when you are trying to create an evenly spaced list of elements for your x-axis.
Random
NumPy random function allows you to generate a matrix of random numbers between a specified range or as a matrix. You can create an n by n matrix of random uniformly distributed numbers or you can create a matrix with a specified number of integers within a range.
Want to learn more about NumPy and other useful programming tips and tools for data scientists? Be sure to follow me on LinkedIn so you don’t miss any of my upcoming posts.
Founder at PeaceMaker with expertise in Full-Stack Development & ML Engineering
3 年simplicita
PhD Statistics, ISU || Statistical Methods in Forensics || Bayesian Statistics || Statistical Learning
3 年Love this