<jupyter_start><jupyter_text>Assignment 1:• Consider a case of 50 student, out of which 35Passed (P) and 15 Failed(F) in the Certification Exam.Write a program to compute Entropy forthe given information.• If we have the information that some of the candidates have undergonetraining and some didn’t. Based on it split the dataset, let say 20 peopleundergone the training and 30 didn’t. Out of this 20 trained candidate18P and 2F, while in case of 30 untrained 17P and 13F. Write aprogram to calculate the information gain after split of data.Project 1:From Data given it is required to develop the model to predict the profit of the company. Dataset filename: Companies-profit.csvTask:1) Load the dataset2) Analyse the data visually and give justification for selecting particular algorithm for developing themodel.3) Split it into test and train.4) Develop a model for predicting the profit of the company5) Measure all the parameters of the model6) Measure the performance of the modelProject 2:Use wine dataset from sklearnLoad the dataset and split it into test and train.Develop the model to Classify wines into different categories.Train the model using Gaussian and Multinominal classifier, please mentionwhich model is better and why.• Use the trained model to perform some predictions on test data.Project 3:• Datasets for Disease operation is provided. Dataset file name: Disease.csv1. Load the dataset and split it into test and train.2. Analyse the Data Visually.3. Find limitation with data if any in developing the model4. Develop the model to identify whether operation successful or not usingDecision Tree algorithm5. Measure the performance of the model6. Try to improvise the model by suitable technique.7. Measure the performance of the modelProject: 4• Use iris flower dataset fromsklearn library and try toform clusters of flowersusing petal width and lengthfeatures.• Analyse the Data Visuallybefore training• Drop other two features forsimplicity.• Try to Use K MeanClustering• Analyse the Data Visually bycomparing before and afterthe training.Practice: Exercise 11. What is K-Nearest Neighbour(KNN) Algorithm for Machine Learning2. How does K-NN work?3. Implementation of the KNN algorithm- Take dataset: diabetes.csv• Data Pre-processing step-if any• Fitting the K-NN algorithm to the Training set• Predicting the test result• Test accuracy of the result<jupyter_code>Assignment 1:


• Consider a case of 50 student, out of which 35Passed (P) and 15 Failed
(F) in the Certification Exam.
Write a program to compute Entropy for
the given information.<jupyter_output><empty_output><jupyter_text>import numpy as np(- (35/50) * np.log((35/50))) - ((15/50) * np.log((15/50)))<jupyter_code>Project 1: From Data given it is required to develop the model to predict the profit of the company. Dataset file name: Companies-profit.csv Task:

1) Load the dataset

2) Analyse the data visually and give justification for selecting particular algorithm for developing the model.

3) Split it into test and train.

4) Develop a model for predicting the profit of the company

5) Measure all the parameters of the model

6) Measure the performance of the model<jupyter_output><empty_output><jupyter_text>import pandas as pddata=pd.read_csv('Companies-profit.csv')data.head() import seaborn as snsimport matplotlib.pyplot as plt%matplotlib inline data.shape data.dtypes missing= data.isnull().sum().sort_values(ascending=False)percent= (data.isnull().sum()/data.isnull().count())total= pd.concat([missing, percent],axis=1, keys=["Total", "Percent"])print(total) numeric= data.select_dtypes(include=[np.number])numeric categorical= data.select_dtypes(include=[np.object])categorical plt.figure(figsize=(12,5))plt.hist(data["Profit"],color="red")plt.title("Profit", size=20)plt.ylabel("Frequency", size=20)plt.xlabel("Profit in Rupees", size=20)plt.tick_params(labelsize=17) plt.tick_params(labelsize=20) ax.xaxis.set_tick_params(labelsize=20) ax.yaxis.set_tick_params(labelsize=20)plt.show() target= np.log(data["Profit"])plt.figure(figsize=(12,5))plt.hist(target, color="blue")plt.xlabel("Profit", size=20)plt.ylabel("Frequency", size=20)plt.title("Log Normal Histogram", size=20)plt.tick_params(labelsize=15)plt.show() X = data.iloc[:, :-1].valuesy = data.iloc[:, 4].values y X.shape correlation= numeric.corr()correlation correlation["Profit"].sort_values(ascending=False)*100 plt.subplots(figsize=(12,10))plt.title("Correlation between numeric variables")sns.heatmap(correlation, square=True, vmin=0, vmax=1, center=0, annot=True, linewidths=0.5)plt.show() columns= ["R&D Spend", "Administration", "Marketing Spend", "Profit"]sns.pairplot(data[columns],size=2, kind="scatter", diag_kind="kde")plt.show() sns.regplot(x="R&D Spend", y="Profit", data=data)plt.title("Correlation between Profit and R&D Spend")plt.xlabel("R&D Spend")plt.ylabel("Profit")plt.show() fig, axs = plt.subplots(ncols=3, figsize=(14,10))sns.regplot(x='R&D Spend', y='Profit', scatter= True, data=data, ax=axs[0])sns.regplot(x='Administration', y='Profit', scatter=True, data=data, ax=axs[1])sns.regplot(x='Marketing Spend',y='Profit', scatter=True, data=data, ax=axs[2]) from sklearn.preprocessing import LabelEncoder, OneHotEncoderlabelencoder= LabelEncoder()X[:,3]= labelencoder.fit_transform(X[:, 3]) from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test= train_test_split(X,y,test_size=0.3, random_state=5) from sklearn.linear_model import LinearRegressionlm= LinearRegression()lm.fit(X_train,y_train) pred= lm.predict(X_test) print(lm.intercept_)print(lm.coef_) list(zip(data, lm.coef_)) print("R square is {}:".format(lm.score(X_test,y_test))) from sklearn import metricsfrom sklearn.metrics import mean_squared_errorfrom sklearn.model_selection import cross_val_score print("MAE:", metrics.mean_absolute_error(pred, y_test)) print("RMSE:", np.sqrt(metrics.mean_squared_error(pred, y_test)))<jupyter_code>Project 2:

