1604549993

Numpy is an open-source Python library. This library is essential for data scientists who use python. Some other essential libraries like Pandas, Scipy are built on the Numpy library. So I decided to make a cheat sheet. Here I included all the Numpy functions that I used so far. And I believe these functions will be enough for you to do your job in your everyday work life as a data scientist or a data analyst.

I will start with the very basic Numpy functions slowly moved towards the more advanced ones. But using Numpy is easy. You will not find any complicated coding trick here.

In Numpy documentation, Numpy is defined like this:

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

I use this library every day. Most data scientists do if they are python users. It is fast, easy to use, understandable, uncomplicated. I don’t want to write too much about how and why it is so good. Because you will see it for yourself as you read through this article.

My goal is to document Numpy’s every day used methods.

As the title says, it is a guide to Numpy. It can be used as a cheat sheet as well. If you use the Numpy library or planning to use in the future, or trying to learn, this page can be a great resource for your everyday life.

These are the topics that will be covered here:

- Numpy Array Basics
- Repeat
- Mathematics
- Statistics
- Initializing Different Types of Arrays
- Rearranging or Reorganizing Arrays
- Indexing and Slicing of Array
- Adding Rows or Columns
- Append, Insert, Delete, and Sort
- Random
- File Import, Save, and, Load

Let’s start!!

This section is

I used a Jupyter Notebook for this whole exercise. First import Numpy.

```
import numpy as np
```

Make a Numpy array. To do that we need to pass a Python list.

input:

```
a = np.array([1,2,3])
a
```

output:

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

In the array ‘a’ I used all the integers. Now, make an array of floats:

input:

```
b = np.array([[9.0, 10.0, 6.0], [6.0,1.0,7.0]])
b
```

output:

```
array([[ 9., 10., 6.],
[ 6., 1., 7.]])
```

Let’s try to make an array with both ints and floats:

input:

```
np.array([1, 3.0, 0.004, -2])
```

output:

```
array([ 1\. , 3\. , 0.004, -2\. ])
```

Notice, Numpy automatically converted the integers into floats!

Find out the dimensions of array a and b:

input:

```
a.ndim
```

output:

```
1
```

input:

```
b.ndim
```

output:

```
2
```

Array ‘a’ is a one-dimensional array and array b is a two-dimensional array.

Now, find the shape of the array ‘a’ and ‘b’:

input:

```
a.shape
```

output:

```
(3,)
```

input:

```
b.shape
```

output:

```
(2, 3)
```

Array ‘a’ is a one-dimensional array. So, it has only one value in shape. But array ‘b’ is a two-dimensional array. So, it’s shape is 2 x 3. That means it has 2 rows and 3 columns.

Find the length of the arrays:

input:

```
len(a)
```

output:

```
3
```

input:

```
len(b)
```

output:

```
2
```

Array ‘a’ has length 3 because it has 3 elements in it. Array ‘b’ is a two-dimensional array. So, the length of the array does not mean the number of elements in it. The length means the number of one-dimensional arrays in it or the number of rows in it. It has two rows. So, the length is 2.

There are a few different ways to repeat the elements of an array. If you want to repeat the whole array,

input:

```
np.array([2,4,6]*4)
```

output:

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

Look, the array [2, 4, 6] was repeated 4 times.

Here is how to do the elementwise repetition,

input:

```
np.repeat([1,2,3], 3)
```

output:

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

This time each element was repeated 3 times.

Let’s use this for a two-dimensional array,

input:

```
arr = np.array([[2, 4, 6]])
arr
```

output:

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

Now, use repeat on it:

input:

```
np.repeat(arr,3,axis=0)
```

output:

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

Here, we mentioned axis = 0. So, the repetition occurred in the axis-0 direction or rows direction.

input:

```
np.repeat(arr,3,axis=1)
```

output:

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

Axis 1 indicates the direction of columns. So, repetition happens in the column’s direction.

In this section, I am going to show the mathematical operations. Most of the operations are self-explanatory. I will start with mathematical operations on one array.

input:

```
a = np.array([1,2,3,4])
a
```

output:

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

input:

```
a+2
```

output:

```
array([3, 4, 5, 6])
```

It adds 2 to each element of the array.

input:

```
a-2
```

output:

```
array([-1, 0, 1, 2])
```

You can simply use similar operations this way such as:

input:

```
a/2
```

output:

```
array([0.5, 1\. , 1.5, 2\. ])
```

input:

```
a**2
```

output:

```
array([ 1, 4, 9, 16], dtype=int32)
```

Two asterisks mean exponents. Each element in ‘a’ is squared.

