<jupyter_start><jupyter_text>Hands-on : ClassificationIn this hands on activity you will learn how to develop an end to end ML based classification.project. We will work with the iris flower dataset. The dataset contains features of a particular flower and the task is to classify it into one of the flower species namely Iris setosa, Iris virginica and Iris versicolor The dataset filename is `iris.data.csv`Attribute Information:1. sepal length in cm 2. sepal width in cm 3. petal length in cm 4. petal width in cm 5. class: -- Iris Setosa -- Iris Versicolour -- Iris Virginica 1. Load the necessary libraries<jupyter_code># Fill in with your code
import pandas as pd<jupyter_output><empty_output><jupyter_text>2. Load the dataset<jupyter_code># Fill in with your code

df = pd.read_csv("../data/iris.data.csv")
print(df.head())
df = pd.read_csv('../data/iris.data.csv', header=None)
df.head()
df = pd.read_csv('../data/iris.data.csv', header=None, 
                 names = ["sepal-length", "sepal-width", "petal-length", "petal-width","class"])
df.head()<jupyter_output><empty_output><jupyter_text>3. Summarize the dataset<jupyter_code># Fill in with your code
df.shape
df.ndim<jupyter_output><empty_output><jupyter_text>The dataset contains 150 instances and 5 attributes<jupyter_code># Fill in with your code
df.head()<jupyter_output><empty_output><jupyter_text>The first 20 instances in the data<jupyter_code># Fill in with your code
df.describe()
#lihat tipe data
df.dtypes<jupyter_output><empty_output><jupyter_text>Statistical descriptions of the data<jupyter_code># Fill in with your code
df.groupby("class")["class"].count()<jupyter_output><empty_output><jupyter_text>Distribution of the class/label in the data<jupyter_code># Fill in with your code
df.hist()<jupyter_output><empty_output><jupyter_text>4. Create a train test split<jupyter_code># Fill in with your code
from sklearn.model_selection import train_test_split
#X,y = df[(:,:-1)],df[(:,-1:)]
X = df.iloc[:,:-1]
y = df.iloc[:,-1]
y.head()
seed = 7
test_size = 0.2
X_train, X_test, Y_train, Y_test = train_test_split(X,y, test_size=test_size, random_state=seed)
X_train.head()
X_test.head()
Y_train.head()
Y_test.head()<jupyter_output><empty_output><jupyter_text>5. Cross ValidationTry using any classifier you like e.g. Logistic Regression then print the average precision and also the precision, recall, and F1 for each label<jupyter_code># Fill in with your code
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
import sklearn.metrics as metrics
from sklearn.model_selection import cross_val_predict
seed = 7
num_folds = 5
k_fold = KFold(n_splits=num_folds, random_state=seed)
model = LogisticRegression()
predicted = cross_val_predict(model, X_train, Y_train, cv=k_fold)
print("Accuracy : %.3f "%(metrics.accuracy_score(Y_train, predicted)*100))

from sklearn.metrics import classification_report
report = classification_report(Y_train, predicted)
print(report)<jupyter_output>precision    recall  f1-score   support

    Iris-setosa       1.00      1.00      1.00        43
Iris-versicolor       1.00      0.89      0.94        38
 Iris-virginica       0.91      1.00      0.95        39

    avg / total       0.97      0.97      0.97       120<jupyter_text>6. Evaluate performance on the test setNow we have performed the cross validation, we can build the model on all training data and then test it on unseen data. Print the confusion matrix and also the classification report<jupyter_code># Fill in with your code
model.fit(X_train, Y_train)
# save model
from pickle import dump
from pickle import load

file_name = "final_model.sav"
dump(model, open(file_name, "wb"))
# load model
loaded_model = load(open(file_name, "rb"))
# Inference/prediction

predicted = loaded_model.predict(X_test)
metrics.precision_score(Y_test, predicted, average="weighted")
report = classification_report(Y_test, predicted)
print(report)<jupyter_output>precision    recall  f1-score   support

    Iris-setosa       1.00      1.00      1.00         7
Iris-versicolor       0.88      0.58      0.70        12
 Iris-virginica       0.67      0.91      0.77        11

    avg / total       0.83      0.80      0.80        30
{'fraction_non_alphanumeric': 0.09626168224299066, 'fraction_numerical': 0.028271028037383177, 'mean_word_length': 4.252760736196319, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '656230', 'license': 'MIT', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1411, 'n_words': 422, 'path': 'exercise classification/Hands On Classification - Exercise @indra.ipynb', 'repo_name': 'indrawaspada/dmbd2018_ui', 'size': '4280'}