Week-1
# Week-1
# Naive Bayes (NB)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
df = pd.read_csv("sample_data/pima_indian.csv")
feature_col_names = ['num_preg', 'glucose_conc', 'diastolic_bp', 'thickness', 'insulin', 'bmi',
'diab_pred', 'age']
predicted_class_names = ['diabetes']
X = df[feature_col_names].values # these are factors for the prediction
y = df[predicted_class_names].values # this is what we want to predict
#splitting the dataset into train and test data
xtrain,xtest,ytrain,ytest=train_test_split(X,y,test_size=0.33)
print ('\n the total number of Training Data :',[Link])
print ('\n the total number of Test Data :',[Link])
# Training Naive Bayes (NB) classifier on training data.
clf = GaussianNB().fit(xtrain,[Link]())
predicted = [Link](xtest)
predictTestData= [Link]([[6,148,72,35,0,33.6,0.627,50]])
#printing Confusion matrix, accuracy, Precision and Recall
print('\n Confusion matrix')
print(metrics.confusion_matrix(ytest,predicted))
print('\n Accuracy of the classifier is',metrics.accuracy_score(ytest,predicted))
print('\n The value of Precision', metrics.precision_score(ytest,predicted))
print('\n The value of Recall', metrics.recall_score(ytest,predicted))
print("Predicted Value for individual Test Data:", predictTestData)
week-2
#Week-2
import pandas
from sklearn import tree
import pydotplus
from [Link] import DecisionTreeClassifier
import [Link] as plt
import [Link] as pltimg
df = pandas.read_csv("/content/sample_data/[Link]")
d = {'Sunny': 0, 'Overcast': 1, 'Rain': 2}
df['Outlook'] = df['Outlook'].map(d)
d = {'Hot': 0, 'Mild': 1,'Cool': 2}
df['Temperature'] = df['Temperature'].map(d)
d = {'High': 0, 'Normal': 1}
df['Humidity'] = df['Humidity'].map(d)
d = {'Weak': 0, 'Strong': 1}
df['Wind'] = df['Wind'].map(d)
d = {'No': 0, 'Yes': 1}
df['playtennis'] = df['playtennis'].map(d)
features = ['Outlook', 'Temperature', 'Humidity', 'Wind']
X = df[features]
y = df['playtennis']
dtree = DecisionTreeClassifier(criterion="entropy")
dtree = [Link](X, y)
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('[Link]')
img=[Link]('[Link]')
imgplot = [Link](img)
[Link]()
Week-3(a)
# Week-3a
import numpy as np
import pandas as pd
from [Link] import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
# Define the column names for the dataset
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']
# Read dataset to pandas dataframe
dataset = pd.read_csv("/content/sample_data/[Link]", names=names, header=0)
# Split the dataset into features (X) and labels (y)
X = [Link][:, :-1]
y = [Link][:, -1]
# Display the first few rows of the features
print([Link]())
# Split the data into training and testing sets
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.10)
# Train the k-NN classifier
classifier = KNeighborsClassifier(n_neighbors=5).fit(Xtrain, ytrain)
# Predict the labels of the test set
ypred = [Link](Xtest)
# Display the results
i=0
print ("\n-------------------------------------------------------------------------")
print ('%-25s %-25s %-25s' % ('Original Label', 'Predicted Label', 'Correct/Wrong'))
print ("-------------------------------------------------------------------------")
for label in ytest:
print ('%-25s %-25s' % (label, ypred[i]), end="")
if (label == ypred[i]):
print (' %-25s' % ('Correct'))
else:
print (' %-25s' % ('Wrong'))
i=i+1
print ("-------------------------------------------------------------------------")
print("\nConfusion Matrix:\n", metrics.confusion_matrix(ytest, ypred))
print ("-------------------------------------------------------------------------")
print("\nClassification Report:\n", metrics.classification_report(ytest, ypred))
print ("-------------------------------------------------------------------------")
print('Accuracy of the classifier is %0.2f' % metrics.accuracy_score(ytest, ypred))
Week-3(b)
#Week-3b
import numpy as nm
import [Link] as mtp
import pandas as pd
data_set= pd.read_csv('/content/sample_data/Salary_Data.csv')
x= data_set.iloc[:, :-1].values
y= data_set.iloc[:, 1].values
# Splitting the dataset into training and test set.
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 1/3, random_state=0)
#Fitting the Simple Linear Regression model to the training dataset
from sklearn.linear_model import LinearRegression
regressor= LinearRegression()
[Link](x_train, y_train)
#Prediction of Test and Training set result
y_pred= [Link](x_test)
x_pred= [Link](x_train)
#visualizing the Training set results:
[Link](x_train, y_train, color="green")
[Link](x_train, x_pred, color="red")
[Link]("Salary vs Experience (Training Dataset)")
[Link]("Years of Experience")
[Link]("Salary(In Rupees)")
[Link]()
#Step: 5. visualizing the Test set results:
#visualizing the Test set results
[Link](x_test, y_test, color="blue")
[Link](x_train, x_pred, color="red")
[Link]("Salary vs Experience (Test Dataset)")
[Link]("Years of Experience")
[Link]("Salary(In Rupees)")
[Link]()
Week-4(a)
#Week-4a
import [Link] as plt
from sklearn import svm, datasets
from [Link] import DecisionBoundaryDisplay
# import some data to play with
iris = datasets.load_iris()
# Take the first two features. We could avoid this by using a two-dim dataset
X = [Link][:, :2]
y = [Link]
# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0 # SVM regularization parameter
models = (
[Link](kernel="linear", C=C),
[Link](C=C, max_iter=10000),
[Link](kernel="rbf", gamma=0.7, C=C),
[Link](kernel="poly", degree=3, gamma="auto", C=C),
models = ([Link](X, y) for clf in models)
# title for the plots
titles = (
"SVC with linear kernel",
"LinearSVC (linear kernel)",
"SVC with RBF kernel",
"SVC with polynomial (degree 3) kernel",
# Set-up 2x2 grid for plotting.
fig, sub = [Link](2, 2)
plt.subplots_adjust(wspace=0.4, hspace=0.4)
X0, X1 = X[:, 0], X[:, 1]
for clf, title, ax in zip(models, titles, [Link]()):
disp = DecisionBoundaryDisplay.from_estimator(
clf,
X,
response_method="predict",
cmap=[Link],
alpha=0.8,
ax=ax,
xlabel=iris.feature_names[0],
ylabel=iris.feature_names[1],
[Link](X0, X1, c=y, cmap=[Link], s=20, edgecolors="k")
ax.set_xticks(())
ax.set_yticks(())
ax.set_title(title)
[Link]()
week-4(b)
#Week-4b
import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
# Importing the classification report and confusion matrix
from [Link] import classification_report, confusion_matrix
%matplotlib inline
# Checking the dataset
[Link]()
# Creating a pairplot to visualize the similarities and especially difference between the species
[Link](data=iris, hue='species', palette='Set2')
# Separating the independent variables from dependent variables
x=[Link][:,:-1]
y=[Link][:,4]
x_train,x_test, y_train, y_test=train_test_split(x,y,test_size=0.30)
from [Link] import SVC
model=SVC()
[Link](x_train, y_train)
pred=[Link](x_test)
print(confusion_matrix(y_test,pred))
print(classification_report(y_test, pred))
Week-5
#Week-5
#EM algorithm and K-Means Algorithm
import [Link] as plt
from sklearn import datasets
from [Link] import KMeans
import [Link] as sm
import pandas as pd
import numpy as np
iris = datasets.load_iris()
X = [Link]([Link])
[Link] = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y = [Link]([Link])
[Link] = ['Targets']
model = KMeans(n_clusters=2)
[Link](X)
[Link](figsize=(14,7))
colormap = [Link](['red', 'lime', 'black'])
# Plot the Original Classifications
[Link](1, 2, 1)
[Link](X.Petal_Length, X.Petal_Width, c=colormap[[Link]], s=40)
[Link]('Real Classification')
[Link]('Petal Length')
[Link]('Petal Width')
# Plot the Models Classifications
[Link](1, 2, 2)
[Link](X.Petal_Length, X.Petal_Width, c=colormap[model.labels_], s=40)
[Link]('K Mean Classification')
[Link]('Petal Length')
[Link]('Petal Width')
print('The accuracy score of K-Mean: ',sm.accuracy_score(y, model.labels_))
print('The Confusion matrixof K-Mean: ',sm.confusion_matrix(y, model.labels_))
from sklearn import preprocessing
scaler = [Link]()
[Link](X)
xsa = [Link](X)
xs = [Link](xsa, columns = [Link])
#[Link](5)
from [Link] import GaussianMixture
gmm = GaussianMixture(n_components=3)g
[Link](xs)
y_gmm = [Link](xs)
#y_cluster_gmm
[Link](2, 2, 3)
[Link](X.Petal_Length, X.Petal_Width, c=colormap[y_gmm], s=40)
[Link]('EMM Classification')
[Link]('Petal Length')
[Link]('Petal Width')
print('The accuracy score of EM: ',sm.accuracy_score(y, y_gmm))
print('The Confusion matrix of EM: ',sm.confusion_matrix(y, y_gmm))