NumPy Data Set Exercises

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.