To write a piece of python code for Sub setting and indexing of Numpy array.
Numpy array.
Sub set of an array
Boolean indexing of an array
Fancy indexing of an array
Load the sample data.
Create an array.
Do indexing and slicing of an array using appropriate procedures.
Print the results.
#import libraries
import numpy as np
import pandas as pd
#load the sample data from csv file
data = pd.read_csv(‘/home/soft50/soft50/Sathish/practice/iris.csv’)
#Make it as a data frame
df = pd.DataFrame(data)
#feature selection
X = np.array(df.iloc[:,0:4])
print(“Sample data\n”)
for i in range(20):
print(X[i])
print(“\n”)
#Subsetting of an array
print(“Subsetting an array\n”)
print(X[2])
print(X[2,2])
print(“\n”)
#Slicing of an array
print(“Slicing an array\n”)
print(X[0:2],”\n”)
print(X[0:2,3])
print(“\n”)
#Boolean indexing of an array
print(“Boolean indexing\n”)
print(X[X<3])
print(“\n”)
#Fancy indexing of an array
print(“Fancy Indexing an array\n”)
print(X[[1, 0, 1, 0]][:,[0,1,2,0]])