24
loading...
This website collects cookies to deliver better user experience
#import numpy
import numpy as np
#creating a numpy array a
a = np.array([[1,2,3,4,5],[2,3,4,5,6]])
print(a)
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6]])
type(a)
numpy.ndarray
a.dtype
dtype('int32')
a.size
10
a.ndim
2
a.shape
(2, 5)
Data Type |
Description |
bool_ |
Boolean (true or false) stored as a byte |
int_ |
Default integer type (same as C long; normally either int64 or int32) |
intc |
Identical to C int (normally int32 or int64 |
intp |
Integer used for indexing (same as C size_t; normally either int32 or int64 |
int8 |
Byte (�128 to 127) |
int16 |
Integer (�32768 to 32767) |
int32 |
Integer (�2147483648 to 2147483647) |
int64 |
Integer (�9223372036854775808 to 9223372036854775807) |
uint8 |
Unsigned integer (0 to 255) |
uint16 |
Unsigned integer (0 to 65535 |
uint32 |
Unsigned integer (0 to 4294967295) |
uint64 |
Unsigned integer (0 to 18446744073709551615) |
float_ |
Shorthand for float64 |
float16 |
Half precision float: sign bit, 5-bit exponent, 10-bit mantissa |
float32 |
Single precision float: sign bit, 8-bit exponent, 23-bit mantissa |
float64 |
Double precision float: sign bit, 11-bit exponent, 52-bit mantissa |
complex_ |
Shorthand for complex128 |
complex64 |
Complex number, represented by two 32-bit floats (real and imaginary components |
complex128 |
Complex number, represented by two 64-bit floats (real and imaginary components) |
list1 = [[1+1j,2+2j,3+2j,4+8j,5+6j],[1+1j,3,4,5,2]]
complex_array = np.array(list1)
complex_array.dtype
dtype('complex128')
list1 = [[1,3,5,6],[1,2,4,5]]
cmp = np.array(list1,dtype = float)
print(cmp)
print(cmp.dtype)
[[1. 3. 5. 6.]
[1. 2. 4. 5.]]
dtype('float64')
np.zeros((2,2))
array([[0., 0.],
[0., 0.]])
np.ones((2, 3))
array([[1., 1., 1.],
[1., 1., 1.]])
np.diag([5,6,5,3,4])
array([[5, 0, 0, 0, 0],
[0, 6, 0, 0, 0],
[0, 0, 5, 0, 0],
[0, 0, 0, 3, 0],
[0, 0, 0, 0, 4]])
np.arange(1, 50)
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, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])
np.arange(1, 50,5)
array([ 1, 6, 11, 16, 21, 26, 31, 36, 41, 46])
np.arange(1, 50,2.5)
array([ 1. , 3.5, 6. , 8.5, 11. , 13.5, 16. , 18.5, 21. , 23.5, 26. ,
28.5, 31. , 33.5, 36. , 38.5, 41. , 43.5, 46. , 48.5])
print("Before Applying reshape function")
beforeArray = np.arange(1, 50,2.5)
print(beforeArray.shape)
print("After Applying reshape function")
afterArray = beforeArray.reshape(4,5)
print(afterArray.shape)
Before Applying reshape function
(20,)
After Applying reshape function
(4, 5)
np.random.seed(5)
firstRandomArray = np.random.random((3,3))
print(firstRandomArray)
print("n")
print("Again let's use same seed value 5 n")
np.random.seed(5)
secondRandomArray = np.random.random((3,3))
print(secondRandomArray)
print("n")
print("Now lets use different seed value say 10 n")
np.random.seed(10)
thirdRandomArray = np.random.random((3,3)
print(thirdRandomArray)
[[0.22199317 0.87073231 0.20671916]
[0.91861091 0.48841119 0.61174386]
[0.76590786 0.51841799 0.2968005 ]]
Again let's use the same seed value 5
[[0.22199317 0.87073231 0.20671916]
[0.91861091 0.48841119 0.61174386]
[0.76590786 0.51841799 0.2968005 ]]
Now let's use different seed values say 10
[[0.77132064 0.02075195 0.63364823]
[0.74880388 0.49850701 0.22479665]
[0.19806286 0.76053071 0.16911084]]
a = np.arange(5)
print(a)
b = np.arange(5,10)
print(b)
[0 1 2 3 4]
[5 6 7 8 9]
print("adding any scaler to the array elements")
add = a+15
print(add)
print("adding any two arrays")
add =a+b
print(add)
adding any scaler to the array elements
[15 16 17 18 19]
adding any two arrays
[ 5 7 9 11 13]
print("subtracting any scaler to the array elements")
diff = 15-a # a-15 can is also a valid
print(diff)
print("subtraction between any two arrays")
diff =a-b
print(diff)
subtracting any scaler to the array elements
[15 14 13 12 11]
subtraction between any two arrays
[-5 -5 -5 -5 -5]
print("multiplying any scaler with the array elements")
mul =a*15
print(mul)
print("multiplication between any two arrays")
mul =a*b
print(mul)
multiplying any scaler with the array elements
[ 0 15 30 45 60]
multiplication between any two arrays
[ 0 6 14 24 36]
A = np.ones((3, 3))
B = np.arange(0,9).reshape(3,3)
print("A is")
print(A)
print("B is")
print(B)
A is
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
B is
[[0 1 2]
[3 4 5]
[6 7 8]]
A * B
array([[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]])
product_AxB = np.dot(A,B) # A.dot(B) is also the same
product_BxA = np.dot(B,A) # B.dot(A) is also the same
print(" The matrix Mult AxB is")
print(product_AxB)
print(" The matrix Mult BxA is")
print(product_BxA)
The matrix Mult AxB is
[[ 9. 12. 15.]
[ 9. 12. 15.]
[ 9. 12. 15.]]
The matrix Mult BxA is
[[ 3. 3. 3.]
[12. 12. 12.]
[21. 21. 21.]]
A = np.arange(0,9).reshape(3,3)
print("A is")
print(A)
# transpose calculation
print('n')
A_transpose = A.T
print(" The transpose of A is")
print(A_transpose)
A is
[[0 1 2]
[3 4 5]
[6 7 8]]
The transpose of A is
[[0 3 6]
[1 4 7]
[2 5 8]]
A = np.arange(1,10).reshape(3,3)
determinant = np.linalg.det(A)
print("Determinant of A is")
print(determinant)
Determinant of A is
-9.51619735392994e-16
A = np.arange(1,10).reshape(3,3)
print("A is")
print(A)
# Inverse calculation
print('n')
A_inv = np.linalg.inv(A)
print(" The Inverse of A is")
print(A_inv)
A is
[[1 2 3]
[4 5 6]
[7 8 9]]
The Inverse of A is
[[ 3.15251974e+15 -6.30503948e+15 3.15251974e+15]
[-6.30503948e+15 1.26100790e+16 -6.30503948e+15]
[ 3.15251974e+15 -6.30503948e+15 3.15251974e+15]]
A = np.arange(0,9).reshape(3,3)
print("A is")
print(A)
'''Here A is a singular matrix,
if you try to find its inverse,
you will get an error
you can try finding inverse as done in above example'''
# Pseudo-Inverse calculation
print('n')
A_pinv = np.linalg.pinv(A)
print(" The pseudo-Inverse of A is")
print(A_pinv)
A is
[[0 1 2]
[3 4 5]
[6 7 8]]
The pseudo-Inverse of A is
[[-5.55555556e-01 -1.66666667e-01 2.22222222e-01]
[-5.55555556e-02 1.83880688e-16 5.55555556e-02]
[ 4.44444444e-01 1.66666667e-01 -1.11111111e-01]]
import numpy as np
A = np.arange(1,6,0.6)
print("the array is")
print(A)
print("the sum is")
print(A.sum())
print("the min is")
print(A.min())
print("the max is")
print(A.max())
print("the mean is")
print(A.mean())
print("the std is")
print(A.std())
the array is
[1. 1.6 2.2 2.8 3.4 4. 4.6 5.2 5.8]
the sum is
30.600000000000005
the min is
1.0
the max is
5.800000000000001
the mean is
3.4000000000000004
the std is
1.549193338482967
array = np.array([23,4,23,11,2])
print("element with Index 0 =>",array[0])
print("element with Index 1 =>",array[1])
print("element with Index 2 =>",array[2])
print("element with Index 3 =>",array[3])
print("element with Index 4 =>",array[4])
element with Index 0 => 23
element with Index 1 => 4
element with Index 2 => 23
element with Index 3 => 11
element with Index 4 => 2
array = np.array([23,4,23,11,2])
#size of array is 5
print("element with Index 0 or Index -5 =>",array[-5])
print("element with Index 1 or Index -4 =>",array[-4])
print("element with Index 2 or Index -3 =>",array[-3])
print("element with Index 3 or Index -2 =>",array[-2])
print("element with Index 4 or Index -1 =>",array[-1])
element with Index 0 or Index -5 => 23
element with Index 1 or Index -4 => 4
element with Index 2 or Index -3 => 23
element with Index 3 or Index -2 => 11
element with Index 4 or Index -1 => 2
A = np.arange(1,5).reshape(2,2)
print(A)
print(A[0,0])
print(A[0,1])
print(A[1,0])
print(A[1,1])
[[1 2]
[3 4]]
1
2
3
4
A = np.arange(1, 10)
print(A)
print('n')
print("Slice from index 5 upto 8")
print(A[5:8])
print('n')
print("Slice from index 2 upto 8 with gap of 3")
print(A[2:8:3])
[1 2 3 4 5 6 7 8 9]
Slice from index 5 up to 8
[6 7 8]
Slice from index 2 up to 8 with a gap of 3
[3 6]
A = np.arange(1, 10)
print(A)
print('n')
print("Omitting first and second number")
print(A[::2])
print('n')
print("Omitting first number only")
print(A[:7:2])
print('n')
print("Omitting first and last number ")
print(A[:7:])
[1 2 3 4 5 6 7 8 9]
Omitting first and second number
[1 3 5 7 9]
Omitting the first number only
[1 3 5 7]
Omitting first and the last number
[1 2 3 4 5 6 7]
A = np.arange(10, 19).reshape((3, 3))
print(A)
print('n')
print("sliced array is")
sliced = A[0:2,0:2]
print(sliced)
print(sliced.shape)
[[10 11 12]
[13 14 15]
[16 17 18]]
sliced array is
[[10 11]
[13 14]]
(2, 2)
A = np.arange(1, 10)
for i in A:
print(i)
1
2
3
4
5
6
7
8
9
A = np.arange(1,10)
print(A)
print('n')
print("After reshaping to 3x3")
A_3x3 = A.reshape(3,3)
print(A_3x3)
[1 2 3 4 5 6 7 8 9]
After reshaping to 3x3
[[1 2 3]
[4 5 6]
[7 8 9]]
A_3x3 = np.arange(1,10).reshape(3,3)
print(A_3x3)
print('n')
print("After using ravel ")
A_ravel = A.ravel()
print(A_ravel)
[[1 2 3]
[4 5 6]
[7 8 9]]
After using ravel
[1 2 3 4 5 6 7 8 9]
A_3x3 = np.arange(1,10).reshape(3,3)
print(A_3x3)
print('n')
print("After flattening ")
A_flattened = A.flatten()
print(A_flattened)
[[1 2 3]
[4 5 6]
[7 8 9]]
After flattening
[1 2 3 4 5 6 7 8 9]
A = np.ones((3, 3))
B = np.zeros((3, 3))
np.vstack((A, B))
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
A = np.ones((3, 3))
B = np.zeros((3, 3))
np.hstack((A, B))
array([[1., 1., 1., 0., 0., 0.],
[1., 1., 1., 0., 0., 0.],
[1., 1., 1., 0., 0., 0.]])
A = np.arange(16).reshape((4, 4))
print(A)
[a,b] = np.hsplit(A, 2)
print('n')
print(a)
print('n')
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
A = np.arange(16).reshape((4, 4))
print(A)
print('n')
[a,b] = np.vsplit(A, 2)
print(a)
print('n')
print(b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[0 1 2 3]
[4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
A = np.arange(16).reshape((4, 4))
[A1,A2,A3,A4] = np.split(A,[1,2,3],axis=1)
print(A1)
print('n')
print(A2)
print('n')
print(A3)
print('n')
print(A4)
[[ 0]
[ 4]
[ 8]
[12]]
[[ 1]
[ 5]
[ 9]
[13]]
[[ 2]
[ 6]
[10]
[14]]
[[ 3]
[ 7]
[11]
[15]]
A = np.arange(16).reshape((4, 4))
[A1,A2,A3,A4] = np.split(A,[1,2,3],axis=0)
print(A1)
print('n')
print(A2)
print('n')
print(A3)
print('n')
print(A4)
[[0 1 2 3]]
[[4 5 6 7]]
[[ 8 9 10 11]]
[[12 13 14 15]]
array_to_save = np.array([1,2,3,4,5,6])
np.save("saved_data",array_to_save)
load_data = np.load("saved_data.npy")
print(load_data)
[1 2 3 4 5 6]
data = np.array([ [1.2,2,3], [4,5,6], [7,8,9] ])
np.savetxt("saved_data.csv", data,fmt="%f", delimiter=",")
load_data = np.genfromtxt('saved_data.csv',delimiter=',')
print(load_data)
[[1.2 2. 3. ]
[4. 5. 6. ]
[7. 8. 9. ]]