input:

```
np.sqrt(a) #square root
```

output:

```
array([1\. , 1.41421356, 1.73205081, 2\. ])
```

We can also perform some trigonometric operations:

input:

```
np.cos(a)
```

Output:

```
array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362])
```

input:

```
np.sin(a)
```

output:

```
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 ])
```

input:

```
np.tan(a)
```

output:

```
array([ 1.55740772, -2.18503986, -0.14254654, 1.15782128])
```

Now see how we can do some mathematical operation in two arrays or matrices. First, make one more array,

input:

```
b = np.array([3,4,5,6])
```

output:

```
array([3, 4, 5, 6])
```

Just as a reminder, our array ‘a’ looked like this:

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

Now, we have two arrays, a and b. Let’s do the same mathematical operations. Again, it’s simple and self-explanatory,

input:

```
a + b
```

output:

```
array([ 4, 6, 8, 10])
```

In the same way, you can do these following operations:

```
a - b
a*b
a/b
a**b
```

Another widely used operation is,

input:

```
a.dot(b)
```

output:

```
50
```

What is a.dot(b)? It’s element-wise multiplication and then addition like this,

```
1*3 + 2*4 + 3*5 + 4*6
```

where array ‘a’ is [1,2,3,4] and array b is [3,4,5,6].

You can write the syntax a bit differently as well,

```
np.dot(a, b)
```

This works the same. The output will be 50.

We can use this dot process in multi-dimensional arrays. Let’s make two multi-dimensional arrays for that,

input:

```
c = np.array([[3, 5, 1], [6, 4, 9]])
c
```

output:

```
array([[3, 5, 1],
[6, 4, 9]])
```

input:

```
d = np.array([[5,2], [7,9], [4, 3]])
d
```

output:

```
array([[5, 2],
[7, 9],
[4, 3]])
```

We are ready for a ‘dot’ operation on a multi-dimensional array,

input:

```
c.dot(d)
```

output:

```
array([[54, 54],
[94, 75]])
```

When the inputs are 2D array, ‘dot’ function behaves like matrix multiplication.

That means you can only perform the ‘dot’ operation when the number of columns of the first array matches the number of rows in the second array.

If the first array is m x n, the second array should be n x p.

Matrix multiplication has another expression,

input:

```
np.matmul(c, d)
```

output:

```
array([[54, 54],
[94, 75]])
```

‘np.matmul’ does not work in one-dimensional arrays

Remember, this multiplication rule does not apply in other operations such as addition, deduction or division. We need to have the arrays of same shape and size to add, subtract or divide one matrix by another.

Numpy has basic statistical operations as well. Here are some examples.

Make a new array first.

input:

```
x = np.array([1,3,4,6,-3,-2])
x.sum()
```

output:

```
9
```

input:

```
x.max()
```

output:

```
6
```

input:

```
x.min()
```

output:

```
-3
```

input:

```
x.mean()
```

output:

```
1.5
```

input:

```
x.std() #standard deviation
```

output:

```
3.2015621187164243
```

There are two other very useful functions which are not exactly statistical,

input:

```
x.argmin()
```

output:

```
4
```

input:

```
x.argmax()
```

output:

```
3
```

What ‘argmin()’ or ‘argmax()’?

‘argmin()’ gives you the index of the minimum element of the array and ‘argmax()’ returns the index of the maximum value of the array.

The minimum element of the array ‘x’ is -3 and the maximum element of array ‘x’ is 6. Now check if their index matches the result.

#machine-learning #data-science #numpy #python #developer

1595235240

In this Numpy tutorial, we will learn Numpy applications.

NumPy is a basic level external library in Python used for complex mathematical operations. NumPy overcomes slower executions with the use of multi-dimensional array objects. It has built-in functions for manipulating arrays. We can convert different algorithms to can into functions for applying on arrays.NumPy has applications that are not only limited to itself. It is a very diverse library and has a wide range of applications in other sectors. Numpy can be put to use along with Data Science, Data Analysis and Machine Learning. It is also a base for other python libraries. These libraries use the functionalities in NumPy to increase their capabilities.

Arrays in Numpy are equivalent to lists in python. Like lists in python, the Numpy arrays are homogenous sets of elements. The most important feature of NumPy arrays is they are homogenous in nature. This differentiates them from python arrays. It maintains uniformity for mathematical operations that would not be possible with heterogeneous elements. Another benefit of using NumPy arrays is there are a large number of functions that are applicable to these arrays. These functions could not be performed when applied to python arrays due to their heterogeneous nature.

