<jupyter_start><jupyter_text>In this lab, you will get familiar with some machine learning algorithms using Python. You will implement different techniques for predictive analytics. At the end of this lab, you will know:1.   How to split data into training and testing sets2.   How to predict a target value using the decision tree technique3.   How to predict a target value using K nearest neighbors<jupyter_code>!wget -q https://raw.githubusercontent.com/tofighi/ITM760/main/dataset/fruit_data_with_colors.txt -O fruit_data_with_colors.txt
import pandas as pd
fruits = pd.read_table('fruit_data_with_colors.txt')
fruits.head()
from pandas.plotting import scatter_matrix
from matplotlib import cm
feature_names = ['mass', 'width', 'height', 'color_score']
X = fruits[feature_names]
y = fruits['fruit_label']
#Create Training and Test Sets and Apply Scaling
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
#Building Models (Decision Tree)
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier().fit(X_train, y_train)
print('Accuracy of Decision Tree classifier on training set: {:.2f}'
     .format(clf.score(X_train, y_train)))
print('Accuracy of Decision Tree classifier on test set: {:.2f}'
     .format(clf.score(X_test, y_test)))
#K-Nearest Neighbors
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
print('Accuracy of K-NN classifier on training set: {:.2f}'
     .format(knn.score(X_train, y_train)))
print('Accuracy of K-NN classifier on test set: {:.2f}'
     .format(knn.score(X_test, y_test)))<jupyter_output>Accuracy of K-NN classifier on training set: 0.95
Accuracy of K-NN classifier on test set: 1.00
{'fraction_non_alphanumeric': 0.09375, 'fraction_numerical': 0.008854166666666666, 'mean_word_length': 5.8362989323843415, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '542962', 'license': 'CC0-1.0', 'n_tokens_mistral': 651, 'n_tokens_neox': 613, 'n_words': 196, 'path': 'Lab7.ipynb', 'repo_name': 'tofighi/ITM760', 'size': '1920'}