diff --git a/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/.ipynb_checkpoints/MLmodel-checkpoint.ipynb b/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/.ipynb_checkpoints/MLmodel-checkpoint.ipynb new file mode 100644 index 0000000..c195fe5 --- /dev/null +++ b/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/.ipynb_checkpoints/MLmodel-checkpoint.ipynb @@ -0,0 +1,181 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "' ML Assignment (SVM-linear) (Accuracy: 97.202 %)'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "''' ML Assignment (SVM-linear) (Accuracy: 97.202 %)'''" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# importing files\n", + "import numpy as np\n", + "import pandas as pd\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# getting the dataset\n", + "from sklearn import datasets\n", + "cancer=datasets.load_breast_cancer()\n", + "dataset=pd.DataFrame(cancer['data'],columns=cancer['feature_names'])\n", + "# 0:-malignant,1:-benign\n", + "dataset['Target']=cancer['target']" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# features and target\n", + "X=dataset.iloc[:,:-1].values\n", + "y=dataset.iloc[:,30].values" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# spliting data into test set and train set\n", + "from sklearn.model_selection import train_test_split\n", + "X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# feature scaling \n", + "from sklearn.preprocessing import StandardScaler\n", + "sc_X=StandardScaler()\n", + "X_train=sc_X.fit_transform(X_train)\n", + "X_test=sc_X.transform(X_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n", + " decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',\n", + " max_iter=-1, probability=False, random_state=0, shrinking=True,\n", + " tol=0.001, verbose=False)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# making SVM-linear classifier\n", + "from sklearn.svm import SVC\n", + "classifier=SVC(kernel='linear',random_state=0)\n", + "classifier.fit(X_train,y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# predicting test set\n", + "y_pred=classifier.predict(X_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "# making of confusion matrix\n", + "from sklearn.metrics import confusion_matrix\n", + "cm=confusion_matrix(y_test,y_pred)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[51, 2],\n", + " [ 2, 88]], dtype=int64)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/MLmodel.ipynb b/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/MLmodel.ipynb new file mode 100644 index 0000000..c195fe5 --- /dev/null +++ b/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/MLmodel.ipynb @@ -0,0 +1,181 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "' ML Assignment (SVM-linear) (Accuracy: 97.202 %)'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "''' ML Assignment (SVM-linear) (Accuracy: 97.202 %)'''" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# importing files\n", + "import numpy as np\n", + "import pandas as pd\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# getting the dataset\n", + "from sklearn import datasets\n", + "cancer=datasets.load_breast_cancer()\n", + "dataset=pd.DataFrame(cancer['data'],columns=cancer['feature_names'])\n", + "# 0:-malignant,1:-benign\n", + "dataset['Target']=cancer['target']" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# features and target\n", + "X=dataset.iloc[:,:-1].values\n", + "y=dataset.iloc[:,30].values" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# spliting data into test set and train set\n", + "from sklearn.model_selection import train_test_split\n", + "X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# feature scaling \n", + "from sklearn.preprocessing import StandardScaler\n", + "sc_X=StandardScaler()\n", + "X_train=sc_X.fit_transform(X_train)\n", + "X_test=sc_X.transform(X_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n", + " decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',\n", + " max_iter=-1, probability=False, random_state=0, shrinking=True,\n", + " tol=0.001, verbose=False)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# making SVM-linear classifier\n", + "from sklearn.svm import SVC\n", + "classifier=SVC(kernel='linear',random_state=0)\n", + "classifier.fit(X_train,y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# predicting test set\n", + "y_pred=classifier.predict(X_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "# making of confusion matrix\n", + "from sklearn.metrics import confusion_matrix\n", + "cm=confusion_matrix(y_test,y_pred)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[51, 2],\n", + " [ 2, 88]], dtype=int64)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/README.md b/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/README.md new file mode 100644 index 0000000..f7097bb --- /dev/null +++ b/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/README.md @@ -0,0 +1,24 @@ +These are the accuracies I got with different models: +1. Logistic Regressor: 95.804 % +2. KNN: 95.104 % +3. SVM (linear): 97.202 % +4. SVM (rbf): 96.503 % +5. Naive Bayes: 91.608 % +6. Decision Tree: 95.804 % +7. Random Forest: 95.804 % + + +Model Used: SVM (linear) with 97.202 % accuracy + + +Code flow: +1. Importing files +2. Getting the dataset +3. Making of features and target +4. Splitting of dataset into test and training sets +5. Feature scaling (Important for SVM linear) +6. Making of SVM classifier with linear kernel +7. Predicting test set +8. Making of confusion matrix + + diff --git a/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/desktop.ini b/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/desktop.ini new file mode 100644 index 0000000..ab17096 --- /dev/null +++ b/Introduction-to-Data-Science/Week-6/Assignment-5/Skanda/desktop.ini @@ -0,0 +1,4 @@ +[ViewState] +Mode= +Vid= +FolderType=Documents diff --git a/week3/skanda_assignment4/3.py b/week3/skanda_assignment4/3.py new file mode 100644 index 0000000..c0494a2 --- /dev/null +++ b/week3/skanda_assignment4/3.py @@ -0,0 +1,32 @@ + + +l=[1,2,3,4,5] + +print('Following is a list:',l) + +print('Trying to access element at index 10: ') + +try: + print('Element at index 10:',l[10]) + +except IndexError: + print('Index Error!') + + +print('5 % 0 = ') + +try: + print(5%0) + +except ZeroDivisionError: + + print('ZeroDivisionError!') + + +print('x not defined, x = ') + +try: + print(x) + +except NameError: + print('NameError!') diff --git a/week3/skanda_assignment4/4.py b/week3/skanda_assignment4/4.py new file mode 100644 index 0000000..40d8424 --- /dev/null +++ b/week3/skanda_assignment4/4.py @@ -0,0 +1,44 @@ + +fp=open('reverse.txt','w') + +op='y' +while op=='y' or op=='Y': + line=input('Enter a line into the file: ') + fp.write(line+'\n') + op=input('Want to enter more content? : ') + + +fp.close() + + +fp=open('reverse.txt','r') + +fp.seek(0,2) +end=fp.tell() +fp.seek(0,0) + +content=[] +line=fp.readline() + +while fp.tell()!=end: + content.append(line) + line=fp.readline() + + +content.append(line) + +#print('Content: ',content) + +fp.close() + +content.reverse() + +fp=open('reverse.txt','w') + +for i in content: + fp.write(i) + +print('The File has been reversed.') + + + diff --git a/week3/skanda_assignment4/Descriptive.txt b/week3/skanda_assignment4/Descriptive.txt new file mode 100644 index 0000000..ce38c44 --- /dev/null +++ b/week3/skanda_assignment4/Descriptive.txt @@ -0,0 +1,8 @@ + +1. ab+ mode opens a file in append and read mode in binary format. It sets the pointer at the end of the file. If file is not present, it opens a new file in read and write mode. + + +2. When a file is opened, the entire file is stored in a temporary place in memory called buffer. All the data is first read or written to this buffer. When the file is closed, an EOF character is appended to this buffer and everything on the buffer is written onto the file. It is used to speeden up the process of writing or reading from a file as it will be very slow to access the file directly from the secondary memory everytime a change is done on the file. + + + diff --git a/week3/skanda_assignment5/10.py b/week3/skanda_assignment5/10.py new file mode 100644 index 0000000..874c38d --- /dev/null +++ b/week3/skanda_assignment5/10.py @@ -0,0 +1,51 @@ + +class Person: + def __init__(self,first_name,last_name,phone_number,*email): + self.first_name=first_name + self.last_name=last_name + self.phone_number=phone_number + self.email=email + + +class Address_book: + def __init__(self): + self.contacts=[] + + def add_contact(self,first_name,last_name,phone_number,*email): + temp=Person(first_name,last_name,phone_number,*email) + self.contacts.append(temp) + + def lookup_contact(self,last_name,first_name=None): + for i in self.contacts: + if first_name==None and i.last_name==last_name: + self.print_contact(i) + elif i.first_name==first_name and i.last_name==last_name: + self.print_contact(i) + + def print_contact(self,person): + print('First Name:',person.first_name) + print('Last Name:',person.last_name) + print('Phone Number:',person.phone_number) + print('Email:',person.email) + print() + + + + +# a-An object of Address_book class +a=Address_book() + +#adding contacts +a.add_contact('abcd','qwerty','9786432188','abcd@gmail.com','abcd.qwert@gmail.com') +a.add_contact('abcy','qwerty','7777755555','qwerty.abcy@gmail.com') +a.add_contact('cdefg','tyuio','1234567890','cde.tyu@gmail.com','cdefg.ty@gmail.com') +a.add_contact('asdfgh','fghjk','9008889900','asdfgh@gmail.com','fghy@gmail.com') + +#searching using only last name +a.lookup_contact('qwerty') + +#searching using first name and last name +a.lookup_contact('fghjk','asdfgh') + + + diff --git a/week3/skanda_assignment5/9.py b/week3/skanda_assignment5/9.py new file mode 100644 index 0000000..53f305c --- /dev/null +++ b/week3/skanda_assignment5/9.py @@ -0,0 +1,46 @@ + + +import random + +class Card: + suit=['Hearts','Diamonds','Clubs','Spades'] + value=['A',2,3,4,5,6,7,8,9,10,'J','Q','K'] + + +class Deck(Card): + def __init__(self): + self.cards=[(a,b) for a in self.value for b in self.suit] + + def deal(self): + t=random.choice(self.cards) + print('Dealt:',t) + self.cards.remove(t) + + def Shuffle(self): + self.cards=[(a,b) for a in self.value for b in self.suit] + random.shuffle(self.cards) + + +a=Deck() #An object of class Deck + +#prints all the cards present in a deck currently +print('Cards present in a deck: ',a.cards) + +#gets back all the dealt cards and shuffles the deck +a.Shuffle() + +print() + +#dealing cards +a.deal() +a.deal() +a.deal() + +print('Cards present in a deck: ',a.cards) + +a.Shuffle() + +print() +print('Cards present in a deck: ',a.cards) + + diff --git a/week3/skanda_assignment5/Descriptive b/week3/skanda_assignment5/Descriptive new file mode 100644 index 0000000..7432bc9 --- /dev/null +++ b/week3/skanda_assignment5/Descriptive @@ -0,0 +1,33 @@ + +1. A class is single entity or type in which all the related data and methods are contained. + + +2. An object is a variable of the type class. + + +3. An instance is a variable of the class type. It can access the data and the methods which the class contains. + + +4. syntax: + +class ClassName (InheritedClass): #InheritedClass opional + #methods + #attributes + + +5. Methods are functions defined inside a class which can only be used by an object of that class or object of any class that has inherited the original class. + + +6. Self is an argument passed to the methods which help in differentiating various objects of the same class. + + +7. The __init__() method is a constructor which is implicitly called whenever an object is created. Its primary function is to allocate memory for the object. + + +8. Suppose class A and B need same data and methods except that B requires additional attributes. Instead of maintaining the two different copies of the same functions and data in A and B, B can be inherited from A. This prevents duplicaton of code in B. + + + + + +