Skip to content
Sign upLog in
← Back to Community

NumPy For Beginners

Profile icon
dillonjoshua68

NumPy Basics

This contains 9 Topics
Basics/Intro, Creating arrays, Indexing, Slicing, Data types, Copy vs View, Array shape, and Array reshape

NumPy aims to provide an array object that is up to 50x faster that traditional Python lists.

The array object in NumPy is called ndarray

NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.
This behavior is called locality of reference on computer science.
In python version 3.4 and later Pip is already installed... it’s a package manager for python packages or modules.

Ex:
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)

To check numpy version use the version attribute.
print(numpy.version)

if we print the type of the array from above (arr) we get:
numpy.ndarray

A dimension in arrays is one level of depth (nested arrays).
nested arrays - arrays that have arrays as their elements.

Dimensions in arrays:
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Ex:
import numpy as np
arr = np.array(42)
print(arr)

1-D array is an array that has 0-D arrays as its elements is called uni-dimensional or 1-D array
Ex:
arr = np.array([1, 2, 3, 4, 5])
print(arr)

2-D arrays are arrays that have 1-D arrays as its elements. They are often used to represent matrix or 2nd order tensors.
Ex:
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

NumPy has a whole sub module dedicated towards matrix operations called numpy.mat

3-D array is an array that has 2-D arrays (matrices) as its elements. Often used to represent a 3rd order tensor.
Ex:
arr = np.array([[1, 2, 3], [4, 5, 6]], [1, 2, 3], [4, 5, 6]])
print(arr)

To check the number of dimensions do this:
a = np.array(42) # this is the variable we’re checking
print(a.ndim) # this is how to check

To add more dimensions we have to do this:
arr = np.array([1, 2, 3,4], ndmin = 8)

pass an argument called ndmin.

specify the number of dimensions

NumPy arrays follow the same indexing rules as normal arrays/lists. Even negative indexing.

Ex:
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])

this gets the numbers in those indexes and adds them.

It also follows the same rules for slicing...[start🔚step].
If we don’t pass any parameter for each the default for start is 0, for step is 1, for end is the length of the array.

For 2-D and above we do this:
print(arr[1, 1:8]). This prints the values in the first array, and prints the elements in the positions 1-7, 8 is ignored.

print(arr[0:2, 2]) - this returns the second index from both elements(arrays inside the array).

NumPy has extra data types, and refer to data types with one character... i for integer, etc.

i - integer
b - bool
u - unsigned int
f - float
c - complex float
m - timedelta
M - datetime
O - object
S - string
U - Unicode string
V - fixed chunk of memory for other type (void).

To check the data type of an array do this
print(arr.dtype)
we can even create an array with a specific data type:
Ex:
arr = np.array([1, 2, 3, 4], dtype = ‘S’)
print(arr)
print(arr.dtype)

For i, u, f, S, and U we can also define size:
Ex:
arr = np.array([1, 2, 3, 4], dtype = ‘i4’)

To convert data type on existing arrays we do this:
We need to store it in a variable
The astype() methods creates a copy of the array, and Takes data type as a parameter.

Copy vs View: copy.. copies the array, and any changes made won’t affect the original. And if the original is changed it won’t affect the copy. Whereas View is just view of the original array, and any changes made to either will affect the other one.

NumPy has the attribute base that returns None if the array owns the data... otherwise it’ll refer to the original object. The Copy returns none. View return the original
Ex:
x = arr.copy()
y = arr.view()
print(x.base, y.base)

The shape of an array is the number of elements in each dimension.

They have an attribute called shape that returns a tuple having the number of corresponding elements.
(2, 4) - means that the array has 2 elements, and each dimension has 4 elements.

Using the reshape method we can add or remove dimensions or change number of elements in each dimension.
ex:
newarr = arr.reshape(4,3) - the outermost array will have 4 dimensions and 3 elements in each.
print(newarr)
Trying to convert a 1-D array with elements to a 2-D array with 3 elements in each dimension will raise an error. This applies to others as well.

You are allowed to have one “unknown” dimension... meaning that you don’t have to specify an exact number for one of the dimensions in the reshape method. Pass -1 as the value, and NumPy will calculate this number for you.
We cannot pass -1 to more than one dimension.

Flattening an array means converting a multidimensional array into a 1-D array. To flatten an array we can use reshape(-1)

Voters
Profile icon
jimmyChohan
Profile icon
AlexWinter5
Profile icon
KarlaValverde
Profile icon
tibawaad
Profile icon
NasirAl
Profile icon
AllysaPacunio
Profile icon
JASMINI-NAYAKNA
Profile icon
derschlas
Profile icon
BaronFamily
Profile icon
DynamicSquid
Comments
hotnewtop
Profile icon
PlasticRanger

acrylic plastics It is one of the most useful thermoplastics in the world with more than 20,000 tons of products every year.

