-
-
Save HamoyeHQ/3dc027be9818e4bc64f70586e7bb0bf8 to your computer and use it in GitHub Desktop.
# 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.]]) |
Thanks to everyone that took time to point out the corrections, it has really made understanding a lot easier.
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?
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)
@froschi95, that was a very helpful explanation.
This really helped, thanks to everyone for their corrections.
Please how do I import numpy because mine is not.
import numpy as np
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?
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
Good day,
I think this is supposed to be showcasing empty(), zeros(), ones(), full(). Instead we have repetitions of np.zerosa 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.
thank you
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
Good day,
I think this is supposed to be showcasing empty(), zeros(), ones(), full(). Instead we have repetitions of np.zerosa 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.
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.
This is quite self explanatory. Thanks
a 2x3 array of ones
np.ones((2, 3)) = array([[1., 1., 1.], [1., 1., 1.]])
Weldone guys
Good day,
I think this is supposed to be showcasing empty(), zeros(), ones(), full(). Instead we have repetitions of np.zerosa 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!
Integer indexing has a ridiculously cool vibe!
The 'Star' omment was very helpful. Big ups
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.
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])
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