Python Iris Classification Implementation

Source: Internet
Author: User

#coding: Utf-8

From sklearn.datasets import Load_iris
From sklearn.model_selection import train_test_split
From sklearn.neighbors import kneighborsclassifier
import NumPy as NP
import Matplotlib.pyplot as Plt

Iris_dataset = Load_iris () # Get Data
# print ("Keys of iris_dataset:\n{}". Format (Iris_dataset.keys ()))
# Print (iris_dataset["DESCR"][:193]+ "\ n ...")
# print ("Target names:{}". Format (iris_dataset["Target_names"))
# Print ("Feature names:{}". Format (iris_dataset["Feature_names"] )
# Print (iris_dataset["data"][:5])
# Print (iris_dataset["Data"], iris_dataset["target"])
# split the data into training data and test data
X_train, X_test, y_train, y_test = Train_test_split (iris_dataset["Data"], iris_dataset["target"], random_state=0)
# Print (X_train, x_test, Y_train, y_test)

KNN = Kneighborsclassifier (n_neighbors=1) # get KNN object
Knn.fit (X_train, Y_train) # Training Model

# Evaluation Model
Y_pre = knn.predict (x_test)
score = Knn.score (X_test, y_test) # Call the scoring function
print ("Test set predictions:\n{}". Format (y_test))
print ("Test set score:{:.2f}". Format (score))
if score > 0.9:
x_new = Np.array ([[5, 2.9, 1, 0.3]])
print ("x_new.shape:{}". Format (x_new.shape))
prediction = Knn.predict (x_new) # forecast
print ("prediction:{}". Format (prediction))
print ("predicted target name:{}". Format (iris_dataset["Target_names"][prediction] )

# Visual Display
plt.title ("KNN classification")
Plt.plot (X_train, Y_train, "B.") # Training Data Dot
Plt.plot (x_test, Y_test, "Y.") # test Data dot
Plt.plot (x_new, prediction, "RO") # Predictive Data dot
plt.show ()
Else:
print ("Used train or test data is not available!")



Python Iris Classification Implementation

Related Article

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.