Image for post
Image for post

Python Numpy

Overview & Getting started

What is it ?

NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.

From lists to Numpy Arrays

list = [1,2,3,4,5,6]
print(list)
>>>
[1, 2, 3, 4, 5, 6]
import numpy as npnp_array = np.array([1, 2, 3, 4, 5, 6])
print(np_array)
>>>
[1 2 3 4 5 6]

Do note the lack of commas which tell us we are dealing with something different than a regular list.

Creating simple arrays:

Type and type conversion

You can find all the types and more information here:Numpy data Types

Dimensions and Shape

grocery_list = np.array(['eggs', 'milk', 'cereal', 'bacon'])print("Dimensions:", grocery_list.ndim)
>>> Dimensions: 1
print("Shape:", grocery_list.shape)
Dimensions: 1
>>> Shape: (4,)
chartData = X,Y = np.array([[1, 2, 3, 4],[2, 4, 6, 8]])Dimensions: 2
Shape: (2, 4) length: 2 = 2 dimensions, first axe has 2 elements [1,2,3,4,] and [2,4,6,8], the second axe has 4 elements, also note we are assigning X and Y values upon creation which is a common way to pass values to a chart...
print(X):
>>> [1 2 3 4]
print(Y):
>>> [2 4 6 8]
point_in_space = X,Y,Z = np.array([[[1]],[[2]],[[3]]])Dimensions: 3
Shape: (3, 1, 1)
point_in_spaceWithComment = X, Y, Z, comment = np.array([
[[[1]]],
[[[2]]],
[[[3]]],
[[['My Favorite Point']]]
])
Dimensions: 4
Shape: (4, 1, 1, 1)
points_in_spaceWithComments = np.array([
[2, 4, 2, 'Comment1'],
[4, 6, 8, 'Comment2'],
[1, 4, 3, 'Comment3']
])
Dimensions: 2
Shape: (3, 4)

Data I/O and Indexing

Dealing with multiple dimensions:

Reshaping

What else ?

Written by

AI, Software Developer, Designer : www.k3no.com

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