Use wine dataset from sklearn Load the dataset and split it into test and train.

Develop the model to Classify wines into different categories.

Train the model using Gaussian and Multinominal classifier, please mention which model is better and why.

• Use the trained model to perform some predictions on test data.<jupyter_output><empty_output><jupyter_text>from sklearn import datasetsfrom sklearn import metricsfrom sklearn.model_selection import train_test_splitimport matplotlib.pyplot as pltplt.style.use("ggplot")from sklearn import naive_bayesfrom sklearn.datasets import load_winerdata = load_wine() print(rdata['DESCR']) for key,value in rdata.items():    print(key,'\n',value,'\n') features = pd.DataFrame(data=rdata['data'],columns=rdata['feature_names'])data = featuresdata['target']=rdata['target']data['class']=data['target'].map(lambda ind: rdata['target_names'][ind])data.head() sns.distplot(data['alcohol'],kde=0) for i in data.target.unique():    sns.distplot(data['alcohol'][data.target==i],                 kde=1,label='{}'.format(i))plt.legend() import matplotlib.gridspec as gridspecfor feature in rdata['feature_names']:    print(feature)    sns.boxplot(data=data,x=data.target,y=data[feature])    gs1 = gridspec.GridSpec(3,1)    ax1 = plt.subplot(gs1[:-1])    ax2 = plt.subplot(gs1[-1])    gs1.update(right=0.60)    sns.boxplot(x=feature,y='class',data=data,ax=ax2)    sns.kdeplot(data[feature][data.target==0],ax=ax1,label='0')    sns.kdeplot(data[feature][data.target==1],ax=ax1,label='1')    sns.kdeplot(data[feature][data.target==2],ax=ax1,label='2')    ax2.yaxis.label.set_visible(False)    ax1.xaxis.set_visible(False)    plt.show() from sklearn import datasetsfrom sklearn import metricsfrom sklearn.model_selection import train_test_splitimport matplotlib.pyplot as pltplt.style.use("ggplot")from sklearn import naive_bayes dataset = datasets.load_wine()X = dataset.data; y = dataset.targetX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) model = naive_bayes.GaussianNB()model.fit(X_train, y_train)print(model) expected_y  = y_testpredicted_y = model.predict(X_test) print(metrics.classification_report(expected_y, predicted_y, target_names=dataset.target_names))print(metrics.confusion_matrix(expected_y, predicted_y)) model = naive_bayes.MultinomialNB()model.fit(X_train, y_train)print(); print(model) expected_y  = y_testpredicted_y = model.predict(X_test) print(metrics.classification_report(expected_y, predicted_y,target_names=dataset.target_names))print(metrics.confusion_matrix(expected_y, predicted_y))<jupyter_code>Conclusion: 
    
