Numpy array have functions for matrices ,linear algebra ,Fourier Transform. Numpy arrays provide 50x more speed than a python list.
Numpy array have functions for matrices ,linear algebra ,Fourier Transform. Numpy arrays provide 50x more speed than a python list.
pip install numpy
pip install numpy
import numpy
import numpy as np
import numpy as np
pi = np.float32(3.16)
print(type(pi))
print(pi)
<class 'numpy.float32'>
3.16
num_arr = np.array(42)
print(type(num_arr))
print(num_arr)
num_arr = np.array([1, 2, 3, 4, 5])
print(num_arr)
print(type(num_arr))
print(num_arr.shape)
print(num_arr.ndim)
[1 2 3 4 5]
<class 'numpy.ndarray'>
(5,)
1
num_arr1=np.array([1, 2, 3, 4, 5])
num_arr2=np.array([6, 7, 8, 9, 10])
TwoD = np.array( [ num_arr1 , num_arr2 ] )
print(TwoD)
print(TwoD.shape)
print(TwoD.ndim)
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
(2, 5)
2
num_arr1=np.array([1, 2, 3])
num_arr2=np.array([4, 5, 6])
num_arr3=np.array([7, 8, 9])
T3D=np.array([
[num_arr1,num_arr2],
[num_arr2,num_arr3],
[num_arr1,num_arr3]
]
)
print(T3D)
print(T3D.shape)
print(T3D.ndim)
[[[1 2 3]
[4 5 6]]
[[4 5 6]
[7 8 9]]
[[1 2 3]
[7 8 9]]]
(3, 2, 3)
3
Furthermore, we will use ndim attribute to determine the number of dimensions.
np.array([[[[1,2,3], [4,5,6], [7,8,9]], [[10,11,12], [13,14,15], [16,17,18]]], [[[19,20,21], [22,23,24], [25,26,27]], [[28,29,30], [31,32,33], [34,35,36]]]])
print(a.ndim)
a
4
array([[[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]],
[[[19, 20, 21],
[22, 23, 24],
[25, 26, 27]],
[[28, 29, 30],
[31, 32, 33],
[34, 35, 36]]]])
Attribute shape determines the length of each dimension inside the whole array since we know the 4 dimensions are created in the array.
import numpy as np
e = np.array([[[[4, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]])
e.shape
(2, 2, 3, 3)
By using concatenate, we can combine two arrays along a given axis. In the following examples, we will be showcasing two examples where axis value is set to 0 by default, and we have to set any other value manually.
import numpy as np
arr1 = np.array([1.1, 2.1, 3.1])
arr2 = np.array([1.2, 2.2, 3.2])
arr = np.concatenate((arr1, arr2))
arr
array([1.1, 2.1, 3.1, 1.2, 2.2, 3.2])
import numpy as np
arr1 = np.array([[1.1, 2.1, 3.1],[1.2, 2.2, 3.2],[1.3, 2.3, 3.3]])
arr2 = np.array([[1,2,3]])
arr = np.concatenate((arr1, arr2.T),axis=1)
arr
array([[1.1, 2.1, 3.1, 1. ],
[1.2, 2.2, 3.2, 2. ],
[1.3, 2.3, 3.3, 3. ]])
The Numpy stack allows us to join multiple arrays. And we can also use an axis with this method similar to concatenate.
import numpy as np
arr1 = np.array([1, 2, 3,4])
arr2 = np.array([4, 5, 6,7])
arr3 = np.array([7, 8, 9,10])
arr4 = np.array([10, 11, 12,13])
arr4 = np.array([11, 12, 13,14])
arr = np.stack((arr1, arr2 , arr3 , arr4 ),axis=0)
print(arr)
[[ 1 2 3 4]
[ 4 5 6 7]
[ 7 8 9 10]
[11 12 13 14]]
import numpy as np
arr1 = np.array([1, 2, 3,4])
arr2 = np.array([4, 5, 6,7])
arr3 = np.array([7, 8, 9,10])
arr4 = np.array([10, 11, 12,13])
arr4 = np.array([11, 12, 13,14])
arr = np.stack((arr1, arr2 , arr3 , arr4 ),axis=1)
print(arr)
[[ 1 4 7 11]
[ 2 5 8 12]
[ 3 6 9 13]
[ 4 7 10 14]]
import numpy as np
arr1 = np.array([1, 2, 3,4])
arr2 = np.array([4, 5, 6,7])
arr3 = np.array([7, 8, 9,10])
arr4 = np.array([10, 11, 12,13])
arr4 = np.array([11, 12, 13,14])
arr = np.stack((arr1, arr2 , arr3 , arr4 ),axis=-1)
print(arr)
[[ 1 4 7 11]
[ 2 5 8 12]
[ 3 6 9 13]
[ 4 7 10 14]]
Also, take note of axis 1 and axis-1 output
Depth stack combines multiple arrays depth-wise
import numpy as np
arr1 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
arr2 = np.array([[4, 5, 6],[4, 5, 6],[4, 5, 6]])
arr = np.dstack(( arr1 , arr2 ))
print(arr)
[[[1 4]
[2 5]
[3 6]]
[[1 4]
[2 5]
[3 6]]
[[1 4]
[2 5]
[3 6]]]
Horizontal stack combines arrays in a horizontal direction along columns. This can be similar to concatenate.
import numpy as np
arr1 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
arr2 = np.array([[4, 5, 6],[4, 5, 6],[4, 5, 6]])
arr = np.hstack(( arr1 , arr2 ))
print(arr)
[[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]]
Horizontal stack combines arrays in a horizontal direction along columns. And it is obviously similar to axis 0.
import numpy as np
arr1 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
arr2 = np.array([[4, 5, 6],[4, 5, 6],[4, 5, 6]])
arr = np.vstack(( arr1 , arr2 ))
print(arr)
[[1 2 3]
[1 2 3]
[1 2 3]
[4 5 6]
[4 5 6]
[4 5 6]]
Horizontal stack combines arrays in a horizontal direction along columns. And it is obviously similar to axis 0.
import numpy as np
arr1 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
arr2 = np.array([[4, 5, 6],[4, 5, 6],[4, 5, 6]])
arr = np.vstack(( arr1 , arr2 ))
print(arr)
[[1 2 3]
[1 2 3]
[1 2 3]
[4 5 6]
[4 5 6]
[4 5 6]]
ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.
What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!
Start using NotebookLM today and embark on a smarter, more efficient learning journey!
This can be a super guide for you to start and excel in your data science career.
Solve this quiz for testing Manova Basics
Test your knowledge on pandas groupby with this quiz
Observe the dataset and try to solve the Visualization quiz on it
To perform ANCOVA (Analysis of Covariance) with a dataset that includes multiple types of variables, you’ll need to ensure your dependent variable is continuous, and you can include categorical variables as factors. Below is an example using the statsmodels library in Python: Mock Dataset Let’s create a dataset with a mix of variable types: Performing…
How useful was this post? Click on a star to rate it! Submit Rating
Complete the code by dragging and dropping the correct functions
Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain. Defining a Function: In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses,…
Mastering indexing will significantly boost your data manipulation and analysis skills, a crucial step in your data science journey.
Stable Diffusion Models: Where Art and AI Collide Artificial Intelligence meets creativity in the fascinating realm of Stable Diffusion Models. These innovative models take text descriptions and bring them to life in the form of detailed and realistic images. Let’s embark on a journey to understand the magic behind Stable Diffusion in a way that’s…