Profile icon
DynamicSquid

Cool! But just a tip, you might wanna use some more markdown.

Profile icon
dillonjoshua68

Ya I should’ve done that, thanks for the recommendation

@DynamicSquid

Profile icon
dillonjoshua68

I’ll, try to fix it today

@DynamicSquid

Profile icon
loginsdetail

Cool! and wonderful..All content was wonderful. https://loginsdetail.com/invisalign-doctor-login/

image

Profile icon
shrutibae

A card collecting battle game with exquisite character illustration. download Destiny Child MOD APK Unlimited Money, Crystal.

Profile icon
CapraFalconeri

i really like it thank you so much for sharing PowerDirector MOD APK

Profile icon
manisingh21

The information you have shared here is very helpful. I really like and appreciate your work.
https://installingwhatsapp.com/fmwhatsapp-apk/
https://installingwhatsapp.com/nswhatsapp-3d-apk/

Profile icon
manisingh21

The information you have shared here is very helpful. I really like and appreciate your work.
https://installingwhatsapp.com/fmwhatsapp-apk/
https://installingwhatsapp.com/nswhatsapp-3d-apk/

Profile icon
ShinraTensei5

Thanks for sharing the basics with us. It's so helpful for me. Aero WhatsApp

Profile icon
Eidulfitr2022

It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing. https://eidulfitr2022.com/

Profile icon
jimmyChohan

hope You Are very well. I Like that Type of Content Like Math and Coding. I am Coder of App Development That's way I share my Work.https://queenmodapk.com/hempire-mod-apk/

Profile icon
MichelleRichers

Thanks for the post and great tips: even I also think that hard work is the most important aspect of getting success mccqe part 1

Profile icon
FelicityLetitia

Me gusta escuchar música, me hace sentir relajado. Puede descargar el juego 18+ en el enlace de descarga gratuita en https://modpree.com/teaching-feeling/

Profile icon
vishlsen

I was looking for the best video editing app for my mobile device and, my friend suggested I use Kinemaster Pro,(https://modpapa.com/kinemaster/) from Modpapa.com and it was really good for me.

Profile icon
edeals2002

It's like you read my mind! You seem to grasp a lot approximately this like you wrote the e-book in it or something. Thanks for sharing this information https://e-deals.tv/

Profile icon
gegukaze

The massively popular video game Fortnite has swept the globe. Despite the fact that the game may be downloaded and played for free, many players wish to obtain free V Bucks, the in-game cash. However, cheating or hacking isn't always the best solution. Online V Bucks codes generators are straightforward to use and free.

Profile icon
Make-MastiMasti

Well this one is very usefull and informative https://www.techcng.com

Profile icon
Cherishy

According to expert and professional gamers it's now available Robux special edition Robux with media partner for every single one of Roblox Gamers around the world with Android phone users and iOS iPhone or PC Gaming.

Profile icon
ghkanwar

Is there anyone who can code app like this:
bellara injector

Profile icon
reduaa

We offer best services for packers and movers services in Pune service providers in India. You can find suitable options for home, office, vehicle and other relocation needs.
https://cityrelocationservices.co.in

Profile icon
reduaa

We are providing online marketplace For Buy and sell websites, android or ios apps across india
https://actinium.in

Profile icon
calismith1

This is amazing info.Please check this: https://lotteryresulthouse.com/

Profile icon
NovaSmith2

Thanks for sharing this great info. Please check this: https://apkvow.com/nicoo-apk/com-naviemu-nicoo/

Profile icon
senivasan

If your carrier allows it, you can view your APN settings in one of the following locations: Settings > Cellular > Cellular Data Options > Cellular Network. Settings > Mobile Data > Mobile Data Options > Mobile Data Network

https://www.apnsettingsandroid.com/
https://www.corporateofficehqaddress.com/

Profile icon
senivasan

On this page we'll show you some common errors and how to fix them, some great tips and apps to help you get the most out of your Chromebook, and a list of Chromebook supporting apps.

https://chromebookappsinfo.com/

Profile icon
barryharris33

on the music player system of deezer premium apk has also been applied

Profile icon
asadali34

Wonderful information! I found amazing information on the blogs i suggest everyone
Regards:installinggames

Profile icon
NTSPLIVE
Profile icon
laranohara

This is very good info. Please check this: https://whatsappinstalling.com/ar/whatsapp-plus/

Profile icon
EjazAhmed6

This is amazing info.Thanks for this so much info. You can get more info from here: https://apkvow.com/

Profile icon
megamegamodsapk

I find this very interesting because this blog is very helpful for me. I look waiting to another informative poster. @Clash of Clans MOD APK

Profile icon
gochi88

Hey, I really like this blog and it was very helpful for me to thank you so much. https://relaxmodapk.com/youtube-vanced-apk/