{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Basic\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"what is name ?mosh\n",
"HI mosh\n"
]
}
],
"source": [
"name = input(\"what is name ?\")\n",
"print('HI ' +name)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"what is your birthdate ? 2000\n",
"19\n"
]
}
],
"source": [
"name = input('what is your birthyear ? ')\n",
"year = 2019 -int(name);\n",
"print(year)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Type"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'str'>\n"
]
}
],
"source": [
"#type \n",
"\n",
"print(type(name))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"what is weight 45\n",
"20.25\n"
]
}
],
"source": [
"weight = input(\"what is weight \")\n",
"final = int(weight)*0.45\n",
"print(final)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"you're awesome\n",
"im \"Hritik\"\n",
"\n",
" Hii buddy \n",
"\n",
" im Hritik \n",
"\n",
" you look awesome \n",
"\n",
" thank you,\n",
" \n"
]
}
],
"source": [
"#if want to use this type statement -> year's then we have to use double quotes \"\"\n",
"string = \"you're awesome\"\n",
"print(string)\n",
"\n",
"# if want to assign \"important msg\" like this we have to use single quote ''\n",
"str = 'im \"Hritik\"'\n",
"print(str)\n",
"\n",
"# for multitline string ''' % ''' for sending mail \n",
"\n",
"mail = '''\n",
" Hii buddy \n",
"\n",
" im Hritik \n",
"\n",
" you look awesome \n",
"\n",
" thank you,\n",
" '''\n",
"\n",
"print(mail)\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p\n",
"r\n",
"python beginners\n",
"python beginners\n",
"pytho\n",
"pytho\n",
"yt\n",
"ython beginner\n"
]
}
],
"source": [
"course = 'python beginners'\n",
"print(course[0])\n",
"print(course[-2])\n",
"print(course[0:])\n",
"print(course[:])\n",
"print(course[:5])\n",
"print(course[0:5])\n",
"print(course[1:3]) #exclude 3 only takes 1,2\n",
"\n",
"print(course[1:-1]) # exclude -1 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Inbuilt - Function"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n"
]
}
],
"source": [
"#length of string including space\n",
"c = 'hii buddy'\n",
"print(len(c))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"python is very imp lang\n",
"PYTHON IS VERY IMP LANG\n",
"2\n",
"2\n",
"Python Is Very Imp Lang\n",
"python are very imp lang\n"
]
}
],
"source": [
"#various function \n",
"msg = 'python is very imp lang'\n",
"print(msg.lower())\n",
"print(msg.upper())\n",
"print(msg.count('i'))\n",
"print(msg.find('t'))\n",
"print(msg.title())\n",
"print(msg.replace('is','are'))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22\n"
]
}
],
"source": [
"#ordering \n",
"# exp > (mul or div) > (add or sub)\n",
"exp = 10 + 3*2**2\n",
"print(exp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Math function"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"8\n"
]
}
],
"source": [
"#Math function\n",
"# 1) round\n",
"x = 6.5\n",
"y = 7.66\n",
"print(round(x))\n",
"print(round(y))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.5\n"
]
}
],
"source": [
"# 2) abs -> return positive no.\n",
"x = -3.5\n",
"print(abs(x))"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"2\n"
]
}
],
"source": [
"# 3) Mathametical function\n",
"import math\n",
"pi = 3.142\n",
"math.cos((60*pi)/180)\n",
"\n",
"# ceil and floor\n",
"print(math.ceil(2.9))\n",
"print(math.floor(2.9))\n",
"\n",
"#thier are many more maths module"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# If - Statement"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"nothing\n",
"SUM OF X AND Y : 15\n"
]
}
],
"source": [
"#if statement\n",
"\n",
"x = 5\n",
"y = 10\n",
"\n",
"if (x>y):\n",
" print(\"truee\")\n",
"elif (x==y):\n",
" print(\"falsee\")\n",
"else:\n",
" print(\"nothing\")\n",
" \n",
"print(f\"SUM OF X AND Y : {x+y}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Format"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = \"Hii\"\n",
"y = 'Hritik {}'\n",
"z = 'Jaiswal'\n",
"print(f\"sum of string is x + y : {x+y}\")\n",
"print(y.format(z))"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"# we can use 'and' & 'or' in if statement\n",
"a=2 \n",
"b=3\n",
"c=4\n",
"if (a<b) and (c>b):\n",
" print('True')\n",
"elif (a==b) or (c>a):\n",
" print('Truee')\n",
"elif (b>a) and not(b>c):\n",
" print('Trueee')\n",
"else:\n",
" print('false')\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Weight :45\n",
"(L)bs or (K)gv\n",
"100.0\n"
]
}
],
"source": [
"#Exercise\n",
"\n",
"weight = int(input('Weight :'))\n",
"unit = input('(L)bs or (K)g')\n",
"if unit.lower()=='l' or unit.upper()=='L':\n",
" c = weight*0.45;\n",
"else:\n",
" c = weight/0.45;\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# While loop"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"*\n",
"**\n",
"***\n",
"****\n"
]
}
],
"source": [
"#while\n",
"\n",
"i=1\n",
"while (i<5):\n",
" print('*'*i)\n",
" i+=1\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# for loop"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T\n",
"e\n",
"a\n",
"c\n",
"h\n",
"e\n",
"r\n"
]
}
],
"source": [
"#for loop\n",
"for item in 'Teacher':\n",
" print(item)"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hii\n",
"buddy \n",
"whats\n",
"up\n",
"-----------------\n",
"0\n",
"1\n",
"2\n",
"3\n",
"-----------------\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"-----------------\n",
"2\n",
"5\n",
"-----------------\n",
"5\n",
"6\n",
"7\n",
"8\n"
]
}
],
"source": [
"for i in ['hii','buddy ','whats','up']:\n",
" print(i)\n",
"print('-----------------')\n",
"for i in range(4):\n",
" print(i)\n",
"print('-----------------')\n",
"for i in range(2,8):\n",
" print(i)\n",
"print('-----------------')\n",
"for i in range(2,8,3):\n",
" print(i)\n",
"print('-----------------')\n",
"for i in [5,6,7,8]:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"14\n",
"-----------\n",
"14\n"
]
}
],
"source": [
"r = [2,3,4,5]\n",
"total = 0\n",
"for i in r:\n",
" total+=i\n",
"print(total)\n",
"print('-----------')\n",
"r = [2,3,4,5]\n",
"e = sum(r)\n",
"print(e)"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0,0)\n",
"(0,1)\n",
"(0,2)\n",
"(1,0)\n",
"(1,1)\n",
"(1,2)\n",
"(2,0)\n",
"(2,1)\n",
"(2,2)\n",
"(3,0)\n",
"(3,1)\n",
"(3,2)\n"
]
}
],
"source": [
"for i in range(4):\n",
" for j in range(3):\n",
" print(f'({i},{j})')"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"$\n",
"$$\n",
"$$$\n",
"$$$$\n",
"$$$$$\n"
]
}
],
"source": [
"#array\n",
"num = [1,2,3,4,5]\n",
"for i in num:\n",
" print('$'*i)"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ab \n",
"ae\n"
]
}
],
"source": [
"name = ['ab ','ac','ad','ae']\n",
"print(name[0])\n",
"print(name[-1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# List"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3]\n",
"[4, 5, 6]\n",
"[7, 8, 9]\n",
"-----print individual item-----\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
"#2D-List \n",
"matrix = [[1,2,3],[4,5,6],[7,8,9]]\n",
"for i in matrix:\n",
" print(i)\n",
"print('-----print individual item-----')\n",
"for i in matrix:\n",
" for j in i:\n",
" print(j)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Inbuilt - Function"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-----Append---------\n",
"[2, 3, 4, 6, 9]\n",
"-----Insert---------\n",
"[2, 3, 8, 4, 6, 9]\n",
"-----Remove---------\n",
"[2, 3, 4, 6, 9]\n",
"-----pop---------\n",
"[2, 3, 4, 6]\n",
"-----clear---------\n",
"[]\n"
]
}
],
"source": [
"#working with function \n",
"# 1.append 2.insert 3.remove 4.pop 5.clear\n",
"number = [2,3,4,6]\n",
"print('-----Append---------')\n",
"number.append(9)\n",
"print(number)\n",
"print('-----Insert---------')\n",
"number.insert(2,8)\n",
"print(number)\n",
"print('-----Remove---------')\n",
"number.remove(8)\n",
"print(number)\n",
"print('-----pop---------')\n",
"number.pop()\n",
"print(number)\n",
"print('-----clear---------')\n",
"number.clear()\n",
"print(number)"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"2\n"
]
}
],
"source": [
"#index \n",
"list_item = [2,4,6,7,2,4]\n",
"print(list_item.index(6))\n",
"print(list_item.count(2))\n"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 5, 8]\n",
"[8, 5, 3, 2, 1]\n"
]
}
],
"source": [
"# 1.sort 2.reverse \n",
"item = [3,5,2,8,1]\n",
"item.sort()\n",
"print(item)\n",
"item.reverse()\n",
"print(item)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 4, 5]\n"
]
}
],
"source": [
"#list will update if we changing list item before calling copy function ,\n",
"# but list will not be change when u are appending and deleting after copy function\n",
"a = [2,3,4,5]\n",
"b = a.copy()\n",
"a.append(10)\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 4, 5]\n"
]
}
],
"source": [
"#excercise -> remove a duplicate no. from the list\n",
"numbers = [2,3,4,4,3,5]\n",
"unique = []\n",
"\n",
"for i in numbers:\n",
" if i not in unique:\n",
" unique.append(i)\n",
" \n",
"print(unique)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# tuple"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"#tuple -> we can not append , remove , pop , insert , u can not modify your list .\n",
"#only thing u can do -> count , index a item in list\n",
"\n",
"## The main diffrence between tuple and list is :- tuple is immutable and list is mutable\n",
"## that mean we can not change tuple value by modifying it but in list we can do that.\n",
"number = (1,2,3)\n",
"print(number[0])\n",
"\n",
"# this will throw error :\n",
"# number[0] = 5\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Set"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{1,2,3}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 4, 5}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# display unique value \n",
"{1,1,4,2,2,5}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 4, 8}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set([1,2,4,4,8,8,4])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# add value to the set\n",
"s = {1,2,3}\n",
"s.add(5)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3, 5}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Map"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def times(var):\n",
" return var*2;\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"times(2)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"seq = [1,2,3]\n",
"\n",
"list(map(times,seq))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lambda "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"# instead of writing like this we can write \n",
"# def times(var):\n",
"# return var*2;\n",
"\n",
"t = lambda var : var*2"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t(3)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(t,seq))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Filter"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[False, True, False]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda num : num %2 ==0,seq))"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(lambda num : num %2 ==0,seq))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Unpacking"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"#unpacking\n",
"coordinates = [2,3,4]\n",
"# x = coordinates[0]\n",
"# y = coordinates[1]\n",
"# z = coordinates[2]\n",
"# instead of writing like this we can write \n",
"x,y,z = coordinates #unpacking -> work with both list and tuple\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hritik Jaiswal\n",
"may 31 2000\n",
"None\n",
"HritiK Dinesh Jaiswal\n"
]
}
],
"source": [
"#Dictionaries -> contain key - value pairs \n",
"\n",
"people = {\n",
" \"name\" :\"Hritik Jaiswal\",\n",
" \"age\" : 19,\n",
" \"is_male\" : True\n",
"}\n",
"print(people[\"name\"])\n",
"\n",
"# u can not write like -> print(people[\"Name\"])\n",
"# we can use GET method to take key-value pair if its not present and display key value pair \n",
"print(people.get(\"birth\",\"may 31 2000\"))\n",
"print(people.get(\"gender\")) #None -> is the object that represent absence of the value\n",
"\n",
"people[\"name\"] = \"HritiK Dinesh Jaiswal\" # modify name\n",
"print(people.get(\"name\"))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"phone : 105\n",
"1\n",
"0\n",
"5\n",
"One zero Five \n"
]
}
],
"source": [
"#excersice -> inuput 1234 display output : one two three four\n",
"phone = input(\"phone : \")\n",
"\n",
"dict = {\n",
" \"1\" :\"One\",\n",
" \"2\":\"Two\",\n",
" \"3\" :\"Three\",\n",
" \"4\" :\"Four\",\n",
" \"5\" :\"Five\",\n",
" \"6\" :\"Six\",\n",
" \"7\" :\"Seven\",\n",
" \"8\" :\"Eight\",\n",
" \"9\" :\"Nine\",\n",
" \"0\" :\"zero\",\n",
"}\n",
"output = \"\" \n",
"for i in phone:\n",
" \n",
" output += (dict.get(i,\"?\")) + \" \" \n",
" print(i)\n",
"print(output) "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the word : one seven two\n",
"['one', 'seven', 'two']\n",
"1\n",
"7\n",
"2\n"
]
}
],
"source": [
"#excerisce -> opposite \n",
"words = [\"one\", \"two\",\"three\",\"four\",\"five\",\"six\", \"seven\",\"eight\",\"nine\",\"zero\"]\n",
"\n",
"word = input(\"Enter the word : \")\n",
"mapping = {\n",
" \"one\":\"1\", \n",
" \"two\":\"2\",\n",
" \"three\":\"3\",\n",
" \"four\":\"4\",\n",
" \"five\":\"5\",\n",
" \"six\":\"6\", \n",
" \"seven\":\"7\",\n",
" \"eight\":\"8\",\n",
" \"nine\":\"9\",\n",
" \"zero\":\"0\"\n",
"}\n",
"spliting = word.split()\n",
"print(spliting)\n",
"for i in spliting:\n",
" print(mapping.get(i,\"&\"))\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
">good morning :)\n",
"good morning 😄 \n"
]
}
],
"source": [
"#excersice -> print emoji\n",
"message = input(\">\")\n",
"words= message.split()\n",
"emoji = {\n",
" \":)\" :\"😄\",\n",
" \":(\" :\"😔\"\n",
"}\n",
"output = \"\"\n",
"for i in words:\n",
" output += emoji.get(i,i) + \" \"\n",
" \n",
"print(output)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Function "
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hii\n",
"im hritik\n"
]
}
],
"source": [
"#function\n",
"def text():\n",
" print(\"im hritik\")\n",
"print(\"hii\")\n",
"text()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Parameter and Arguments"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"thanks\n",
"Hii hritik and jaiswal\n"
]
}
],
"source": [
"#function with parameter\n",
"#parameter ->is placeholder that we passed to the function\n",
"#argumetnt -> is actual value that u gone pass inside function\n",
"def text(f_name, l_name):\n",
" print(f'Hii {f_name} and {l_name}')\n",
"print(\"thanks\")\n",
"text('hritik','jaiswal')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# keyword argument"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n",
"Here total is 5000 including shipping charges 2000 + discount 500\n"
]
}
],
"source": [
"#keyword argument -> its helpful when u don't want to pass argument in order u can pass in any order \n",
"def text(discount , shipping ,total ):\n",
" print(f'Here total is {total} including shipping charges {shipping} + discount {discount}')\n",
"print(\"hello\")\n",
"text(shipping=2000,total=5000,discount=500)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25\n"
]
}
],
"source": [
"#return statement \n",
"def square(num):\n",
" return num*num\n",
"result = square(5)\n",
"print(result)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27\n",
"None\n"
]
}
],
"source": [
"#tric -> if u don't write return statement then it will return object which is : none \n",
"def cube(n):\n",
" print(n**3)\n",
"print(cube(3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exception"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"your age : twenty\n",
"invalid value\n"
]
}
],
"source": [
"#exception -> when we try to input string value instead of int \n",
"# age = int(input(\"your age\"))\n",
"# print(age)\n",
"\n",
"try:\n",
" age = int(input(\"your age : \"))\n",
" print(age)\n",
"except ValueError:\n",
" print('invalid value')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"your age : 0\n",
"invalid value or age can't be negative \n"
]
}
],
"source": [
"\n",
"try:\n",
" age = int(input(\"your age : \"))\n",
" income = 20000\n",
" risk = float(income/age);\n",
" print(f'risk is {risk}')\n",
"except ValueError and ZeroDivisionError:\n",
" print(\"invalid value or age can't be negative \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Class"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"area\n"
]
}
],
"source": [
"#class \n",
"# 1) type -1\n",
"\n",
"class rect:\n",
" def rect_area(self):\n",
" print(\"area\")\n",
"p = rect()\n",
"p.rect_area()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hritik Jaiswal\n",
"Hritik Jaiswal\n"
]
}
],
"source": [
"# 2) type -2\n",
"class Employee:\n",
" def __init__(self, first, last , salary):\n",
" self.first = first\n",
" self.last = last\n",
" self.salary = salary\n",
" self.email = first + '.'+last +'@gmail.com'\n",
" \n",
" def fullname(self):\n",
" return \"{} {}\".format(self.first,self.last)\n",
"\n",
"emp1 = Employee('Hritik','Jaiswal',5000)\n",
"emp2 = Employee('Aniket','Jaiswal',6000)\n",
"\n",
"#their are two methods \n",
"print(emp1.fullname())\n",
"print(Employee.fullname(emp1))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"area of square :9\n",
"area of rectangle is : 6\n"
]
}
],
"source": [
"# 3) type -3\n",
"class Point:\n",
" def __init__(self,a,l,h):\n",
" self.a = a\n",
" self.l = l\n",
" self.h = h\n",
" \n",
" def square(self):\n",
" print(f\"area of square :{self.a*self.a}\")\n",
" \n",
" def rectangle(self):\n",
" print(\"area of rectangle is : {}\".format(self.l*self.h))\n",
" \n",
"\n",
"#create a object \n",
"point1 = Point(3,2,3)\n",
"point1.square()\n",
"point1.rectangle()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Inheritance"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bark\n",
"walk\n"
]
}
],
"source": [
"#inheritance -> dog and cat are inherite a class mammel\n",
"class mammel:\n",
" def walk(self):\n",
" print(\"walk\")\n",
"\n",
"class dog(mammel):\n",
" def bark(self):\n",
" print(\"bark\")\n",
"\n",
"class cat(mammel):\n",
" pass\n",
"\n",
"dog1 = dog()\n",
"dog1.bark()\n",
"\n",
"cat1 = cat()\n",
"cat1.walk()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Module\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.0\n"
]
}
],
"source": [
"#module -> module.ipynb file which we have created we can directly import function also\n",
"#we need to install from anaconda prompt -> pip install import-ipynb\n",
"import import_ipynb\n",
"\n",
"import module\n",
"from module import cm2m\n",
"\n",
"cm2m(100)\n",
"m2cm"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"4\n"
]
}
],
"source": [
"numbers = [5,4,6,8,10]\n",
"print(max(numbers))\n",
"print(min(numbers))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Packages\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"package -: we can create a seperate .py file and extract this file and import into another file as similar to module \n",
"\n",
"package is collections of different modules\n",
"\n",
"# Type of Module\n",
"\n",
" 1) Absolute module \n",
"from mypackage.mymodule1 import class A\n",
"obj = class A\n",
"\n",
" 2) relative module\n",
"if im working in \"module1\" & i want to import Class C from \"module2\" into my \"module1\"\n",
"\n",
"from module2 import classC\n",
"obj = classC()\n",
"\n",
"\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Random\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.8556515733440572\n",
"0.9018671283206765\n",
"0.6655666651378818\n"
]
}
],
"source": [
"import random\n",
"\n",
"for i in range(3):\n",
" print(random.random())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12\n",
"20\n",
"15\n"
]
}
],
"source": [
"for i in range(3):\n",
" print(random.randint(10,20))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"jaiswal\n"
]
}
],
"source": [
"# randomly choose the value \n",
"members = ['hritik', 'jaiswal','aniket','shweta']\n",
"show = random.choice(members)\n",
"print(show)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2,4)\n"
]
}
],
"source": [
"#excercise -> dice thrown give random value\n",
"\n",
"class Dice:\n",
" def roll(self):\n",
" x = (1,2,3,4,5,6)\n",
" y = (1,2,3,4,5,6)\n",
" m = random.choice(x)\n",
" l = random.choice(y)\n",
" print(\"({},{})\".format(m,l))\n",
" \n",
"\n",
"r = Dice()\n",
"r.roll()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(5, 6)\n"
]
}
],
"source": [
"# another method\n",
"\n",
"class Dice:\n",
" def roll(self):\n",
" first = random.randint(1,6)\n",
" second = random.randint(1,6)\n",
" return first,second\n",
"dice = Dice()\n",
"print(dice.roll())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Files and Directories"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"from pathlib import Path\n",
"path = Path(\".\")\n",
"print(path.exists())\n",
"\n",
"\n",
"#if u want to make new directory\n",
"\n",
"# path1 = Path(\"Files_Directories\")\n",
"# path1.mkdir()\n",
"\n",
"#when u want to remove directory\n",
"#path.rmdir()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ch02.ipynb\n",
"module.ipynb\n",
"Python-1.ipynb\n"
]
}
],
"source": [
"path2 = Path()\n",
"for file in path2.glob(\"*.ipynb\"):\n",
" print(file)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
".ipynb_checkpoints\n",
"ch02.ipynb\n",
"Files_Directories\n",
"module.ipynb\n",
"Python-1.ipynb\n"
]
}
],
"source": [
"path3 = Path()\n",
"for file in path3.glob(\"*\"):\n",
" print(file)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with spreadsheet"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"transaction_id\n",
"4\n"
]
}
],
"source": [
"import openpyxl as xl\n",
"from openpyxl.chart import BarChart,Reference\n",
"\n",
"# Here openpyxl -> package , chart -> module , BarChart -> class\n",
"#instead of passing a file name -> we can use function and store the path in \"filename\" variable and pass as argument to function \n",
"\n",
"wb = xl.load_workbook(r'C:\\Users\\Hritik Jaiswal\\Downloads\\Spreadsheet\\transactions.xlsx')\n",
"sheet = wb['Sheet1']\n",
"\n",
"\n",
"#method to get a cell \n",
"\n",
"cell = sheet['a1']\n",
"#another method -> cell = sheet.cell(1,1)\n",
"\n",
"print(cell.value)\n",
"#print max row\n",
"print(sheet.max_row)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.95\n",
"6.95\n",
"7.95\n"
]
}
],
"source": [
"#we have to modify value of the cell and store into another excel file\n",
"\n",
"for row in range(2,sheet.max_row+1):\n",
" cell = sheet.cell(row,3) # column is 3\n",
" print(cell.value)\n",
" corrected_value = cell.value * 0.9\n",
" \n",
" #now we have to place a corrected value into anther column \n",
" corrected_value_cell = sheet.cell(row,4) #add corrected value into the 4 column\n",
" \n",
" corrected_value_cell.value = corrected_value\n",
"#Excersice \n",
"\n",
"# u have to create a bar graph in excel\n",
"\n",
"\n",
"values = Reference(sheet, \n",
" \n",
" min_row=2,max_row = sheet.max_row,\n",
" \n",
" min_col = 4 , max_col = 4\n",
" )\n",
"\n",
"chart = BarChart()\n",
"chart.add_data(values)\n",
"sheet.add_chart(chart, 'f2')\n",
"\n",
"\n",
"wb.save(\"transaction2.xlsx\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Machine learning"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Steps :\n",
" \n",
" 1) Import the Data \n",
" 2) clean the Data\n",
" 3) split the Data into training/test sets\n",
" 4) create a model\n",
" 5) train the model\n",
" 6) make prediction\n",
" 7) Evaluate and Improve\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(16598, 11)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Importing a data set\n",
"import pandas as pd\n",
"df = pd.read_csv('vgsales.csv')\n",
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Rank</th>\n",
" <th>Year</th>\n",
" <th>NA_Sales</th>\n",
" <th>EU_Sales</th>\n",
" <th>JP_Sales</th>\n",
" <th>Other_Sales</th>\n",
" <th>Global_Sales</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>16598.000000</td>\n",
" <td>16327.000000</td>\n",
" <td>16598.000000</td>\n",
" <td>16598.000000</td>\n",
" <td>16598.000000</td>\n",
" <td>16598.000000</td>\n",
" <td>16598.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>8300.605254</td>\n",
" <td>2006.406443</td>\n",
" <td>0.264667</td>\n",
" <td>0.146652</td>\n",
" <td>0.077782</td>\n",
" <td>0.048063</td>\n",
" <td>0.537441</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>4791.853933</td>\n",
" <td>5.828981</td>\n",
" <td>0.816683</td>\n",
" <td>0.505351</td>\n",
" <td>0.309291</td>\n",
" <td>0.188588</td>\n",
" <td>1.555028</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>1.000000</td>\n",
" <td>1980.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.010000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>4151.250000</td>\n",
" <td>2003.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.060000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>8300.500000</td>\n",
" <td>2007.000000</td>\n",
" <td>0.080000</td>\n",
" <td>0.020000</td>\n",
" <td>0.000000</td>\n",
" <td>0.010000</td>\n",
" <td>0.170000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>12449.750000</td>\n",
" <td>2010.000000</td>\n",
" <td>0.240000</td>\n",
" <td>0.110000</td>\n",
" <td>0.040000</td>\n",
" <td>0.040000</td>\n",
" <td>0.470000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>16600.000000</td>\n",
" <td>2020.000000</td>\n",
" <td>41.490000</td>\n",
" <td>29.020000</td>\n",
" <td>10.220000</td>\n",
" <td>10.570000</td>\n",
" <td>82.740000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Rank Year NA_Sales EU_Sales JP_Sales \\\n",
"count 16598.000000 16327.000000 16598.000000 16598.000000 16598.000000 \n",
"mean 8300.605254 2006.406443 0.264667 0.146652 0.077782 \n",
"std 4791.853933 5.828981 0.816683 0.505351 0.309291 \n",
"min 1.000000 1980.000000 0.000000 0.000000 0.000000 \n",
"25% 4151.250000 2003.000000 0.000000 0.000000 0.000000 \n",
"50% 8300.500000 2007.000000 0.080000 0.020000 0.000000 \n",
"75% 12449.750000 2010.000000 0.240000 0.110000 0.040000 \n",
"max 16600.000000 2020.000000 41.490000 29.020000 10.220000 \n",
"\n",
" Other_Sales Global_Sales \n",
"count 16598.000000 16598.000000 \n",
"mean 0.048063 0.537441 \n",
"std 0.188588 1.555028 \n",
"min 0.000000 0.010000 \n",
"25% 0.000000 0.060000 \n",
"50% 0.010000 0.170000 \n",
"75% 0.040000 0.470000 \n",
"max 10.570000 82.740000 "
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.describe()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 'Wii Sports', 'Wii', ..., 3.77, 8.46, 82.74],\n",
" [2, 'Super Mario Bros.', 'NES', ..., 6.81, 0.77, 40.24],\n",
" [3, 'Mario Kart Wii', 'Wii', ..., 3.79, 3.31, 35.82],\n",
" ...,\n",
" [16598, 'SCORE International Baja 1000: The Official Game', 'PS2',\n",
" ..., 0.0, 0.0, 0.01],\n",
" [16599, 'Know How 2', 'DS', ..., 0.0, 0.0, 0.01],\n",
" [16600, 'Spirits & Spells', 'GBA', ..., 0.0, 0.0, 0.01]],\n",
" dtype=object)"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Shortcut"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we can use shift-tab to describe function\n",
"\n",
"all shortcut is present when we click 'h' in editor"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Real world problem\n",
"\n",
"recommend various music albums thier likely to buy based on age and gender\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Importing Data "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>age</th>\n",
" <th>gender</th>\n",
" <th>genre</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>20</td>\n",
" <td>1</td>\n",
" <td>HipHop</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>23</td>\n",
" <td>1</td>\n",
" <td>HipHop</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>25</td>\n",
" <td>1</td>\n",
" <td>HipHop</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>26</td>\n",
" <td>1</td>\n",
" <td>Jazz</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>29</td>\n",
" <td>1</td>\n",
" <td>Jazz</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>30</td>\n",
" <td>1</td>\n",
" <td>Jazz</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>31</td>\n",
" <td>1</td>\n",
" <td>Classical</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>33</td>\n",
" <td>1</td>\n",
" <td>Classical</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>37</td>\n",
" <td>1</td>\n",
" <td>Classical</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>20</td>\n",
" <td>0</td>\n",
" <td>Dance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>21</td>\n",
" <td>0</td>\n",
" <td>Dance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>25</td>\n",
" <td>0</td>\n",
" <td>Dance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>26</td>\n",
" <td>0</td>\n",
" <td>Acoustic</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>27</td>\n",
" <td>0</td>\n",
" <td>Acoustic</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>30</td>\n",
" <td>0</td>\n",
" <td>Acoustic</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>31</td>\n",
" <td>0</td>\n",
" <td>Classical</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>34</td>\n",
" <td>0</td>\n",
" <td>Classical</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>Classical</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" age gender genre\n",
"0 20 1 HipHop\n",
"1 23 1 HipHop\n",
"2 25 1 HipHop\n",
"3 26 1 Jazz\n",
"4 29 1 Jazz\n",
"5 30 1 Jazz\n",
"6 31 1 Classical\n",
"7 33 1 Classical\n",
"8 37 1 Classical\n",
"9 20 0 Dance\n",
"10 21 0 Dance\n",
"11 25 0 Dance\n",
"12 26 0 Acoustic\n",
"13 27 0 Acoustic\n",
"14 30 0 Acoustic\n",
"15 31 0 Classical\n",
"16 34 0 Classical\n",
"17 35 0 Classical"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"data = pd.read_csv('music.csv')\n",
"data\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>age</th>\n",
" <th>gender</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>18.000000</td>\n",
" <td>18.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>27.944444</td>\n",
" <td>0.500000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>5.127460</td>\n",
" <td>0.514496</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>20.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>25.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>28.000000</td>\n",
" <td>0.500000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>31.000000</td>\n",
" <td>1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>37.000000</td>\n",
" <td>1.000000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" age gender\n",
"count 18.000000 18.000000\n",
"mean 27.944444 0.500000\n",
"std 5.127460 0.514496\n",
"min 20.000000 0.000000\n",
"25% 25.000000 0.000000\n",
"50% 28.000000 0.500000\n",
"75% 31.000000 1.000000\n",
"max 37.000000 1.000000"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.describe()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# we will create two input data set that will be 'age' and 'gender'\n",
"# we will pass 'age' and 'gender' and based on the input we predict the output \n",
"# output will be stored in 'genre' \n",
"\n",
"X = data.drop(columns=['genre'])\n",
"\n",
"Y = data['genre']\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Learning and Predicting"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['Dance', 'HipHop'], dtype=object)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.tree import DecisionTreeClassifier\n",
"\n",
"model = DecisionTreeClassifier()\n",
"# takes two attributes 1. input dataset 2. output dataset\n",
"model.fit(X,Y)\n",
"\n",
"\n",
"#let make a prediction by passing input Here 22 is age and 0 is female\n",
"\n",
"prediction = model.predict([[22,0],[25,1]])\n",
"prediction\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Calculating the Accuracy"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" age gender\n",
"0 20 1\n",
"3 26 1\n",
"5 30 1\n",
"6 31 1\n",
"12 26 0\n",
"9 20 0\n",
"11 25 0\n",
"2 25 1\n",
"10 21 0\n",
"Accuracy is : 0.6666666666666666\n"
]
}
],
"source": [
"# for calculating the accuracy we need to test and train the model \n",
"# generally 70-80% data need to training and 20-30% for testing\n",
"\n",
"from sklearn.model_selection import train_test_split \n",
"\n",
"# Here we use 20% for testing and check with respect to the predicted value\n",
"# the function will return 4 tuple we have to get that result into a variable \n",
"\n",
"X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.5)\n",
"print(X_train)\n",
"model.fit(X_train,Y_train)\n",
"\n",
"# we pass input as X_test in attribute\n",
"prediction = model.predict(X_test)\n",
"\n",
"# now to check accurancy we have to compair the prediction with y_test()\n",
"from sklearn.metrics import accuracy_score\n",
"\n",
"score = accuracy_score(Y_test,prediction)\n",
"print(\"Accuracy is : {}\".format(score))\n",
"\n",
"#every time we run are model accuracy will be changing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Model Persistance"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"for training model again again takes lot of time instead \n",
"we can save the trained model in one joblib file \n",
"\n",
"run these piece of code after applying model i.e after :\n",
"\n",
" model = DecisionTreeClassifier()\n",
" \n",
" from sklearn.externals import joblib \n",
" joblib.dump(model,'model-recommender.joblib')\n",
"\n",
"after runing these commment the trained model syntax and and then direclty load \n",
"\n",
" joblib.load('model-recommender.joblib')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Visualizing a Decision Tree\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"# u can see a graph i.e decision tree on visual studio code \n",
"# by clicking a sidebar preview button\n",
" \n",
"from sklearn import tree\n",
"tree.export_graphviz(model,\n",
" out_file = \"music-recommender.dot\",\n",
" class_names = sorted(Y.unique()),\n",
" label = 'all',\n",
" rounded =True ,\n",
" filled= True\n",
" )\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}