Academic
  • Introduction
  • Artificial Intelligence
    • Introduction
    • AI Concepts, Terminology, and Application Areas
    • AI: Issues, Concerns and Ethical Considerations
  • Biology
    • Scientific Method
    • Chemistry of Life
    • Water, Acids, Bases
    • Properties of carbon
    • Macromolecules
    • Energy and Enzymes
    • Structure of a cell
    • Membranes and transport
    • Cellular respiration
    • Cell Signaling
    • Cell Division
    • Classical and molecular genetics
    • DNA as the genetic material
    • Central dogma
    • Gene regulation
  • Bioinformatics
    • Bioinformatics Overview
  • Deep Learning
    • Neural Networks and Deep Learning
      • Introduction
      • Logistic Regression as a Neural Network
      • Python and Vectorization
      • Shallow Neural Network
      • Deep Neural Network
    • Improving Deep Neural Networks
      • Setting up your Machine Learning Application
      • Regularizing your Neural Network
      • Setting up your Optimization Problem
      • Optimization algorithms
      • Hyperparameter, Batch Normalization, Softmax
    • Structuring Machine Learning Projects
    • Convolutional Neural Networks
      • Introduction
    • Sequence Models
      • Recurrent Neural Networks
      • Natural Language Processing & Word Embeddings
      • Sequence models & Attention mechanism
  • Linear Algebra
    • Vectors and Spaces
      • Vectors
      • Linear combinations and spans
      • Linear dependence and independence
      • Subspaces and the basis for a subspace
      • Vector dot and cross products
      • Matrices for solving systems by elimination
      • Null space and column space
    • Matrix transformations
      • Functions and linear transformations
      • Linear transformation examples
      • Transformations and matrix multiplication
      • Inverse functions and transformations
      • Finding inverses and determinants
      • More Determinant Depth
  • Machine Learning
    • Introduction
    • Linear Regression
      • Model and Cost Function
      • Parameter Learning
      • Multivariate Linear Regression
      • Computing Parameters Analytically
      • Octave
    • Logistic Regression
      • Classification and Representation
      • Logistic Regression Model
    • Regularization
      • Solving the Problem of Overfitting
    • Neural Networks
      • Introduction of Neural Networks
      • Neural Networks - Learning
    • Improve Learning Algorithm
      • Advice for Applying Machine Learning
      • Machine Learning System Design
    • Support Vector Machine
      • Large Margin Classification
      • Kernels
      • SVM in Practice
  • NCKU - Artificial Intelligence
    • Introduction
    • Intelligent Agents
    • Solving Problems by Searching
    • Beyond Classical Search
    • Learning from Examples
  • NCKU - Computer Architecture
    • First Week
  • NCKU - Data Mining
    • Introduction
    • Association Analysis
    • FP-growth
    • Other Association Rules
    • Sequence Pattern
    • Classification
    • Evaluation
    • Clustering
    • Link Analysis
  • NCKU - Machine Learning
    • Probability
    • Inference
    • Bayesian Inference
    • Introduction
  • NCKU - Robotic Navigation and Exploration
    • Kinetic Model & Vehicle Control
    • Motion Planning
    • SLAM Back-end (I)
    • SLAM Back-end (II)
    • Computer Vision / Multi-view Geometry
    • Lie group & Lie algebra
    • SLAM Front-end
  • Python
    • Numpy
    • Pandas
    • Scikit-learn
      • Introduction
      • Statistic Learning
  • Statstics
    • Quantitative Data
    • Modeling Data Distribution
    • Bivariate Numerical Data
    • Probability
    • Random Variables
    • Sampling Distribution
    • Confidence Intervals
    • Significance tests
Powered by GitBook
On this page
  • Numpy arrays
  • Statistical functions
  • Filters on arrays
  • Array Operations

Was this helpful?

  1. Python

Numpy

PreviousPythonNextPandas

Last updated 5 years ago

Was this helpful?

Here are some tutorials and examples :

Before you start, It's important to import numpy in your code.

import numpy as np

Numpy arrays

a = np.zeros(3)
# [0. 0. 0]  ndarray with floats

a.shape
# (3,)

a.shape = (3, 1)
# [[0.
#   0.
#   0.]]

a = np.ones(5)
# [1. 1. 1. 1. 1.]

a = np.empty(3)
# [0. 0. 0]

a = np.linspace(2, 10, 5)  # from 2 to 10, with 5 elements
# [2. 4. 6. 8. 10.]

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

a_list = [[10, 20, 30], [40, 50]]
a = np.array(a_list)
# [[10, 20, 30], [40, 50]]  ndarray

np.random.seed(0)
a = np.random.randint(10, size=5)
# [5 0 3 3 7]

a[0:2]
# [5, 0]

a[-1]
# 7

Statistical functions

np.sum(arr)  # sum
np.prod(arr)  # product
np.mean(arr)  # mean
np.std(arr)  # standard deviation
np.var(arr)  # variance
np.min(arr)  # minimum
np.max(arr)  # maximum
np.argmin(arr)  # indices of min
np.argmax(arr)  # indices of max

Filters on arrays

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

a < 3
# [True, True, False, False, False]

a > 3
# [False, False, False, True, True]

a[a < 3]
# [1, 2]

Array Operations

a = np.array([1, 2, 3, 4, 5])
b = np.array([6, 7, 8, 9, 10])

a + b
# [7, 9, 11, 13, 15]

a + 30
# [31, 32, 33, 34, 35]

a * b
# [6, 14, 24, 36, 50]

a @ b  # dot product
# 130

a = np.array([[1, 2, 3], [4, 5, 6]])
a.T  # transpose
# [[1, 4],
#  [2, 5],
#  [3, 6]]

a = np.array([1, 4, 2, 6, 0])
np.sort(a)
# [0, 1, 2, 4, 6]
Quickstart tutorial - Numpy Official documentation
Learn Numpy in 5 minutes - Best Python Library