Supervised Learning - Classification Week 8 Challenge
1.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import plot_confusion_matrix
#Dataframe df
df= pd.read_csv('fault.csv')
#To print data
print(df)
#Size of data
print('n',df.shape)
#To print column name of data
print('n',df.columns)
#To check for datatype and presence of null value of each column
print('n',df.info())
print('n',df.isnull().values.any())
#Last seven columns are type of error classification
Label_columns =df.columns.values[-7:]
#assigning the error classification values to variable targets
Targets=(df.iloc[:,-7:]==-1).idxmax(1)
print('n',lablel_columns)
print('n',tragets)
#Dropping the 7 error classification columns and retaining only targets
dataset =df.drop(label_columns,axis=1)
#creating a new column 'target' with all classification values
dataset['target']=targets
print('n',dataset)
#printing count of each type of error
print('n',dataset.target.value_counts())
fig,ax = plt.subplots(1,2,figsize=(15,6))
#Visualising the distribution of each error using histogram and pie chart
sns.countplot(x='target',data= dataset , ax=ax[0])
dataset['target'].value_counts().plot.pie(autopct='%.1f' ,ax=ax[1])
#Visualising the correlation among each dataset feature
plt.figure(figsize=(10,11))
sns.heatmap(dataset.corr(),annot=false)
#dropping feature with high correlation to others
dataset=dataset.drop('typeofsteel_A400',axis=1)
dataset=dataset.drop('X_Minimum,axis=1)
dataset=dataset.drop('Y_Minimum,axis=1)
#Assigngning feature column value to x
x = dataset.iloc[:,0:24]
#Assigngning feature column value to y
y = dataset.iloc[:,24]
#splitting the dataset to training and testing sets
x_train,x_test,y_train,y_test = train_test_split(x,y,test,labels=none,sample_weight=none,normalize=none)
plt.show()
2.
Some Advantages of KNN
- Quick calculation time
- Simple algorithm – to interpret
- Versatile – useful for regression and classification
- High accuracy – you do not need to compare with better-supervised learning models
- No assumptions about data – no need to make additional assumptions, tune several parameters, or build a model. This makes it crucial in nonlinear data cases.
Some Disadvantages of KNN
- Accuracy depends on the quality of the data
- With large data, the prediction stage might be slow
- Sensitive to the scale of the data and irrelevant features
- Require high memory – need to store all of the training data
- Given that it stores all of the training, it can be computationally expensive