Skip to content

Instantly share code, notes, and snippets.

@HamoyeHQ
Last active August 26, 2021 09:23
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 46 You must be signed in to fork a gist
  • Save HamoyeHQ/3dc027be9818e4bc64f70586e7bb0bf8 to your computer and use it in GitHub Desktop.
Save HamoyeHQ/3dc027be9818e4bc64f70586e7bb0bf8 to your computer and use it in GitHub Desktop.
Stage A-Lesson 2
# convention for importing numpy
import numpy as np
arr = [6, 7, 8, 9]
print(type(arr)) # prints <class 'list'>
a = np.array(arr)
print(type(a)) # prints <class 'numpy.ndarray'>
print(a.shape) # prints (4,) - a is a 1d array with 4 items
print(a.dtype) # prints int64
# get the dimension of a with ndim
print(a.ndim) # prints 1
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b) # prints [[1 2 3]
[4 5 6]]
print(b.ndim) # prints 2
b.shape # prints (2, 3) - b a 2d array with 2 rows and 3 columns
import numpy as np
arr = [6, 7, 8, 9]
print(type(arr)) # prints <class 'list'>
a = np.array(arr)
print(type(a)) # prints <class 'numpy.ndarray'>
print(a.shape) # prints (4,) - a is a 1d array with 4 items
print(a.dtype) # prints int64
# get the dimension of a with ndim
print(a.ndim) # prints 1
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b) # prints [[1 2 3]
[4 5 6]]
print(b.ndim) # prints 2
b.shape # prints (2, 3) - b a 2d array with 2 rows and 3 columns
# a 2x3 array with random values
np.random.random((2, 3)) =
array([[0.60793904, 0.02881965, 0.73022145],
[0.34183628, 0.63274067, 0.07945224]])
# a 2x3 array of zeros
np.zeros((2, 3)) = array([[0., 0., 0.],[0., 0., 0.]])
# a 2x3 array of ones
np.zeros((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])
# a 2x3 array of ones
np.zeros((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])
# a 3x3 identity matrix
np.zeros(3) = array([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])
#The elements in the example arrays above can be accessed by indexing like lists in Python such that:
a[0] = 6, a[3] = 9, b[0, 0] = 1 , b[1, 2] = 6 c[0, 1] = 8.
#Elements in arrays can also be retrieved by slicing rows and columns or a combination of indexing and slicing.
d[1, 0:2] = array([9., 8.])
e = np.array([[10, 11, 12],[13, 14, 15],
[16, 17, 18],[19, 20, 21]])
# slicing
e[:3, :2] = array([[10, 11], [13, 14],[16, 17]])
#There are other advanced methods of indexing which are shown below.
# integer indexing
e[[2, 0, 3, 1],[2, 1, 0, 2]] = array([18, 11, 19, 15])
# boolean indexing meeting a specified condition
e[e>15] = array([16, 17, 18, 19, 20, 21])
c = np.array([[9.0, 8.0, 7.0], [1.0, 2.0, 3.0]])
d = np.array([[4.0, 5.0, 6.0], [9.0, 8.0, 7.0]])
c + d = array([[13., 13., 13.], c * d = array([[36., 40., 42.],
[ 10., 10., 10.]]) [9., 16., 21.]])
5 / d = array([[1.25 , 1. , 0.83333333],
[0.55555556, 0.625 , 0.71428571]])
c ** 2 = array([[81., 64., 49.],[ 1., 4., 9.]])
@DILH-bit
Copy link

Please can anyone help explain better the below code for integer indexing,
e[[2, 0, 3, 1],[2, 1, 0, 2]] = array([18, 11, 19, 15])

