Creating a NumPy Array
- Basic ndarray
- Array of zeros
- Array of ones
- Random numbers in ndarray
- An array of your choice
- Imatrix in NumPy
- Evenly spaced ndarray
Program
import numpy as np
#Creating basic array
ary = np.array([[2,4,6,8,10],[1,3,5,7,9]],ndmin=2)
print("Two-Dimensional Array is:")
print(ary)
#Array of Zeros
allZeros = np.zeros([3,3],dtype=int)
print("Array with only zero's:")
print(allZeros)
#Array of Ones
allOnes = np.ones([7],dtype=int)
print("Array with only one's:")
print(allOnes)
#Random numbers in ndarray
aryRand = np.ndarray(shape=(2,2))
print("Array with random numbers:")
print(aryRand)
#An Array of User Choice
n = int(input("Enter number of elements: "))
ary = np.array([],dtype=int)
print("Enter %d elements:"%(n))
for i in range(n):
ary = np.append(ary,int(input()))
print("Array is:")
print(ary)
#Matrix in NumPy
mtrx = np.matrix(([1, 2, 3],[4, 5, 6]))
print("Matrix is:")
print(mtrx)
imtrx = mtrx.getI()
print("Imatrix of given matrix:")
print(imtrx)
#Evenly Spaced ndarray
evenlySpacedAry = np.linspace(20,30,num=4,dtype=int)
print("Evenly Spaced Array is:")
print(evenlySpacedAry)
#Creating basic array
ary = np.array([[2,4,6,8,10],[1,3,5,7,9]],ndmin=2)
print("Two-Dimensional Array is:")
print(ary)
#Array of Zeros
allZeros = np.zeros([3,3],dtype=int)
print("Array with only zero's:")
print(allZeros)
#Array of Ones
allOnes = np.ones([7],dtype=int)
print("Array with only one's:")
print(allOnes)
#Random numbers in ndarray
aryRand = np.ndarray(shape=(2,2))
print("Array with random numbers:")
print(aryRand)
#An Array of User Choice
n = int(input("Enter number of elements: "))
ary = np.array([],dtype=int)
print("Enter %d elements:"%(n))
for i in range(n):
ary = np.append(ary,int(input()))
print("Array is:")
print(ary)
#Matrix in NumPy
mtrx = np.matrix(([1, 2, 3],[4, 5, 6]))
print("Matrix is:")
print(mtrx)
imtrx = mtrx.getI()
print("Imatrix of given matrix:")
print(imtrx)
#Evenly Spaced ndarray
evenlySpacedAry = np.linspace(20,30,num=4,dtype=int)
print("Evenly Spaced Array is:")
print(evenlySpacedAry)
Output
Two-Dimensional Array is:
[[ 2 4 6 8 10]
[ 1 3 5 7 9]]
Array with only zero's:
[[0 0 0]
[0 0 0]
[0 0 0]]
Array with only one's:
[1 1 1 1 1 1 1]
Array with random numbers:
[[6.01334515e-154 2.18176648e+243]
[7.68290838e+170 4.88257229e+199]]
Enter number of elements: 3
Enter 3 elements:
7
5
9
Array is:
[7 5 9]
Matrix is:
[[1 2 3]
[4 5 6]]
Imatrix of given matrix:
[[-0.94444444 0.44444444]
[-0.11111111 0.11111111]
[ 0.72222222 -0.22222222]]
Evenly Spaced Array is:
[20 23 26 30]
[[ 2 4 6 8 10]
[ 1 3 5 7 9]]
Array with only zero's:
[[0 0 0]
[0 0 0]
[0 0 0]]
Array with only one's:
[1 1 1 1 1 1 1]
Array with random numbers:
[[6.01334515e-154 2.18176648e+243]
[7.68290838e+170 4.88257229e+199]]
Enter number of elements: 3
Enter 3 elements:
7
5
9
Array is:
[7 5 9]
Matrix is:
[[1 2 3]
[4 5 6]]
Imatrix of given matrix:
[[-0.94444444 0.44444444]
[-0.11111111 0.11111111]
[ 0.72222222 -0.22222222]]
Evenly Spaced Array is:
[20 23 26 30]