Arrays in NumPy are objects. Python deletes and creates these objects continually, as per the requirements. Hence, the memory allocation is less as compared to Python lists. NumPy has features to avoid memory wastage in the data buffer. It consists of functions like copies, view, and indexing that helps in saving a lot of memory. Indexing helps to return the view of the original array, that implements reuse of the data. It also specifies the data type of the elements which leads to code optimization.

We can also create multi-dimensional arrays in NumPy.These arrays have multiple rows and columns. These arrays have more than one column that makes these multi-dimensional. Multi-dimensional array implements the creation of matrices. These matrices are easy to work with. With the use of matrices the code also becomes memory efficient. We have a matrix module to perform various operations on these matrices.

Working with NumPy also includes easy to use functions for mathematical computations on the array data set. We have many modules for performing basic and special mathematical functions in NumPy. There are functions for Linear Algebra, bitwise operations, Fourier transform, arithmetic operations, string operations, etc.

#numpy tutorials #applications of numpy #numpy applications #uses of numpy #numpy

1595235180

Welcome to DataFlair!!! In this tutorial, we will learn Numpy Features and its importance.

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays

NumPy (Numerical Python) is an open-source core Python library for scientific computations. It is a general-purpose array and matrices processing package. Python is slower as compared to Fortran and other languages to perform looping. To overcome this we use NumPy that converts monotonous code into the compiled form.

These are the important features of NumPy:

This is the most important feature of the NumPy library. It is the homogeneous array object. We perform all the operations on the array elements. The arrays in NumPy can be one dimensional or multidimensional.

The one-dimensional array is an array consisting of a single row or column. The elements of the array are of homogeneous nature.

In this case, we have various rows and columns. We consider each column as a dimension. The structure is similar to an excel sheet. The elements are homogenous.

We can use the functions in NumPy to work with code written in other languages. We can hence integrate the functionalities available in various programming languages. This helps implement inter-platform functions.

#numpy tutorials #features of numpy #numpy features #why use numpy #numpy

1625957100

For all those budding professionals and newbies alike who are thinking of taking a dive into the booming world of data science, we have compiled a quick cheat sheet to get you brushed up with the basics and methodologies that underline this field.

The data that gets generated in our world is in a raw form, i.e., numbers, codes, words, sentences, etc. Data Science takes this very raw data to process it using scientific methods to transform it into meaningful forms to gain knowledge and insights.

- Structured
- Unstructured
- Semi-structured
- Quantitative
- Big Data

- Data Classification
- Data Cleansing
- Data Masking

#data science #cheat sheet #data science cheat sheet

1619660285

A geek in Machine Learning with a Master’s degree in…

####### READ NEXT

NumPy is an essential Python library to perform mathematical and scientific computations. NumPy offers Python’s array-like data structures with exclusive operations and methods. Many data science libraries and frameworks, including Pandas, Scikit-Learn, Statsmodels, Matplotlib and SciPy, are built on top of NumPy with Numpy arrays in their building blocks. Some frameworks, including TensorFlow and PyTorch, introduce NumPy arrays or NumPy-alike arrays as their fundamental data structure in the name of tensors.

How NumPy becomes the base of Data Science computing system (source)

Data Science relies heavily on Linear Algebra. NumPy is famous for its Linear Algebra operations. This article discusses methods available in the NumPy library to perform various Linear Algebra operations with examples. These examples assume that the readers have a basic understanding of NumPy arrays. Check out the following articles to have a better understanding of NumPy fundamentals:

#developers corner #linear algebra #matrices #numpy #numpy array #numpy dot product #numpy matrix multiplication #numpy tutorial #svd #vectors

1596475108

NumPy consists of different methods to duplicate an original array. The two main functions for this duplication are copy and view. The duplication of the array means an array assignment. When we duplicate the original array, the changes made in the new array may or may not reflect. The duplicate array may use the same location or may be at a new memory location.

It returns a copy of the original array stored at a new location. The copy doesn’t share data or memory with the original array. The modifications are not reflected. The copy function is also known as deep copy.

```
import numpy as np
arr = np.array([20,30,50,70])
a= arr.copy()
#changing a value in original array
arr[0] = 100
print(arr)
print(a)
```

Output

[100 30 50 70]

[20 30 50 70]

Changes made in the original array are not reflected in the copy.

```
import numpy as np
arr = np.array([20,30,50,70])
a= arr.copy()
#changing a value in copy array
a[0] = 5
print(arr)
print(a)
```

Output

[20 30 50 70]

[ 5 30 50 70]

Changes made in copy are not reflected in the original array

#numpy tutorials #numpy copy #numpy views #numpy