As showed in this project the Naive Bayes Classifiers, I feel the accuracy of GaussianNB is better than MultinomialNB. 
So i would go for GaussianNB.<jupyter_output><empty_output><jupyter_text>Project 3:• Datasets for Disease operation is provided. Dataset file name: Disease.csvLoad the dataset and split it into test and train.Analyse the Data Visually.Find limitation with data if any in developing the modelDevelop the model to identify whether operation successful or not using Decision Tree algorithmMeasure the performance of the modelTry to improvise the model by suitable technique.Measure the performance of the model<jupyter_code>import pandas as pd
import numpy as np 
import matplotlib as plt
%matplotlib inline
import seaborn as sns
df=pd.read_csv("Disease.csv")
df.head()
df.info()
sns.pairplot(df, hue='Disease', palette='Set1')
from sklearn.model_selection import train_test_split
X=df.drop("Disease",axis=1)
y=df["Disease"]
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3)
from sklearn.tree import DecisionTreeClassifier
dt=DecisionTreeClassifier()
dt.fit(X_train,y_train)
pred=dt.predict(X_test)
pred
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test,pred))
print(classification_report(y_test,pred))<jupyter_output>[[16  5]
 [ 4  0]]
              precision    recall  f1-score   support

      absent       0.80      0.76      0.78        21
     present       0.00      0.00      0.00         4

    accuracy                           0.64        25
   macro avg       0.40      0.38      0.39        25
weighted avg       0.67      0.64      0.66        25<jupyter_text>By using Random Forest, accuracy is bit improved<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rf=RandomForestClassifier()
rf.fit(X_train,y_train)
pred_rf=rf.predict(X_test)
pred_rf
print(confusion_matrix(y_test,pred_rf))
print(classification_report(y_test,pred_rf))<jupyter_output>[[18  3]
 [ 4  0]]
              precision    recall  f1-score   support

      absent       0.82      0.86      0.84        21
     present       0.00      0.00      0.00         4

    accuracy                           0.72        25
   macro avg       0.41      0.43      0.42        25
weighted avg       0.69      0.72      0.70        25<jupyter_text>Project: 4• Use iris flower dataset from sklearn library and try to form clusters of flowers using petal width and length features. • Analyse the Data Visually before training • Drop other two features for simplicity. • Try to Use K Mean Clustering• Analyse the Data Visually by comparing before and after the training.<jupyter_code>import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import sklearn.metrics as sm
%matplotlib inline
iris = datasets.load_iris()
print (iris.data)

print (iris.target_names)

print (iris.target)
x = pd.DataFrame(iris.data, columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width'])
y = pd.DataFrame(iris.target, columns=['Target'])

x.head()
y.head()
plt.figure(figsize=(12,3))
colors = np.array(['red', 'green', 'blue'])
iris_targets_legend = np.array(iris.target_names)
red_patch = mpatches.Patch(color='red', label='Setosa')
green_patch = mpatches.Patch(color='green', label='Versicolor')
blue_patch = mpatches.Patch(color='blue', label='Virginica')


plt.subplot(1, 2, 1)
plt.scatter(x['Sepal Length'], x['Sepal Width'], c=colors[y['Target']])
plt.title('Sepal Length vs Sepal Width')
plt.legend(handles=[red_patch, green_patch, blue_patch])

plt.subplot(1,2,2)
plt.scatter(x['Petal Length'], x['Petal Width'], c= colors[y['Target']])
plt.title('Petal Length vs Petal Width')
plt.legend(handles=[red_patch, green_patch, blue_patch])
#just taking clusters as 3 for instance
iris_k_mean_model = KMeans(n_clusters=3)
iris_k_mean_model.fit(x)
print (iris_k_mean_model.labels_)
print (iris_k_mean_model.cluster_centers_)

plt.figure(figsize=(12,3))

colors = np.array(['red', 'green', 'blue'])

predictedY = np.choose(iris_k_mean_model.labels_, [1, 0, 2]).astype(np.int64)

plt.subplot(1, 2, 1)
plt.scatter(x['Petal Length'], x['Petal Width'], c=colors[y['Target']])
plt.title('Before classification')
plt.legend(handles=[red_patch, green_patch, blue_patch])

plt.subplot(1, 2, 2)
plt.scatter(x['Petal Length'], x['Petal Width'], c=colors[predictedY])
plt.title("Model's classification")
plt.legend(handles=[red_patch, green_patch, blue_patch])
sm.accuracy_score(predictedY, y['Target'])
#Confustion matrix measure

sm.confusion_matrix(predictedY, y['Target'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11187594553706505, 'fraction_numerical': 0.0226928895612708, 'mean_word_length': 5.724821973550356, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '478534', 'license': 'CC0-1.0', 'n_tokens_mistral': 4445, 'n_tokens_neox': 4172, 'n_words': 1196, 'path': 'Online Certificate Course in Data Science and Machine Learning rearranged/18 Mini Project ML/mini project ML.ipynb', 'repo_name': 'jsrikantappa/nielit', 'size': '13220'}