Ids 6 Experiments
Ids 6 Experiments
Ids 6 Experiments
import numpy as np
[ 4, 2, 5]] )
print(arr) ;
Output :
[[ 1, 2, 3],
[ 4, 2, 5]]
No. of dimensions: 2
Size of array: 6
import numpy as np
array_1d = np.zeros(3)
print(array_1d)
Output:
[0. 0. 0.]
Notice that the elements are having the default data type as the float. That’s why the zeros are 0.
import numpy as np
print(array_2d)
Output:
[[0. 0. 0.]
[0. 0. 0.]]
import numpy as np
print(array_2d_int)
Output:
[[0 0 0]
[0 0 0]]
4. NumPy Array with Tuple Data Type and Zeroes
We can specify the array elements as a tuple and specify their data types too.
import numpy as np
print(array_mix_type)
print(array_mix_type.dtype)
Output:
import numpy as np
array_1d = np.ones(3)
print(array_1d)
Output:
[1. 1. 1.]
Notice that the elements are having the default data type as the float. That’s why the ones are 1. in the
array.
import numpy as np
print(array_2d)
Output:
[[1. 1. 1.]
[1. 1. 1.]]
import numpy as np
print(array_2d_int)
Output:
[[1 1 1]
[1 1 1]]
We can specify the array elements as a tuple and specify their data types too.
import numpy as np
print(array_mix_type)
print(array_mix_type.dtype)
Output:
1.using randint
import numpy as np
# if the shape is not mentioned the output will just be a random integer in the given range
rand_int = np.random.randint(5,10)
print("First array", rand_int)
Output:
First array 7
[66 49 29 73 37]
[39 87 36 20 87]
[76 86 89 69 50]]
[71 50]]
2.using randn
import numpy as np
rand_n2 = np.random.randn(4,5) # outputs an 4x5 array filled with random floating point numbers
Output:
import numpy as np
rand2 = np.random.rand(3,2) # outputs an 3x2 array filled with random floating point values.
Output:
[0.89344256 0.68226333]
[0.46247578 0.09658549]]
import numpy as np
random_choice = np.random.choice(array1)
print(random_choice)
# Output: 3
Code:
import numpy as np
A = np.matrix('1 2 3; 4 5 6')
print("Array created using string is :\n", A)
Output:
1.
import numpy as np
arr = np.arange(0,10,2,float)
print(arr)
Output:
[0. 2. 4. 6. 8.]
2.
import numpy as np
arr = np.arange(10,100,5,int)
Output:
import numpy as np
[ 4, 2, 5]] )
print(arr) ;
2.Printing type of arr object
Output :
[[ 1, 2, 3],
[ 4, 2, 5]]
No. of dimensions: 2
Size of array: 6
import numpy as np
a_1d = np.arange(3)
print(a_1d)
Output
# [0 1 2]
a_2d = np.arange(12).reshape((3, 4))
print(a_2d)
Output
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(a_3d)
Output
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
import numpy as np
b = a.flatten()
print(b)
Output:
[1 2 3 4]
2.Using numpy flatten() method to flatten an array using column-major order
import numpy as np
b = a.flatten(order='F')
print(b)
Output:
[1 3 2 4]
import numpy as np
print(f'Original Array:\n{arr1}')
arr1_transpose = arr1.transpose()
print(f'Transposed Array:\n{arr1_transpose}')
Output:
Original Array:
[[1 2 3]
[4 5 6]]
Transposed Array:
[[1 4]
[2 5]
[3 6]]
3.Expanding and squeezing numpy array
# import numpy
import numpy as np
print(gfg.shape)
print(gfg.shape)
Output :
(2, )
(1, 2)
# import numpy
import numpy as np
print(gfg.shape)
print(gfg.shape)
Output :
(2, 2)
(1, 2, 2)
b) squeezing numpy array
import numpy as np
x = np.arange(9).reshape(1,3,3)
print x
print '\n'
y = np.squeeze(x)
print y
print '\n'
Array X:
[[[0 1 2]
[3 4 5]
[6 7 8]]]
Array Y:
[[0 1 2]
[3 4 5]
[6 7 8]]
(1, 3, 3) (3, 3)
c) sorting numpy array
# importing libraries
import numpy as np
Output :
[[10 1]
[12 15]]
[[10 15]
[ 1 12]]
[ 1 10 12 15]
4.Indexing and slicing numpy array
a) slicing ID arrays
import numpy as np
arr2 = arr[1:6]
print(arr2)
# OutPut
# [ 5 7 9 11 15]
arr2 = arr[3:]
print(arr2)
# Output
# [ 9 11 15 18 22]
arr2 = arr[:5]
print(arr2)
# Output
# [ 3 5 7 9 11]
arr2 = arr[::3]
print(arr2)
# Output
#[ 3 9 18]
arr2 = arr[-4:-2]
print(arr2)
# Output
# [11 15]
arr2 = arr[3::4]
print(arr2)
# Output
# [ 9 22]
[2, 4, 6, 8, 10]])
arr2 = arr[1:,1:3]
print(arr2)
# Output
#[[4 6]]
[2, 4, 6, 8, 10]],
[[5, 7, 8, 9, 2],
[7, 2, 3, 6, 7]]])
arr2 = arr[0,1,0:2]
print(arr2)
# Output:
# [2 4]
a) stacking ndarrays
import numpy as np
a = np.array([1, 2, 3])
b = np.array([5,6,7])
np.vstack((a,b))
Output
array([[1, 2, 3],
[5, 6, 7]])
a = np.array([[1, 2],[3,4]])
b = np.array([[5,6],[7,8]])
np.hstack((a,b))
Output
array([[1, 2, 5, 6],
[3, 4, 7, 8]])
a = np.array([[1, 2],[3,4]])
b = np.array([[5,6],[7,8]])
np.dstack((a,b))
Output
array([[[1, 5],
[4, 8]]])
import numpy as np
a = np.concatenate((arr1, arr2))
print(a)
Output
[[1 2]
[5 6]
[7 8]
[3 4]]
import numpy as np
a = np.append(arr1,arr2, axis=1)
print(a)
Output
[[1 2 7 8]
[5 6 3 4]]
c) broadcasting numpy array
import numpy as np
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c=a*b
print c
[10 40 90 160]
import numpy as np
a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])
print a
print '\n'
print b
print '\n'
print a + b
First array:
[[ 0. 0. 0.]
[ 1. 2. 3.]
[[ 1. 2. 3.]
a) creating dataframe
# import pandas as pd
import pandas as pd
# list of strings
df = pd.DataFrame(lst)
print(df)
Output:
# By default addresses.
import pandas as pd
# Create DataFrame
df = pd.DataFrame(data)
print(df)
Output:
import pandas as pd
df = pd.DataFrame(data)
print(df[['Name', 'Qualification']])
Output
b)concate
import pandas as pd
display('series1:', series1)
# concatenating
display('After concatenating:')
display(pd.concat([series1, series2]))
Output:
import pandas as pd
display('series2:', series2)
# concatenating
display('After concatenating:')
display(pd.concat([series1, series2],
axis = 1))
Output:
import pandas as pd
display('df2:', df2)
# concatenating
display('After concatenating:')
display(pd.concat([df1, df2],
import pandas as pd
display('df1:', df1)
display('df2:', df2)
# concatenating
display('After concatenating:')
display(pd.concat([df1, df2],
axis = 1))
Output:
d) adding new column
import pandas as pd
df['Address'] = address
print(df)
Output:
import pandas as pd
import numpy as np