NumPy Data Set Exercises

Source: Internet
Author: User

(1) Installing the Scipy,numpy,sklearn package

(2) The IRIS data set is read from the data set in the Sklearn package

(3) View data type

# 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 ())

Operation Result:

(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)# Iris three categories #Data_target =data.target_names  Print(data_target) iris_data=data.targetprint(iris_data)  # data type #type (iris_data)

Operation Result:

(5) Extract the data of the calyx length (cm) of all flowers

# iris calyx Length Data #  for  in data['data'))print(sepal_length)

Operation Result:

(6) Remove all flower petal length (cm) + petal width (cm) data

# the length of all flower petals #  for  in data['data']# width of all flower petals # for in data['data']# The length and width of all flower petals Data_petal_l_ W=np.array ([Data_petal_l,data_petal_w])

Operation Result:

(7) Take out four characteristics and categories of a flower

(8) Divide all flowers into three groups, 50 per group

(9) Generate new Ganso, each group includes features and categories

#(7) four characters and categories of a flowerData_flower= (data['Data'][0],data['Target_names'][0]) data_flower#(8) Define three lists to hold the categories of different types of flowers #Data_setosa=[]#storing flowers of the class 0Data_versicolor=[]#storing flowers of the Class 1Data_virginica=[]#storing flowers of the class 2Len (data['Data']) forIinchRange (0,150):    ifdata['Target'][i]==0:#category is Setosadatas=data['Data'][i].tolist () datas.append ('Setosa')        Print(Data_setosa.append (datas))elifdata['Target'][i]==1:#category is Versicolordatas=data['Data'][i].tolist () datas.append ('versicolor') data_versicolor.append (datas)Else: Datas=data['Data'][i].tolist ()#category is VirginicaDatas.append ('virginica') data_virginica.append (datas)#(9) forming a new array to hold three categories of flowers #New_data=(Np.array ([Data_setosa,data_versicolor,data_virginica]))Print(New_data)

Operation Result:

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.