NumPy array basics A
Lists in Python are quite general, and can have arbitrary objects as elements. Addition and scalar multiplication are defined for lists. However, lists won't give us what we want for numerical computations as shown in the following examples:
Multiplication - repeats:
>>> a = [1, 2]>>> 2*a [1, 2, 1, 2]
Addition - concatenates:
>>> a = [1, 2]>>> b = [3, 4]>>> a + b [1, 2, 3, 4]
If we do the same operations with NumPy, we get:
>>> import numpy as np>>> a = np.array([1, 2])>>> 2*a array([2, 4])>>> >>> b = np.array([3, 4])>>> a + b array([4, 6])
Also, note here that the '*' does component-wise multiplication:
>> x = np.array(range(5))>>> x array([0, 1, 2, 3, 4])>>> np.sqrt(x) * x + np.cos(x) array([ 1. , 1.54030231, 2.41228029, 4.20615993, 7.34635638])
One more thing related to the data type before we dive into NumPy section. Unlike lists, all elements of an np.array have the same type:
>>> np.array([1., 2., 3.]) # all floats array([ 1., 2., 3.])>>> np.array([1. , 2, 3]) # one float array([ 1., 2., 3.]) # all elements become float
NumPy can explicitly state data type:
>>> np.array([1, 2, 3], dtype=complex) array([ 1.+0.j, 2.+0.j, 3.+0.j])
NumPy is a Python extension to add support for large, multi-dimensional arrays and matrices, along with a large library of high-level mathematical functions.
>>> import numpy as np>>> x = np.array([1,2,3])>>> x array([1, 2, 3])>>>
We can also create an array using an input:
>>> use_me = [ [1,2,3],[4,5,6]]>>> myArray = np.array(use_me)>>> myArray array([[1, 2, 3], [4, 5, 6]])
We can fill all elements with zeros.
>>> import numpy as np>>> zeroArray = np.zeros((2,4))>>> zeroArray array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
Or ones:
>>> onesArray = np.ones((4,2))>>> onesArray array([[ 1., 1.], [ 1., 1.], [ 1., 1.], [ 1., 1.]])
The np.empty(...) is filled with random/junk values:
>>> import numpy as np>>> emptyArray = np.empty((2,3))>>> emptyArray array([[ 0.00000000e+000, 3.39519327e-313, 0.00000000e+000], [ 4.94065646e-324, 1.83322544e-316, 6.94110822e-310]])
It looks like random, but it's not. So, if we need real random numbers, we should not use this empty(...).
numpy.arange([start], stop[, step], dtype=None)
If we want to specify the step, then the start should be spefied:
>>> a = np.arange(5, 10, 0.5)>>> a array([ 5. , 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])
>>> import numpy as np>>> randomArray = np.random.random((4,4))>>> randomArray array([[ 0.10085918, 0.44759528, 0.40433292, 0.30975764], [ 0.2023531 , 0.88821789, 0.71853805, 0.64503574], [ 0.36394454, 0.01794277, 0.09041095, 0.74117827], [ 0.41225956, 0.20244151, 0.59867229, 0.80260473]])
np.random.random(...) is actually using a random number generator to fill in each of the spots in the array with a randomly sampled number from 0 to 1.
We can specify low and high as shown in the example below (low = 1, high = 10)
>>> a = np.random.randint(1, 10, (5,2))>>> a array([[3, 2], [8, 4], [5, 2], [3, 2], [4, 4]])
>>> import numpy as np>>> rArray = np.arange(0,20).reshape((5,4))>>> rArray array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])>>>
- Note that the arange(...) function returns a 1D array similar to what we'd get from using the built-in python function range(...) with the same arguments.
>>> np.arange(0,20) array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
- The reshape method takes the data in an existing array, and puts it into an array with the given shape and returns it.
>>> rArray.reshape((2,10)) array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])
- Note that the original rArray stays there not changed by another reshape(...):
>>> rArray array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
- When we use reshape(...), the total number of elements in the array must remain the same. So, reshaping an array with 4 rows and 5 columns into one with 10 rows and 2 columns is fine, but 5x5 or 7x3 would fail:
>>> rArray.reshape((5,5)) Traceback (most recent call last): File "
", line 1, in ValueError: total size of new array must be unchanged
The shape attribute for numpy arrays returns the dimensions of the array. If Arr has m rows and m columns, then Arr.shape is (m,n). So Arr.shape[0] is m and Arr.shape[1] is n. Also, Arr.shape[-1] is n, Arr.shape[-2] is m.
>>> a = np.arange(10)>>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> a.shape (10,)>>> b = a.reshape(5,-1)>>> b array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])>>> b.shape (5, 2)>>> b.shape[0] 5>>> b.shape[1] 2>>> b.shape[-1] 2>>> b.shape[-2] 5
Accessing an array is pretty much straight forward. We access a specific location in the table by referring to its row and column inside square braces.
>>> rArray = np.arange(0,20).reshape((5,4))>>> rArray array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])
To get an element, we specify rArray[row, column]:
>>> rArray[2,3] 11>>> rArray[4,0] 16
Note that the index starts from 0.
We can also refer to ranges inside an array:
>>> rArray[3,1:3] array([13, 14])>>> rArray[2:5,1:4] array([[ 9, 10, 11], [13, 14, 15], [17, 18, 19]])
These ranges work just like slices for lists. s:e:step specifies a range that starts at s, and stops before e, in steps size of step. If any of these are left off, they're assumed to be the s, the e+1, and 1, respectively.
If we want only the elements in the first column, we do this:
>>> rArray[:,0:5:4] array([[ 0], [ 4], [ 8], [12], [16]])>>> rArray[:,0] array([ 0, 4, 8, 12, 16])
If we want only the 0th, 2nd, 4th rows:
>>> rArray[0:5:2,:] array([[ 0, 1, 2, 3], [ 8, 9, 10, 11], [16, 17, 18, 19]])
Or we can left off for the defaults:
>>> rArray[::2,] array([[ 0, 1, 2, 3], [ 8, 9, 10, 11], [16, 17, 18, 19]])
We can use minus(-) index.
To get the last column only:
>>> rArray array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])>>> rArray[:,-1] array([ 3, 7, 11, 15, 19])
For the 2nd from the last:
>>> rArray[:,-2] array([ 2, 6, 10, 14, 18])
The np.newaxis can be used to adding additional dimension to the np.ndarray:
>>> a = np.arange(6).reshape(3,2)>>> a array([[0, 1], [2, 3], [4, 5]])>>> new_a = a[:,:, np.newaxis]>>> new_a array([[[0], [1]], [[2], [3]], [[4], [5]]])>>> new_a.shape (3, 2, 1)
Column vector:
>>> c = np.array([1,2,3])>>> c array([1, 2, 3])>>> c.shape (3,)>>> c.size 3
row vector:
>>> r = np.array([ [1,2,3] ])>>> r array([[1, 2, 3]])>>> r.shape (1, 3)>>> r.size 3>>> r[0,0] 1>>> r[0,1] 2>>> r[0,2] 3
To join a sequence of arrays together, we use numpy.concatenate():
numpy.concatenate((a1, a2, ...), axis=0)
Here, axis denotes the axis along which the arrays will be joined. Default is 0 (row join).
row concatenate:
>>> import numpy as np>>> a = np.array([[1,2], [3,4]])>>> a.shape (2, 2)>>> b = np.array([[5, 6]])>>> b.shape (1, 2)>>> # row join>>> np.concatenate((a, b), axis=0) array([[1, 2], [3, 4], [5, 6]])
column concatenate:
>>> a = np.array([[1,2], [3,4]])>>> a array([[1, 2], [3, 4]])>>> a.shape (2, 2)>>> b = np.array([[5, 6]])>>> b array([[5, 6]])>>> b.shape (1, 2)>>> np.concatenate((a,b),axis=1) Traceback (most recent call last): File "", line 1, in ValueError: all the input array dimensions except for the concatenation axis must match exactly
We need to have the same shapes for the arrays to concatenate. So, b should be transposed:
>>> bt = b.T>>> bt array([[5], [6]])>>> bt.shape (2, 1)>>> np.concatenate((a,bt),axis=1) array([[1, 2, 5], [3, 4, 6]])
Continued to NumPy Array Basics B.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization
Python tutorial
Python Home
Introduction
Running Python Programs (os, sys, import)
Modules and IDLE (Import, Reload, exec)
Object Types - Numbers, Strings, and None
Strings - Escape Sequence, Raw String, and Slicing
Strings - Methods
Formatting Strings - expressions and method calls
Files and os.path
Traversing directories recursively
Subprocess Module
Regular Expressions with Python
Regular Expressions Cheat Sheet
Object Types - Lists
Object Types - Dictionaries and Tuples
Functions def, *args, **kargs
Functions lambda
Built-in Functions
map, filter, and reduce
Decorators
List Comprehension
Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism
Hashing (Hash tables and hashlib)
Dictionary Comprehension with zip
The yield keyword
Generator Functions and Expressions
generator.send() method
Iterators
Classes and Instances (__init__, __call__, etc.)
if__name__ == '__main__'
argparse
Exceptions
@static method vs class method
Private attributes and private methods
bits, bytes, bitstring, and constBitStream
json.dump(s) and json.load(s)
Python Object Serialization - pickle and json
Python Object Serialization - yaml and json
Priority queue and heap queue data structure
Graph data structure
Dijkstra's shortest path algorithm
Prim's spanning tree algorithm
Closure
Functional programming in Python
Remote running a local file using ssh
SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table
SQLite 3 - B. Selecting, updating and deleting data
MongoDB with PyMongo I - Installing MongoDB ...
Python HTTP Web Services - urllib, httplib2
Web scraping with Selenium for checking domain availability
REST API : Http Requests for Humans with Flask
Blog app with Tornado
Multithreading ...
Python Network Programming I - Basic Server / Client : A Basics
Python Network Programming I - Basic Server / Client : B File Transfer
Python Network Programming II - Chat Server / Client
Python Network Programming III - Echo Server using socketserver network framework
Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn
Python Coding Questions I
Python Coding Questions II
Python Coding Questions III
Python Coding Questions IV
Python Coding Questions V
Python Coding Questions VI
Python Coding Questions VII
Python Coding Questions VIII
Python Coding Questions IX
Python Coding Questions X
Image processing with Python image library Pillow
Python and C++ with SIP
PyDev with Eclipse
Matplotlib
Redis with Python
NumPy array basics A
NumPy Matrix and Linear Algebra
Pandas with NumPy and Matplotlib
Celluar Automata
Batch gradient descent algorithm
Longest Common Substring Algorithm
Python Unit Test - TDD using unittest.TestCase class
Simple tool - Google page ranking by keywords
Google App Hello World
Google App webapp2 and WSGI
Uploading Google App Hello World
Python 2 vs Python 3
virtualenv and virtualenvwrapper
Uploading a big file to AWS S3 using boto module
Scheduled stopping and starting an AWS instance
Cloudera CDH5 - Scheduled stopping and starting services
Removing Cloud Files - Rackspace API with curl and subprocess
Checking if a process is running/hanging and stop/run a scheduled task on Windows
Apache Spark 1.3 with PySpark (Spark Python API) Shell
Apache Spark 1.2 Streaming
bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...
Flask app with Apache WSGI on Ubuntu14/CentOS7 ...
Selenium WebDriver
Fabric - streamlining the use of SSH for application deployment
Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App
Neural Networks with backpropagation for XOR using one hidden layer
NLP - NLTK (Natural Language Toolkit) ...
RabbitMQ(Message broker server) and Celery(Task queue) ...
OpenCV3 and Matplotlib ...
Simple tool - Concatenating slides using FFmpeg ...
iPython - Signal Processing with NumPy
iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github
iPython and Jupyter Notebook with Embedded D3.js
Downloading YouTube videos using youtube-dl embedded with Python
Machine Learning : scikit-learn ...
Django 1.6/1.8 Web Framework ...
OpenCV 3 image and video processing with Python
OpenCV 3 with Python
Image - OpenCV BGR : Matplotlib RGB
Basic image operations - pixel access
iPython - Signal Processing with NumPy
Signal Processing with NumPy I - FFT and DFT for sine, square waves, unitpulse, and random signal
Signal Processing with NumPy II - Image Fourier Transform : FFT & DFT
Inverse Fourier Transform of an Image with low pass filter: cv2.idft()
Image Histogram
Video Capture and Switching colorspaces - RGB / HSV
Adaptive Thresholding - Otsu's clustering-based image thresholding
Edge Detection - Sobel and Laplacian Kernels
Canny Edge Detection
Hough Transform - Circles
Watershed Algorithm : Marker-based Segmentation I
Watershed Algorithm : Marker-based Segmentation II
Image noise reduction : Non-local Means denoising algorithm
Image object detection : Face detection using Haar Cascade Classifiers
Image segmentation - Foreground extraction Grabcut algorithm based on graph cuts
Image Reconstruction - Inpainting (Interpolation) - Fast Marching Methods
Video : Mean shift object tracking
Machine Learning : Clustering - K-Means clustering I
Machine Learning : Clustering - K-Means clustering II
Machine Learning : Classification - k-nearest neighbors (k-NN) algorithm
Machine Learning with scikit-learn
scikit-learn installation
scikit-learn : Features and feature extraction - iris dataset
scikit-learn : Machine Learning Quick Preview
scikit-learn : Data Preprocessing I - Missing / Categorical data
scikit-learn : Data Preprocessing II - Partitioning a dataset / Feature scaling / Feature Selection / Regularization
scikit-learn : Data Preprocessing III - Dimensionality reduction vis Sequential feature selection / Assessing feature importance via random forests
Data Compression via Dimensionality Reduction I - Principal component analysis (PCA)
scikit-learn : Data Compression via Dimensionality Reduction II - Linear Discriminant Analysis (LDA)
scikit-learn : Data Compression via Dimensionality Reduction III - Nonlinear mappings via kernel principal component (KPCA) analysis
scikit-learn : Logistic Regression, Overfitting & regularization
scikit-learn : Supervised Learning & Unsupervised Learning - e.g. Unsupervised PCA dimensionality reduction with iris dataset
scikit-learn : Unsupervised_Learning - KMeans clustering with iris dataset
scikit-learn : Linearly Separable Data - Linear Model & (Gaussian) radial basis function kernel (RBF kernel)
scikit-learn : Decision Tree Learning I - Entropy, Gini, and Information Gain
scikit-learn : Decision Tree Learning II - Constructing the Decision Tree
scikit-learn : Random Decision Forests Classification
scikit-learn : Support Vector Machines (SVM)
scikit-learn : Support Vector Machines (SVM) II
Flask with Embedded Machine Learning I : Serializing with pickle and DB setup
Flask with Embedded Machine Learning II : Basic Flask App
Flask with Embedded Machine Learning III : Embedding Classifier
Flask with Embedded Machine Learning IV : Deploy
Flask with Embedded Machine Learning V : Updating the classifier
scikit-learn : Sample of a spam comment filter using SVM - classifying a good one or a bad one
Machine learning algorithms and concepts
Batch gradient descent algorithmSingle Layer Neural Network - Perceptron model on the Iris dataset using Heaviside step activation function
Batch gradient descent versus stochastic gradient descent
Single Layer Neural Network - Adaptive Linear Neuron using linear (identity) activation function with batch gradient descent method
Single Layer Neural Network : Adaptive Linear Neuron using linear (identity) activation function with stochastic gradient descent (SGD)
Logistic Regression
VC (Vapnik-Chervonenkis) Dimension and Shatter
Bias-variance tradeoff
Maximum Likelihood Estimation (MLE)
Neural Networks with backpropagation for XOR using one hidden layer
minHash
tf-idf weight
Natural Language Processing (NLP): Sentiment Analysis I (IMDb & bag-of-words)
Natural Language Processing (NLP): Sentiment Analysis II (tokenization, stemming, and stop words)
Natural Language Processing (NLP): Sentiment Analysis III (training & cross validation)
Natural Language Processing (NLP): Sentiment Analysis IV (out-of-core)
Locality-Sensitive Hashing (LSH) using Cosine Distance (Cosine Similarity)
Artificial Neural Networks (ANN)
[Note] Sources are available at Github - Jupyter notebook files1. Introduction
2. Forward Propagation
3. Gradient Descent
4. Backpropagation of Errors
5. Checking gradient
6. Training via BFGS
7. Overfitting & Regularization
8. Deep Learning I : Image Recognition (Image uploading)
9. Deep Learning II : Image Recognition (Image classification)
10 - Deep Learning III : Deep Learning III : Theano, TensorFlow, and Keras