<jupyter_start><jupyter_text>Today concept1. Classification models1. K-Nearest Neibour classifier  Classificationif the target is discrete values then we can say it is classificationexample finding person has cancer or notemail spam filterstudent has pass or failwe want predict given fruit is apple? or mandrain? or orange ? lemon? **We want predict the which fruit it is from (apple/mandarin/orange/lemon)?** **1. Get the data**<jupyter_code>import pandas as pd
df = pd.read_csv('fruit_data_with_colours.csv')
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>in this dataset fruit_label is our target column **what are the avilable fruits in this data frame?**<jupyter_code>df['fruit_name'].unique()<jupyter_output><empty_output><jupyter_text>**what is the count of each fruit in this dataset?**<jupyter_code>df['fruit_name'].value_counts()<jupyter_output><empty_output><jupyter_text>**2. pre-processing** is there any missing values?<jupyter_code>df.isna().sum()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 59 entries, 0 to 58
Data columns (total 7 columns):
fruit_label      59 non-null int64
fruit_name       59 non-null object
fruit_subtype    59 non-null object
mass             59 non-null int64
width            59 non-null float64
height           59 non-null float64
color_score      59 non-null float64
dtypes: float64(3), int64(2), object(2)
memory usage: 2.8+ KB<jupyter_text>**we have to seperate the features and target**<jupyter_code>X = df[['width','height']]
y = df['fruit_label']<jupyter_output><empty_output><jupyter_text>**you have to visualize the fruits using width and height?**<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=0.7)<jupyter_output><empty_output><jupyter_text>**3. train the model**<jupyter_code>X_train.head(3)
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train,y_train)
print('training score',model.score(X_train,y_train)* 100)
df.sample(5)
# which type of fruit it is if width has 7.4 and height has 7
model.predict([   [ 7.4,7.0  ]     ])
model.predict([ [ 6.1,8.1 ] ])
model.predict([ [ 7.1,7.6  ] ])
print('testing score',model.score(X_test,y_test)*100)
from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>```accuracy_score(y_actual,y_predict)```<jupyter_code>y_predict = model.predict(X_test)
accuracy_score(y_test,y_predict)*100
accuracy_score(['cat','ant'] ,['cat','cat'])<jupyter_output><empty_output><jupyter_text>**confusion_matrix**<jupyter_code>from sklearn.metrics import confusion_matrix<jupyter_output><empty_output><jupyter_text>```confusion_matrix(y_true,y_predict)```<jupyter_code>y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
confusion_matrix(y_true,y_pred)<jupyter_output><empty_output><jupyter_text>```| |-----> predicted vTrue      ant      bird     catant        2        0       0bird       0        0       1 cat        1        0       2``` you given one person medial details to the modelactual person has No cancer  but model predicted it as person has cancer  prediction of breast cancer for person **1. Get the data**<jupyter_code>from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
data.keys()
print(data['DESCR'])
import pandas as pd
df = pd.DataFrame(data['data'])
df.columns = data['feature_names']
df['target'] = data['target']
df.head()<jupyter_output><empty_output><jupyter_text>**2. pre-processing**<jupyter_code>df.shape
df.isna().sum()
X = df.drop('target',axis=1)
y = df['target']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=0.7)
X_train.shape,X_test.shape
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train,y_train)
print('training score',model.score(X_train,y_train)* 100)
print('testing score',model.score(X_test,y_test)*100)<jupyter_output>testing score 92.39766081871345<jupyter_text>**to find best k values**<jupyter_code>K_values = [3,5,7,9,21,25,51]
scores = {}
for k in K_values:
    model = KNeighborsClassifier(n_neighbors=k)
    model.fit(X_train,y_train)
    scores[k] = model.score(X_train,y_train) 
scores
import matplotlib.pyplot as plt
plt.figure()

plt.scatter(scores.keys(),scores.values(),c='red')

plt.show()<jupyter_output><empty_output><jupyter_text>Task prediction flower it is from iris dataset<jupyter_code>from sklearn.datasets import load_iris
data = load_iris()
data.keys()
print(data['DESCR'])
import pandas as pd
df = pd.DataFrame(data['data'])
df.columns = data['feature_names']
df['target'] = data['target']
df.head()
df.shape
df['target'].unique()
df['target'].value_counts()<jupyter_output><empty_output><jupyter_text>**2. pre-processing**<jupyter_code>df.isna().sum()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
sepal length (cm)    150 non-null float64
sepal width (cm)     150 non-null float64
petal length (cm)    150 non-null float64
petal width (cm)     150 non-null float64
target               150 non-null int32
dtypes: float64(4), int32(1)
memory usage: 5.3 KB<jupyter_text>**we may apply visualisation to understand data**<jupyter_code>import matplotlib.pyplot as plt
setosa = df[df['target']==0]
versicolour = df[df['target']==1]
virginica = df[df['target'] == 2]
plt.figure()
plt.scatter(setosa['petal length (cm)'],setosa['petal width (cm)'],c='red',label='setosa')
plt.scatter(versicolour['petal length (cm)'],versicolour['petal width (cm)'],c='green',label='versicolor')
plt.scatter(virginica['petal length (cm)'],virginica['petal width (cm)'],c='blue',label='virginica')
plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)')
plt.legend()
plt.show()
X = df[['petal length (cm)','petal width (cm)']]
y = df['target']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=0.7)
from sklearn.neighbors import KNeighborsClassifier<jupyter_output><empty_output><jupyter_text>**to find best k value**<jupyter_code>K_values = [3,5,7,9,11,15,17,19,27]
scores = {}
for k in K_values:
    model = KNeighborsClassifier(n_neighbors=k)
    model.fit(X_train,y_train)
    scores[k] = model.score(X_train,y_train)
plt.figure()

plt.scatter(scores.keys(),scores.values(),c='red')

plt.show()
scores[19]
model = KNeighborsClassifier(n_neighbors=19)
model.fit(X_train,y_train)
print('training score',model.score(X_train,y_train)*100)
print('testing score',model.score(X_test,y_test)*100)<jupyter_output>training score 98.09523809523809
testing score 91.11111111111111
{'fraction_non_alphanumeric': 0.14142604596346495, 'fraction_numerical': 0.03285209192692987, 'mean_word_length': 6.610986547085202, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '556668', 'license': 'CC0-1.0', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2453, 'n_words': 518, 'path': 'Day-4/04_Day4_29Oct2020.ipynb', 'repo_name': 'AP-State-Skill-Development-Corporation/Machine-Learning-Using-Python-MB7', 'size': '6788'}