Hands-on with NumPy basics

Prerequisite

Table of Content

  1. Installing NumPy
  2. Importing Library
  3. Introducing Array
  4. Indexing
  5. Some Inbuilt Functions
  6. Reference Type
  7. Random Distribution

Note

Installing NumPy

pip install numpy
# Best practice, use an environment rather than install in the base env
conda create -n my-env
conda activate my-env
# If you want to install from conda-forge
conda config --env --add channels conda-forge
# The actual install command
conda install numpy

Importing Library

import numpy as np

Introducing Array

my_lst = [1,2,3,4,5]
arr = np.array(my_lst)
type(arr)
arr
arr.shape
my_lst1=[1,2,3,4,5]
my_lst2=[2,3,4,5,6]
my_lst3=[9,7,6,8,9]
arr=np.array([my_lst1,my_lst2,my_lst3])
arr
arr.shape
arr.reshape(5,3)

Indexing

arr=np.array([1,2,3,4,5,6,7,8,9])
arr[3]
my_lst1=[1,2,3,4,5]
my_lst2=[2,3,4,5,6]
my_lst3=[9,7,6,8,9]
arr=np.array([my_lst1,my_lst2,my_lst3])
## arr[<row_start>:<row_end+1>,<col_start>:<col_end+1>,<step>]
arr[1:,3:]

Some Inbuilt Functions

arr=np.arange(0,10)
arr
np.linspace(1,10,50)

Reference Type

arr[3:]=100
arr
array([ 0, 1, 2, 100, 100, 100, 100, 100, 100, 100])
arr1=arr
arr1[3:]=500
print(arr1)
print(arr)
[  0   1   2 500 500 500 500 500 500 500]
[ 0 1 2 500 500 500 500 500 500 500]
arr1=arr.copy()
print(arr)
arr1[3:]=1000
print(arr1)
[   0    1    2 1000 1000 1000 1000 1000 1000 1000]
[ 0 1 2 500 500 500 500 500 500 500]
val=2
arr<2
arr[arr<2]
np.ones(4)
np.ones(4,dtype=int)
np.ones((2,5),dtype=float)

Random Distribution

np.random.rand(3,3)
arr_ex=np.random.randn(4,4)
arr_ex
np.random.randint(0,100,8)
np.random.random_sample((1,5))

--

--

--

Student at Jaypee Institute of Information Technology

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Bouquet Analytics REST API —The Case for Self Governed User Management

Object Oriented Programming with C

Chapter 16 -Collections and Generics: Data Structures

How To Create A VPS In AWS The Easy Way With Amazon Lightsail

Journaling Your Bugs For Fun And Profit

Combining constraints in PureScript

The Sixth Stage of Debugging

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavini Jain

Pavini Jain

Student at Jaypee Institute of Information Technology

More from Medium

Recognizing Handwritten Digits with Scikit-Learn

Data Types in Machine Learning

KNN — K-Nearest Neighbours

Predicting House Prices using Supervised Learning Methods