Skip to content
← 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
NasirAl
Profile icon
AllysaPacunio
Profile icon
JASMINI-NAYAKNA
Profile icon
derschlas
Profile icon
BaronFamily
Profile icon
DynamicSquid
Profile icon
dillonjoshua68
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
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
NickJohnson19

Your post is very informative and effective. I got what I was looking for. Here to say thank you. Thanks for sharing valuable content. Keep sharing more and more. Regards https://gbwamod.com/

Profile icon
etrnen

Great tutorial. I have one question. Do you maybe know how to use this Office 365 Trial Resetter ? Since you have posted a great tutorial here you may help me to reset the trial period of my Office 365.

Profile icon
optech

Here we provide some best YoWhatsapp Apk which is the best alternative and have Extra features.

Profile icon
optech

This is an One place for Latest News and Technology Tips & Tricks OneNewsTech

Profile icon
optech

Whatsapp Groups are very helpful, whenever you want to spread any news, trends on Whatsapp Group Link internet.

Profile icon
optech

Here we provide some best Technical guide, information, tips and tricks for all the internet, android, pc & more.
Track Mobile number exact Location USA

Profile icon
Dana7659

Here all features are totally unlocked and unlimited. https://thunderapk.com/damon-ps2-pro-mod-apk/

Profile icon
RichardMiller4

Flattening an array means converting a multidimensional array into a 1-D array. To flatten an array we can use reshape(-1) That's true
https://qatar.carmatec.com/best-mobile-app-development-company-qatar/

Profile icon
bristo

Num players should have beginner's knowledge to propel. It is incredibly useful for me as well. The coding technique has been used to build FMWhatsApp. Try https://www.rogtechs.com/fmwhatsapp-download/

Profile icon
ayaanly

Can this language help me build mod like FMWA? Here https://flosshype.com/fmwhatsapp/

Profile icon
ayaanly

I have found the latest version of JTWhatsApp APK Here- https://www.avoid.uk.net/jtwhatsapp-apk-download/

Profile icon
edna12323

Thank you for giving readers a very good and inspiring article if you want to learn that application then click https://apksunny.com/infinite-design/

Profile icon
EdwinGitonga

Professional custom writing service at affordable fees. Only Masters and PhD level experts on standby 24/7 to serve you. Check us today for a live engagement: https://essaybishops.com/dissertation-help

Profile icon
resistance82

https://www.resistancecalculator.com/2020/09/pcb-trace-resistance-calculator.html will help you to calculate the thickness and proper resistance of PCB tracks.

Profile icon
depigiv711

Latest version yowhatsapp is actually a WhatsApp mod that has some advanced and unique features which you can’t find in WhatsApp.

Profile icon
Lordkali

Thanks for the quality information and please check- https://gbplusmod.com/download-fm-whatsapp-apk/.. Also share more similar pps

Profile icon
ayaanly

Ya i am also facing such kinds of problem, but from your article it helps me a lot. Your mentioned very useful programs which can get out of the errors. You can check out more information from this website - https://www.avoid.uk.net/fmwhatsapp/

Profile icon
MikeTyson6
Profile icon
EnvironmentTalk

https://onl.st/Best-Slogans-on-Air-Pollution/ will give you best slogans on air pollution

Profile icon
EnvironmentTalk

https://www.environmenttalks.com/2019/09/slogans-on-environment.html will help us to aware people about ill effect of pollution on our environment.

Profile icon
erikinsobron

Wonderful all contents. better python language used and have given best example. I like this code in the language

https://easynamesgenerator.com/

Profile icon
DrDejonHahn

Did you know that our Nursing assignment help connects you with a wide range of academic writing services at a reasonable price. Our team of nursing writers is highly dedicated to their work and always provides the best solution for nursing papers. https://buyonlinenursingpapers.com/

Profile icon
kittymitty

Ya i am also facing such kinds of problem, but from your article it helps me a lot. Your mentioned very useful programs which can get out of the errors. You can check out more information from this website - https://gbmod.net/whatsapp-aero-apk/

Profile icon
Susannolen

Wonderful all contents. better python language used and have given best example. I like this code in the language. very difficult coding. but you have a nice solution. i appreciate . keep it up and more Give information. the content is plagiarism free. i like your
https://assignmentshelpwebsite.com

Profile icon
hoobs

wonderful all contents. better python language used and have given best example. I like this code in the language. very difficult coding. but you have a nice solution. i appreciate . keep it up and more Give information.https://gbplusapps.com/yowhatsapp-apk-download/

Profile icon
shaunreill

Thanks for sharing this post. Students who are not confident about their grammar knowledge or writing skills can take Nursing Assignment Help from https://superiornursingpapers.com expert writers

Profile icon
AnaBella1

Thanks for the information you have provided here, keep sharing stuff like this. https://clashmod.net/top-war-battle-game-mod-apk/

Profile icon
isabellacla

Thanks for sharing this amazing post. Check this: https://somaapp.org/virtualxposed-apk/