1. Install the Scipy,numpy,sklearn package
2. The iris data set is read from the data set in the Sklearn package
3. Look at the data type, what is included
# Load NumPy Package Import NumPy # Load Sklearn Package from Import # Read the iris DataSet datadata=load_iris ()# View data type print (Type (data))# View data content print(Data.keys ())
The results of the operation are as follows:
4. Remove the iris feature and Iris category data to see its shape and data type
# Four features of Iris data_feature= data.feature_namesiris_data=data.dataprint(data_ Feature)print(iris_data)# three categories of iris Data_target =data.target_names Print(data_target) iris_data=data.targetprint(iris_data) # datatype type type (iris_data)
The results of the operation are as follows:
5. Extract the data of the calyx length (cm) of all flowers
# iris calyx Length Data for in data['data'))print(sepal_length)
The results of the operation are as follows:
6. Remove all flower petal length (cm) + petal width (cm) data
# Iris Petal Length Data for in data['data'print(petal_length)
# Iris Petal Width Data for in data['data'print(petal_width)
The results of the operation are as follows:
7. Take out the four characteristics of a flower and its category
# take out four characteristics of a flower Print (data.data[0]) # take out the category of a flower Print (Data.target_names[0])
The results of the operation are as follows:
8. Divide the characteristics and categories of all flowers into three groups, 50 per group
9. Generate a new array with four features + categories per element
#define three lists to hold the categories of different types of flowersSetosa_data =[]versicolor_data=[]virginica_data= []# forIinchRange (0,150): #The iris flower data of the Setosa class ifData.target[i] = =0:data1=data.data[i].tolist () data1.append ('Setosa') setosa_data.append (data1)#The iris data of the Versicolor class elifData.target[i] = = 1: Data1=data.data[i].tolist () data1.append ('versicolor') versicolor_data.append (data1)#The rest of the iris data for the Virginica class Else: Data1=data.data[i].tolist () data1.append ('virginica') virginica_data.append (data1)#generate a new array with four features + categories per elementNewdata=(Setosa_data, Versicolor_data,virginica_data)Print(NewData)
The results of the operation are as follows:
NumPy Data Set Exercises