NumPy Syntax

NumPy Logo

NumPy Syntax


Import

Importing Module

import numpy as np

Read Data

np.genfromtxt(...)

Example

data = np.genfromtxt("file_name.csv", delimiter=",", skip_header=1, dtype="U75")

Arrays

Creating Arrays

np.array(...)

Example

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

Array Shape

np_arr.shape

Array Data Type

np_arr.dtype

Array Comparison

# returns bool vector
np_arr02 = (np_arr01 == value)
Example
# selecting rows using bool vector
selected_rows = np_arr03[np_arr02,:]

# replacing values using bool vector
np_arr03[np_arr02,:] = value

Converting Data Types

np_obj.astype(type)

Computing with NumPy

Example
# sum|mean|max|min etc, 0|1 = row|col
np_obj.sum(axis=0)
Written on June 5, 2017