Categories: NumpyPython

Introducing Numpy array in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

Numpy is a large module in Python programming. It handles mainly numeric data analysis, and is also the basis of another large module, Pandas in Python. The heart of Numpy is data object type ndarray. Numpy array stores same type of data in an array. An array usually has dimension (how many axes, or rank), its data type (integer, float, string, or more), and shape (size for each axis in its rank). Numpy array can be created using array() function. Next code example show how to create simple one-dimensional array with Numpy in Python from list as input.

#import Numpy module
import numpy as np
#create a one-dimensional array, from a list of 5 items
a1 = np.array([7, 210, 23,53,99])
#show the array
a1
#output
array([  7, 210,  23,  53,  99])
#type of array object
type(a1)
#output
numpy.ndarray
#type of data in the array
a1.dtype
#output
dtype('int32')
#dimension of array
a1.ndim
#output
1
#how many elements in total stored in the array
a1.size
#output
5
#size of each axis of the dimension
a1.shape
#output
(5,)

Numpy ndarray can be built with more than one dimension. The next example shows creation of a two-dimensional array,

#a two-dimensional array creation
a2 = np.array([[1, 3,5], [2, 4,6]])
#show the element type of array
a2.dtype
#output
dtype('int32')
#dimension of the array
a2.ndim
#output
2
#size, or total elements of the array
a2.size
6
#shape of the array
a2.shape
#output
(2, 3)

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Fine tuning a Hugging Face transformer model for multi-track music generation

To train a PyTorch Transformer for multi-track music generation, the standard paradigm is to convert…

7 days ago

what are Transformer, LLM , and GPT

In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…

2 weeks ago

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

2 weeks ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

2 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

11 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago