<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

url = 'https://storage.googleapis.com/kagglesdsdata/datasets/9590/13660/fruit_data_with_colors.txt?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com%2F20211022%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20211022T044756Z&X-Goog-Expires=259199&X-Goog-SignedHeaders=host&X-Goog-Signature=9ef56913a8cc76e7fd25e334f8115415de0d22f85121c548d7cd06f72a1144cab25432bff426979704fd4292c85bd7ae3a9acea6374a35d5d0abdb526f60f4da2650d1fa317bf27ba484de0f0732d303bc36f715fe65afcda75352b60148ac6a1c7f67b274a8a4de13f4be8b792e7eef15f52d0fdb0442fd4c3689294de647351b1097682004ee76e95541ab40daf7cb325c2a4a795f8f32d4cf84bc872ddbe4ba719f5fd4f0e781308fa30b616d7a9f5da3855dd8a8fc13e08112ee620d36155eeb1731db50da118426195ac0ec21cf607778186b59e94571ffb413ae4b00360063f86c74abf68b47e02cadedca4b5d5f3ac67260057a9fd1e51d32c66e1a2b'
fruits = pd.read_table(url)
fruits.head
predct = dict(zip(fruits.fruit_label.unique(), fruits.fruit_name.unique()))
predct
#checking how many unique fruit names are present in dataset
fruits['fruit_name'].value_counts()
#restore all unique data on four different dataframes
apple_data = fruits[fruits['fruit_name']=='apple']
orange_data = fruits[fruits['fruit_name']=='orange']
lemon_data = fruits[fruits['fruit_name']=='lemon']
mandarin_data = fruits[fruits['fruit_name']=='mandarin']
apple_data.head()
orange_data.head()
lemon_data.head()
mandarin_data.head()
#the data above shown that every fruit has fruit_label {apple is 1, mandarin is 2, orange is 3, and lemon is 4}
plt.scatter(fruits['width'], fruits['height'])
plt.scatter(fruits['mass'], fruits['color_score'])
plt.plot(fruits['height'], label='Height')
plt.plot(fruits['width'], label='Width')
plt.legend()
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
X = fruits[['mass','width','height']]
Y = fruits['fruit_label']
X_train, X_test, y_train, y_test = train_test_split(X, Y, random_state=123)
X_train.describe
X_test.describe
knn=KNeighborsClassifier()
#train data
knn.fit(X_train, y_train)
knn.score(X_test, y_test)
#example 1 with prediction of mass, weidth, and height
prediction1=knn.predict([['100', '6.3', '8']])
predct[prediction1[0]]
#example 2 with prediction of mass, weidth, and height
prediction1=knn.predict([['300', '7', '10']])
predct[prediction1[0]]
#example 3 with prediction of mass, weidth, and height
prediction1=knn.predict([['85', '6', '4']])
predct[prediction1[0]]
plt.plot(fruits['mass'], label='Mass')
plt.plot(fruits['color_score'], label='Color Score')
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12486187845303867, 'fraction_numerical': 0.14880294659300183, 'mean_word_length': 10.964757709251101, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '86623', 'license': 'Apache-2.0', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1116, 'n_words': 156, 'path': 'Week3_ML.ipynb', 'repo_name': 'yahyanh21/Machine-Learning-Homework', 'size': '2715'}