e = np.array([[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
[19, 20, 21]])

Look at this way: arrays can be indexed using [row label/index, column label/index]
This implies that e[[2, 0, 3, 1], [2, 1, 0, 2]] = e[row index, column index]
row index=[2,0,3,1]
column index =[2,1,0,2]
To solve it, we pick the element at row2 and column2
Then the element at row0 and column1
Then row3 and column0
Then row1 and column2
And we get array([18, 11, 19, 15])

Thanks alot, this is helpful

@DILH-bit
Copy link

DILH-bit commented Jul 10, 2020

Please can anyone help explain better the below code for integer indexing,
e[[2, 0, 3, 1],[2, 1, 0, 2]] = array([18, 11, 19, 15])

So what happens basically is that, it picked the first number from the first bracket (2) and the first number in the second bracket(2) which can also be translated as e[2, 2] = 18
Then go the next number from the first bracket (0) and the second number in the second bracket (1) which translates to e[0, 1] = 11
And so on... I hope you understand?

I appreciate your assistance, thank you. Though not so clear

@Ogochukwu-bit
Copy link

Thanks Dave
Please who else run this
np.random.random((2, 3)) =
array([[0.60793904, 0.02881965, 0.73022145],
[0.34183628, 0.63274067, 0.07945224]])
And what did it say?

It won't give the same answer because it is generating random numbers

Yes, I understand what random numbers are.
Thanks

@Elfinoto07
Copy link

please is there anyone who could help explain "Indexing with arrays & Using arrays for data processing.py".. I just need a little bit of clarity . Thanks in anticipation

@Efeberosemary
Copy link

I am seeing people having issues with no
Random.random
I wrote mine directly this way
Print (np.random.random((2,3))) on Jupyter notebook and it ran...so probably you should restart your kernel, else the code is right

@Efeberosemary
Copy link

Indexing with array is just calling out an element of the array you want to work with
For example if you have a 2 by 3 array(2,3)
You have two rows, 3 column
When indexing this array, for example you want to work with an element on the first row, second column, you call it this way (0,1)
Indexing starts from zero not your regular 1....
Then if you want to call out an element in the 2nd row, third column, it's like this (1,2)

@Efeberosemary
Copy link

I am seeing people having issues with no
Random.random
I wrote mine directly this way
Print (np.random.random((2,3))) on Jupyter notebook and it ran...so probably you should restart your kernel, else the code is right

@cryptobills
Copy link

The result c= np.random.random(2,3)
print(c)........will be different for different people because the program is printing random numbers.

Also try and configure Tab autocomplete on anaconda, it makes work easier

@Efeberosemary
Copy link

Efeberosemary commented Jul 12, 2020 via email

@Chioma-Nwandiko
Copy link

Thanks to everyone that took time to point out the corrections, it has really made understanding a lot easier.

@Chioma-Nwandiko
Copy link

please is there anyone who could help explain "Indexing with arrays & Using arrays for data processing.py".. I just need a little bit of clarity . Thanks in anticipation

Do you still have issues with this?

@Luke-Tetteh
Copy link

Please make these corrections in your code

a 2x3 array of zeros

np.zeros((2,3))

a 2x3 array with ones

np.ones((2, 3))

a 3x3 identity matrix

np.identity(3)

@Chudyn
Copy link

Chudyn commented Jul 12, 2020

@froschi95, that was a very helpful explanation.

@lawalstyle
Copy link

the code for the 3x3 identity array is
Untitled
import numpy as np
array_2D=np.identity(3)
print('3x3 matrix:')
print(array_2D)

for the(2,3) that displays ones(1)
just type np.ones((2, 3))

@Helen1000
Copy link

This really helped, thanks to everyone for their corrections.

@Photon12
Copy link

Please how do I import numpy because mine is not.

import numpy as np

@Photon12
Copy link

Thanks to everyone who took time to comment here.

Edit: who has been able to download the code snippet pdf?

Do you still need it?

@DILH-bit
Copy link

Please how do I import numpy because mine is not.

import numpy as np
You can install (pip install numpy) first then import (import numpy as np) just in case it's not installed

@gogzicole
Copy link

Good day,
I think this is supposed to be showcasing empty(), zeros(), ones(), full(). Instead we have repetitions of np.zeros

a 2x3 array of zeros

np.zeros((2, 3)) = array([[0., 0., 0.],[0., 0., 0.]])

a 2x3 array of ones

np.zeros((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])

a 2x3 array of ones

np.zeros((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])

a 3x3 identity matrix

np.zeros(3) = array([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])

You are correct and also there is no need for the equal sign and the expressions on the right hand side. I think what they intend was a comment. eg: np.ones((2,3)) #[[1. , 1. , 1.],[1. , 1. , 1.]]

I am seeing people having issues with no
Random.random
I wrote mine directly this way
Print (np.random.random((2,3))) on Jupyter notebook and it ran...so probably you should restart your kernel, else the code is right

look at your importation statement carefully, importations are case sensitive so using an"R" instead of an"r" would cause an error or else import another module called Random.

@kokovariax
Copy link

thank you

@Mawaxyl
Copy link

Mawaxyl commented Jul 14, 2020

full_value = np.full((2,3), 99)

full_value

array([[99, 99, 99],
[99, 99, 99]])
thanks @ayodejilbilmua. I have been thinking on how to go about the use full() in numpy

@suntimo
Copy link

suntimo commented Jul 14, 2020

Good day,
I think this is supposed to be showcasing empty(), zeros(), ones(), full(). Instead we have repetitions of np.zeros

a 2x3 array of zeros

np.zeros((2, 3)) = array([[0., 0., 0.],[0., 0., 0.]])

a 2x3 array of ones

np.zeros((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])

a 2x3 array of ones

np.zeros((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])

a 3x3 identity matrix

np.zeros(3) = array([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])

yes sir. You are right.

@suntimo
Copy link

suntimo commented Jul 14, 2020

Please can anyone help explain better the below code for integer indexing,
e[[2, 0, 3, 1],[2, 1, 0, 2]] = array([18, 11, 19, 15])
Ans: Note that you are np.array pick [row, column]. Hence [2, 2] = 18 implies row2, column2. Note, first row is 0 and first column is also 0. That is how indexing is done in python. Do it for the rest. The next number should be [0, 1] = 11. I hope this help.

@Adeajakaye
Copy link

This is quite self explanatory. Thanks

@kolardzy
Copy link

a 2x3 array of ones

np.ones((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])

Weldone guys

@Maveriyke
Copy link

Good day,
I think this is supposed to be showcasing empty(), zeros(), ones(), full(). Instead we have repetitions of np.zeros

a 2x3 array of zeros

np.zeros((2, 3)) = array([[0., 0., 0.],[0., 0., 0.]])

a 2x3 array of ones

np.zeros((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])

a 2x3 array of ones

np.zeros((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])

a 3x3 identity matrix

np.zeros(3) = array([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])

Thought I was alone in figuring this out.
Good work guys!

@Wolemercy
Copy link

Integer indexing has a ridiculously cool vibe!

@Ikenergy
Copy link

The 'Star' omment was very helpful. Big ups

@Omoleye-J
Copy link

It took me a while to get familiar with the lessons, the explanations here really went a long way in making things easier for me. Muchas gracias.

@AlxeverCodeX
Copy link

who can explain this for me

#The elements in the example arrays above can be accessed by indexing like lists in Python such that:
a[0] = 6, a[3] = 9, b[0, 0] = 1 , b[1, 2] = 6 c[0, 1] = 8.

#Elements in arrays can also be retrieved by slicing rows and columns or a combination of indexing and slicing.
d[1, 0:2] = array([9., 8.])

e = np.array([[10, 11, 12],[13, 14, 15],
[16, 17, 18],[19, 20, 21]])

slicing

e[:3, :2] = array([[10, 11], [13, 14],[16, 17]])

#There are other advanced methods of indexing which are shown below.

integer indexing

e[[2, 0, 3, 1],[2, 1, 0, 2]] = array([18, 11, 19, 15])

boolean indexing meeting a specified condition

e[e>15] = array([16, 17, 18, 19, 20, 21])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment