├── 020_NIRMAL.html ├── 020_NIRMAL.ipynb ├── Become master in Python.ipynb ├── Boston house price prediction.ipynb ├── EcommerceCustomers ├── IRIS data analysis.ipynb ├── Important details ├── KNN algorithm using IRIS dataset.ipynb ├── LICENSE ├── README.md ├── SUV cars data analysis.ipynb ├── SUV_cars_data.csv ├── Titanic data analysis.ipynb ├── akshay.ipynb ├── github.ipynb ├── headbrain.csv ├── iris.csv ├── linear regression practice.ipynb ├── numpy basic.ipynb ├── numpy1.ipynb ├── numpy2.ipynb ├── practice for interview.ipynb └── train.csv /Become master in Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "ename": "SyntaxError", 10 | "evalue": "invalid syntax (, line 1)", 11 | "output_type": "error", 12 | "traceback": [ 13 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m What Does It Take to Be An Expert At Python\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "What Does It Take to Be An Expert At Python\n", 19 | "Notebook based off James Powell's talk at PyData 2017'\n", 20 | "https://www.youtube.com/watch?v=7lmCu8wz8ro\n", 21 | "\n", 22 | "If you want to become an expert in Python, you should definitely watch this PyData talk from James Powell.\n", 23 | "\n", 24 | "Video Index\n", 25 | "metaclasses: 18:50\n", 26 | "metaclasses(explained): 40:40\n", 27 | "decorator: 45:20\n", 28 | "generator: 1:04:30\n", 29 | "context manager: 1:22:37\n", 30 | "summary: 1:40:00\n", 31 | "Definitions Python is a language orientated around protocols - Some behavior or syntax or bytecode or some top level function and there is a way to tell python how to implement that on an arbitrary object via underscore methods. The exact correspondance is usually guessable, but if you can't guess it you can it... google python data model\n", 32 | "\n", 33 | "Metaclass Mechanism: Some hook into the class construction process. Questions: Do you have these methods implemented. Meaning: Library code & User code? How do you enforce a constraint?\n", 34 | "\n", 35 | "Decorator Hooks into idea that everything creates a structure at run time. Wrap sets of functions with a before and after behavior.\n", 36 | "\n", 37 | "Generators Take a single computation that would otherwise run eagerly from the injection of its parameters to the final computation and interleaving with other code by adding yield points where you can yield the intermediate result values or one small piece of the computation and also yield back to the caller. Think of a generator of a way to take one long piece of computation and break it up into small parts.\n", 38 | "\n", 39 | "Context managers Two structures that allow you to tie two actions together. A setup action and a teardown action and make sure they always happen in concordance with each other.\n", 40 | "\n", 41 | "In [7]:\n", 42 | "# some behavior that I want to implement -> write some __ function __\n", 43 | "# top-level function or top-level syntax -> corresponding __\n", 44 | "# x + y -> __add__\n", 45 | "# init x -> __init__\n", 46 | "# repr(x) --> __repr__\n", 47 | "# x() -> __call__\n", 48 | "\n", 49 | "class Polynomial:\n", 50 | " def __init__(self, *coeffs):\n", 51 | " self.coeffs = coeffs\n", 52 | " \n", 53 | " def __repr__(self):\n", 54 | " return 'Polynomial(*{!r})'.format(self.coeffs)\n", 55 | " \n", 56 | " def __add__(self, other):\n", 57 | " return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))\n", 58 | " \n", 59 | " def __len__(self):\n", 60 | " return len(self.coeffs)\n", 61 | " \n", 62 | " def __call__(self):\n", 63 | " pass\n", 64 | "3 Core Patterns to understand object orientation\n", 65 | "Protocol view of python\n", 66 | "Built-in inheritance protocol (where to go)\n", 67 | "Caveats around how object orientation in python works\n", 68 | "In [8]:\n", 69 | "p1 = Polynomial(1, 2, 3)\n", 70 | "p2 = Polynomial(3, 4, 3)\n", 71 | "In [9]:\n", 72 | "p1 + p2\n", 73 | "Out[9]:\n", 74 | "Polynomial(*(5, 7, 7))\n", 75 | "In [10]:\n", 76 | "len(p1)\n", 77 | "Out[10]:\n", 78 | "3\n", 79 | "Metaclasses\n", 80 | "In [26]:\n", 81 | "# File 1 - library.py\n", 82 | "\n", 83 | "class Base:\n", 84 | " def food(self):\n", 85 | " return 'foo'\n", 86 | "In [27]:\n", 87 | "# File2 - user.py\n", 88 | "\n", 89 | "assert hasattr(Base, 'foo'), \"you broke it, you fool!\"\n", 90 | "\n", 91 | "class Derived(Base):\n", 92 | " def bar(self):\n", 93 | " return self.foo\n", 94 | "---------------------------------------------------------------------------\n", 95 | "AssertionError Traceback (most recent call last)\n", 96 | " in ()\n", 97 | " 1 # File2 - user.py\n", 98 | " 2 \n", 99 | "----> 3 assert hasattr(Base, 'foo'), \"you broke it, you fool!\"\n", 100 | " 4 \n", 101 | " 5 class Derived(Base):\n", 102 | "\n", 103 | "AssertionError: you broke it, you fool!\n", 104 | "In [41]:\n", 105 | "# File 1 - library.py\n", 106 | "\n", 107 | "class Base:\n", 108 | " def foo(self):\n", 109 | " return self.bar()\n", 110 | "In [42]:\n", 111 | "# File2 - user.py\n", 112 | "\n", 113 | "assert hasattr(Base, 'foo'), \"you broke it, you fool!\"\n", 114 | "\n", 115 | "class Derived(Base):\n", 116 | " def bar(self):\n", 117 | " return 'bar'\n", 118 | "In [45]:\n", 119 | "Derived.bar\n", 120 | "Out[45]:\n", 121 | "\n", 122 | "In [18]:\n", 123 | "def _():\n", 124 | " class Base:\n", 125 | " pass\n", 126 | "\n", 127 | "from dis import dis\n", 128 | "In [19]:\n", 129 | "dis(_) # LOAD_BUILD_CLASS\n", 130 | " 2 0 LOAD_BUILD_CLASS\n", 131 | " 2 LOAD_CONST 1 (\", line 2>)\n", 132 | " 4 LOAD_CONST 2 ('Base')\n", 133 | " 6 MAKE_FUNCTION 0\n", 134 | " 8 LOAD_CONST 2 ('Base')\n", 135 | " 10 CALL_FUNCTION 2\n", 136 | " 12 STORE_FAST 0 (Base)\n", 137 | " 14 LOAD_CONST 0 (None)\n", 138 | " 16 RETURN_VALUE\n", 139 | "In [165]:\n", 140 | "# Catch Building of Classes\n", 141 | "\n", 142 | "class Base:\n", 143 | " def foo(self):\n", 144 | " return self.bar()\n", 145 | "\n", 146 | "old_bc = __build_class__\n", 147 | "def my_bc(*a, **kw):\n", 148 | " print('my buildclass ->', a, kw)\n", 149 | " return old_bc(*a, **kw)\n", 150 | "import builtins\n", 151 | "builtins.__build_class__ = my_bc\n", 152 | "In [1]:\n", 153 | "# Catch Building of Classes\n", 154 | "\n", 155 | "class Base:\n", 156 | " def foo(self):\n", 157 | " return self.bar()\n", 158 | "\n", 159 | "old_bc = __build_class__\n", 160 | "def my_bc(fun, name, base=None, **kw):\n", 161 | " if base is Base:\n", 162 | " print('Check if bar method defined')\n", 163 | " if base is not None:\n", 164 | " return old_bc(fun, name, base, **kw)\n", 165 | " return old_bc(fun, name, **kw)\n", 166 | "\n", 167 | "import builtins\n", 168 | "builtins.__build_class__ = my_bc\n", 169 | "In [80]:\n", 170 | "import builtins\n", 171 | "In [79]:\n", 172 | "import importlib\n", 173 | "importlib.reload(builtins)\n", 174 | "Out[79]:\n", 175 | "\n", 176 | "In [2]:\n", 177 | "class BaseMeta(type):\n", 178 | " def __new__(cls, name, bases, body):\n", 179 | " print('BaseMeta.__new__', cls, name, bases, body)\n", 180 | " return super().__new__(cls, name, bases, body)\n", 181 | "\n", 182 | "class Base(metaclass=BaseMeta):\n", 183 | " def foo(self):\n", 184 | " return self.bar()\n", 185 | "BaseMeta.__new__ Base () {'__module__': '__main__', '__qualname__': 'Base', 'foo': }\n", 186 | "In [167]:\n", 187 | "class BaseMeta(type):\n", 188 | " def __new__(cls, name, bases, body):\n", 189 | " if not 'bar' in body:\n", 190 | " raise TypeError('bad user class')\n", 191 | " return super().__new__(cls, name, bases, body)\n", 192 | "\n", 193 | "class Base(metaclass=BaseMeta):\n", 194 | " def foo(self):\n", 195 | " return self.bar()\n", 196 | "my buildclass -> (, 'BaseMeta', ) {}\n", 197 | "---------------------------------------------------------------------------\n", 198 | "RecursionError Traceback (most recent call last)\n", 199 | " in ()\n", 200 | "----> 1 class BaseMeta(type):\n", 201 | " 2 def __new__(cls, name, bases, body):\n", 202 | " 3 if not 'bar' in body:\n", 203 | " 4 raise TypeError('bad user class')\n", 204 | " 5 return super().__new__(cls, name, bases, body)\n", 205 | "\n", 206 | " in my_bc(*a, **kw)\n", 207 | " 8 def my_bc(*a, **kw):\n", 208 | " 9 print('my buildclass ->', a, kw)\n", 209 | "---> 10 return old_bc(*a, **kw)\n", 210 | " 11 import builtins\n", 211 | " 12 builtins.__build_class__ = my_bc\n", 212 | "\n", 213 | " in my_bc(fun, name, base, **kw)\n", 214 | " 10 print('Check if bar method defined')\n", 215 | " 11 if base is not None:\n", 216 | "---> 12 return old_bc(fun, name, base, **kw)\n", 217 | " 13 return old_bc(fun, name, **kw)\n", 218 | " 14 \n", 219 | "\n", 220 | "... last 1 frames repeated, from the frame below ...\n", 221 | "\n", 222 | " in my_bc(fun, name, base, **kw)\n", 223 | " 10 print('Check if bar method defined')\n", 224 | " 11 if base is not None:\n", 225 | "---> 12 return old_bc(fun, name, base, **kw)\n", 226 | " 13 return old_bc(fun, name, **kw)\n", 227 | " 14 \n", 228 | "\n", 229 | "RecursionError: maximum recursion depth exceeded\n", 230 | "In [5]:\n", 231 | "class BaseMeta(type):\n", 232 | " def __new__(cls, name, bases, body):\n", 233 | " if name != 'Base' and not 'bar' in body:\n", 234 | " raise TypeError('bad user class')\n", 235 | " return super().__new__(cls, name, bases, body)\n", 236 | "\n", 237 | "class Base(metaclass=BaseMeta):\n", 238 | " def foo(self):\n", 239 | " return self.bar()\n", 240 | " \n", 241 | " def __init_subclass__(*a, **kw):\n", 242 | " print('init_subclass', a, kw)\n", 243 | " return super().__init_subclass__(*a, **kw)\n", 244 | "In [8]:\n", 245 | "help(Base.__init_subclass__)\n", 246 | "Help on method __init_subclass__ in module __main__:\n", 247 | "\n", 248 | "__init_subclass__(*a, **kw) method of __main__.BaseMeta instance\n", 249 | " This method is called when a class is subclassed.\n", 250 | " \n", 251 | " The default implementation does nothing. It may be\n", 252 | " overridden to extend subclasses.\n", 253 | "\n", 254 | "Decorators\n", 255 | "In [12]:\n", 256 | "# dec.py\n", 257 | "In [77]:\n", 258 | "def add(x, y=10):\n", 259 | " return x + y\n", 260 | "In [14]:\n", 261 | "add(10, 20)\n", 262 | "Out[14]:\n", 263 | "30\n", 264 | "In [15]:\n", 265 | "add\n", 266 | "Out[15]:\n", 267 | "\n", 268 | "In [28]:\n", 269 | "# Name of function\n", 270 | "add.__name__\n", 271 | "Out[28]:\n", 272 | "'add'\n", 273 | "In [27]:\n", 274 | "# What module function is assigned to\n", 275 | "add.__module__\n", 276 | "Out[27]:\n", 277 | "'__main__'\n", 278 | "In [26]:\n", 279 | "# Default values\n", 280 | "add.__defaults__\n", 281 | "Out[26]:\n", 282 | "(10,)\n", 283 | "In [25]:\n", 284 | "# Byte code for function\n", 285 | "add.__code__.co_code\n", 286 | "Out[25]:\n", 287 | "b'|\\x00|\\x01\\x17\\x00S\\x00'\n", 288 | "In [24]:\n", 289 | "# Variable names function interacts with\n", 290 | "add.__code__.co_varnames\n", 291 | "Out[24]:\n", 292 | "('x', 'y')\n", 293 | "What's your source code?\n", 294 | "In [31]:\n", 295 | "from inspect import getsource\n", 296 | "In [32]:\n", 297 | "getsource(add)\n", 298 | "Out[32]:\n", 299 | "'def add(x, y=10):\\n return x + y\\n'\n", 300 | "In [33]:\n", 301 | "print(getsource(add))\n", 302 | "def add(x, y=10):\n", 303 | " return x + y\n", 304 | "\n", 305 | "In [35]:\n", 306 | "# What file are you in? \n", 307 | "from inspect import getfile\n", 308 | "getfile(add)\n", 309 | "Out[35]:\n", 310 | "''\n", 311 | "In [37]:\n", 312 | "from inspect import getmodule\n", 313 | "getmodule(add)\n", 314 | "Out[37]:\n", 315 | "\n", 316 | "In [38]:\n", 317 | "print('add(10)', add(10))\n", 318 | "print('add(20, 30)', add(20, 30))\n", 319 | "print('add(\"a\", \"b\")', add(\"a\", \"b\"))\n", 320 | "add(10) 20\n", 321 | "add(20, 30) 50\n", 322 | "add(\"a\", \"b\") ab\n", 323 | "In [40]:\n", 324 | "#Count how long it took to run\n", 325 | "In [41]:\n", 326 | "def add_timer(x, y=10):\n", 327 | " before = time()\n", 328 | " rv = x + y\n", 329 | " after = time()\n", 330 | " print('elapsed:', after - before)\n", 331 | " return rv\n", 332 | "In [42]:\n", 333 | "print('add(10)', add_timer(10))\n", 334 | "print('add(20, 30)', add_timer(20, 30))\n", 335 | "print('add(\"a\", \"b\")', add_timer(\"a\", \"b\"))\n", 336 | "elapsed: 0.0\n", 337 | "add(10) 20\n", 338 | "elapsed: 9.5367431640625e-07\n", 339 | "add(20, 30) 50\n", 340 | "elapsed: 9.5367431640625e-07\n", 341 | "add(\"a\", \"b\") ab\n", 342 | "But what if we have multiple functions that require timing?\n", 343 | "In [49]:\n", 344 | "def sub(x, y=10):\n", 345 | " return x - y\n", 346 | "In [46]:\n", 347 | "print('sub(10)', sub(10))\n", 348 | "print('sub(20, 30)', sub(20, 30))\n", 349 | "sub(10) 0\n", 350 | "sub(20, 30) -10\n", 351 | "In [55]:\n", 352 | "def timer(func, x, y=10):\n", 353 | " before = time()\n", 354 | " rv = func(x, y)\n", 355 | " after = time()\n", 356 | " print('elapsed', after - before)\n", 357 | " return rv\n", 358 | "In [56]:\n", 359 | "print('add(10)', timer(add, 10))\n", 360 | "print('add(20, 30)', timer(add, 20, 30))\n", 361 | "print('add(\"a\", \"b\")', timer(add, \"a\", \"b\"))\n", 362 | "elapsed 9.5367431640625e-07\n", 363 | "add(10) 20\n", 364 | "elapsed 9.5367431640625e-07\n", 365 | "add(20, 30) 50\n", 366 | "elapsed 9.5367431640625e-07\n", 367 | "add(\"a\", \"b\") ab\n", 368 | "In [74]:\n", 369 | "def timer(func):\n", 370 | " def f(x, y=10):\n", 371 | " before = time()\n", 372 | " rv = func(x, y)\n", 373 | " after = time()\n", 374 | " print('elapsed', after - before)\n", 375 | " return rv\n", 376 | " return f\n", 377 | "In [78]:\n", 378 | "add = timer(add)\n", 379 | "In [79]:\n", 380 | "print('add(10)', add(10))\n", 381 | "print('add(20, 30)', add(20, 30))\n", 382 | "print('add(\"a\", \"b\")', add(\"a\", \"b\"))\n", 383 | "elapsed 9.5367431640625e-07\n", 384 | "add(10) 20\n", 385 | "elapsed 9.5367431640625e-07\n", 386 | "add(20, 30) 50\n", 387 | "elapsed 9.5367431640625e-07\n", 388 | "add(\"a\", \"b\") ab\n", 389 | "In [80]:\n", 390 | "# Don't need to do add = timer(add) with decorators...\n", 391 | "In [87]:\n", 392 | "@timer\n", 393 | "def add_dec(x, y=10):\n", 394 | " return x + y\n", 395 | "\n", 396 | "@timer\n", 397 | "def sub_dec(x, y=10):\n", 398 | " return x - y\n", 399 | "In [88]:\n", 400 | "print('add(10)', add_dec(10))\n", 401 | "print('add(20, 30)', add_dec(20, 30))\n", 402 | "print('add(\"a\", \"b\")', add_dec(\"a\", \"b\"))\n", 403 | "print('sub(10)', sub_dec(10))\n", 404 | "print('sub(20, 30)', sub_dec(20, 30))\n", 405 | "elapsed 9.5367431640625e-07\n", 406 | "add(10) 20\n", 407 | "elapsed 1.1920928955078125e-06\n", 408 | "add(20, 30) 50\n", 409 | "elapsed 1.1920928955078125e-06\n", 410 | "add(\"a\", \"b\") ab\n", 411 | "elapsed 9.5367431640625e-07\n", 412 | "sub(10) 0\n", 413 | "elapsed 0.0\n", 414 | "sub(20, 30) -10\n", 415 | "In [91]:\n", 416 | "# Don't hardcode parameters in decorator functions\n", 417 | "def timer_k(func):\n", 418 | " def f(*args, **kwargs):\n", 419 | " before = time()\n", 420 | " rv = func(*args, **kwargs)\n", 421 | " after = time()\n", 422 | " print('elapsed', after - before)\n", 423 | " return rv\n", 424 | " return f\n", 425 | "In [92]:\n", 426 | "@timer_k\n", 427 | "def add_dec(x, y=10):\n", 428 | " return x + y\n", 429 | "\n", 430 | "@timer_k\n", 431 | "def sub_dec(x, y=10):\n", 432 | " return x - y\n", 433 | "\n", 434 | "print('add(10)', add_dec(10))\n", 435 | "print('add(20, 30)', add_dec(20, 30))\n", 436 | "print('add(\"a\", \"b\")', add_dec(\"a\", \"b\"))\n", 437 | "print('sub(10)', sub_dec(10))\n", 438 | "print('sub(20, 30)', sub_dec(20, 30))\n", 439 | "elapsed 9.5367431640625e-07\n", 440 | "add(10) 20\n", 441 | "elapsed 9.5367431640625e-07\n", 442 | "add(20, 30) 50\n", 443 | "elapsed 9.5367431640625e-07\n", 444 | "add(\"a\", \"b\") ab\n", 445 | "elapsed 0.0\n", 446 | "sub(10) 0\n", 447 | "elapsed 9.5367431640625e-07\n", 448 | "sub(20, 30) -10\n", 449 | "In [93]:\n", 450 | "# What if I want to run a function n number of times\n", 451 | "In [94]:\n", 452 | "# Let's have add run 3 times in a row and sub run twice in a row\n", 453 | "In [97]:\n", 454 | "n = 2\n", 455 | "\n", 456 | "def ntimes(f):\n", 457 | " def wrapper(*args, **kwargs):\n", 458 | " for _ in range(n):\n", 459 | " print('running {.__name__}'.format(f))\n", 460 | " rv = f(*args, **kwargs)\n", 461 | " return rv\n", 462 | " return wrapper\n", 463 | " \n", 464 | " \n", 465 | "@ntimes\n", 466 | "def add_dec(x, y=10):\n", 467 | " return x + y\n", 468 | "\n", 469 | "@ntimes\n", 470 | "def sub_dec(x, y=10):\n", 471 | " return x - y\n", 472 | "In [98]:\n", 473 | "print('add(10)', add_dec(10))\n", 474 | "print('add(20, 30)', add_dec(20, 30))\n", 475 | "print('add(\"a\", \"b\")', add_dec(\"a\", \"b\"))\n", 476 | "print('sub(10)', sub_dec(10))\n", 477 | "print('sub(20, 30)', sub_dec(20, 30))\n", 478 | "running add_dec\n", 479 | "running add_dec\n", 480 | "add(10) 20\n", 481 | "running add_dec\n", 482 | "running add_dec\n", 483 | "add(20, 30) 50\n", 484 | "running add_dec\n", 485 | "running add_dec\n", 486 | "add(\"a\", \"b\") ab\n", 487 | "running sub_dec\n", 488 | "running sub_dec\n", 489 | "sub(10) 0\n", 490 | "running sub_dec\n", 491 | "running sub_dec\n", 492 | "sub(20, 30) -10\n", 493 | "Higher Order Decorators\n", 494 | "In [103]:\n", 495 | "def ntimes(n):\n", 496 | " def inner(f):\n", 497 | " def wrapper(*args, **kwargs):\n", 498 | " for _ in range(n):\n", 499 | " print('running {.__name__}'.format(f))\n", 500 | " rv = f(*args, **kwargs)\n", 501 | " return rv\n", 502 | " return wrapper\n", 503 | " return inner\n", 504 | " \n", 505 | " \n", 506 | "@ntimes(2)\n", 507 | "def add_hdec(x, y=10):\n", 508 | " return x + y\n", 509 | "\n", 510 | "@ntimes(4)\n", 511 | "def sub_hdec(x, y=10):\n", 512 | " return x - y\n", 513 | "In [104]:\n", 514 | "print('add(10)', add_hdec(10))\n", 515 | "print('add(20, 30)', add_hdec(20, 30))\n", 516 | "print('add(\"a\", \"b\")', add_hdec(\"a\", \"b\"))\n", 517 | "print('sub(10)', sub_hdec(10))\n", 518 | "print('sub(20, 30)', sub_hdec(20, 30))\n", 519 | "running add_hdec\n", 520 | "running add_hdec\n", 521 | "add(10) 20\n", 522 | "running add_hdec\n", 523 | "running add_hdec\n", 524 | "add(20, 30) 50\n", 525 | "running add_hdec\n", 526 | "running add_hdec\n", 527 | "add(\"a\", \"b\") ab\n", 528 | "running sub_hdec\n", 529 | "running sub_hdec\n", 530 | "running sub_hdec\n", 531 | "running sub_hdec\n", 532 | "sub(10) 0\n", 533 | "running sub_hdec\n", 534 | "running sub_hdec\n", 535 | "running sub_hdec\n", 536 | "running sub_hdec\n", 537 | "sub(20, 30) -10\n", 538 | "Generators\n", 539 | "In [105]:\n", 540 | "# gen.py - use whenever sequencing is needd\n", 541 | "In [109]:\n", 542 | "# top-level syntax, function -> underscore method\n", 543 | "# x() __call__\n", 544 | "\n", 545 | "def add1(x, y):\n", 546 | " return x + y\n", 547 | "\n", 548 | "class Adder:\n", 549 | " def __call__(self, x, y):\n", 550 | " return x + y\n", 551 | "add2 = Adder()\n", 552 | "In [110]:\n", 553 | "add1(10, 20)\n", 554 | "Out[110]:\n", 555 | "30\n", 556 | "In [111]:\n", 557 | "add2(10, 20)\n", 558 | "Out[111]:\n", 559 | "30\n", 560 | "In [113]:\n", 561 | "# top-level syntax, function -> underscore method\n", 562 | "# x() __call__\n", 563 | "\n", 564 | "def add1(x, y):\n", 565 | " return x + y\n", 566 | "\n", 567 | "class Adder:\n", 568 | " def __init__(self):\n", 569 | " self.z = 0\n", 570 | " \n", 571 | " def __call__(self, x, y):\n", 572 | " self.z += 1\n", 573 | " return x + y + self.z\n", 574 | "\n", 575 | "add2 = Adder()\n", 576 | "In [130]:\n", 577 | "from time import sleep\n", 578 | "# This example has storage... and has eager return of the result sets\n", 579 | "def compute():\n", 580 | " rv = []\n", 581 | " for i in range(10):\n", 582 | " sleep(.5)\n", 583 | " rv.append(i)\n", 584 | " return rv\n", 585 | "In [120]:\n", 586 | "compute()\n", 587 | "Out[120]:\n", 588 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 589 | "Wasteful because we have to wait for the entire action to complete and be read into memory, when we really just care about each number (one by one)\n", 590 | "\n", 591 | "In [129]:\n", 592 | "class Compute:\n", 593 | " def __call__(self):\n", 594 | " rv = []\n", 595 | " for i in range(100000):\n", 596 | " sleep(5)\n", 597 | " rv.append(i)\n", 598 | " return rv\n", 599 | " \n", 600 | " def __iter__(self):\n", 601 | " self.last = 0\n", 602 | " return self\n", 603 | " \n", 604 | " def __next__(self):\n", 605 | " rv = self.last\n", 606 | " self.last += 1\n", 607 | " if self.last > 10:\n", 608 | " raise StopIteration()\n", 609 | " sleep(.5)\n", 610 | " return self.last\n", 611 | " \n", 612 | "compute = Compute()\n", 613 | "\n", 614 | "# THIS IS UGLY... now let's make a generator\n", 615 | "In [131]:\n", 616 | "#This is a generator... don't eagerly compute. Return to user as they ask for it...\n", 617 | "\n", 618 | "def compute():\n", 619 | " for i in range(10):\n", 620 | " sleep(.5)\n", 621 | " yield i\n", 622 | "In [125]:\n", 623 | "# for x in xs:\n", 624 | "# pass\n", 625 | "\n", 626 | "# xi = iter(xs) -> __iter__\n", 627 | "# while True:\n", 628 | "# x = next(xi) -> __next__\n", 629 | "In [136]:\n", 630 | "for val in compute():\n", 631 | " print(val)\n", 632 | "0\n", 633 | "1\n", 634 | "2\n", 635 | "3\n", 636 | "4\n", 637 | "5\n", 638 | "6\n", 639 | "7\n", 640 | "8\n", 641 | "9\n", 642 | "In [133]:\n", 643 | "class Api:\n", 644 | " def run_this_first(self):\n", 645 | " first()\n", 646 | " def run_this_second(self):\n", 647 | " second()\n", 648 | " def run_this_last(self):\n", 649 | " last()\n", 650 | "In [137]:\n", 651 | "def api():\n", 652 | " first()\n", 653 | " yield\n", 654 | " second()\n", 655 | " yield\n", 656 | " last()\n", 657 | "Context Manager\n", 658 | "In [2]:\n", 659 | "# cty.py\n", 660 | "\n", 661 | "from sqlite3 import connect\n", 662 | "In [156]:\n", 663 | "# with ctx() as x:\n", 664 | "# pass\n", 665 | "\n", 666 | "# x = ctx().__enter__\n", 667 | "# try:\n", 668 | "# pass\n", 669 | "# finally:\n", 670 | "# x.__exit__\n", 671 | "\n", 672 | "class temptable:\n", 673 | " def __init__(self, cur):\n", 674 | " self.cur = cur\n", 675 | " def __enter__(self):\n", 676 | " print('__enter__')\n", 677 | " self.cur.execute('create table points(x int, y int)')\n", 678 | " def __exit__(self, *args):\n", 679 | " print('__exit__')\n", 680 | " self.cur.execute('drop table points')\n", 681 | "\n", 682 | "with connect('test.db') as conn:\n", 683 | " cur = conn.cursor()\n", 684 | " with temptable(cur):\n", 685 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 686 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 687 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 688 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 689 | " for row in cur.execute(\"select x, y from points\"):\n", 690 | " print(row)\n", 691 | " for row in cur.execute('select sum(x * y) from points'):\n", 692 | " print(row)\n", 693 | "__enter__\n", 694 | "(1, 1)\n", 695 | "(1, 2)\n", 696 | "(2, 1)\n", 697 | "(2, 2)\n", 698 | "(9,)\n", 699 | "__exit__\n", 700 | "In [162]:\n", 701 | "rm test.db\n", 702 | "In [164]:\n", 703 | "def temptable(cur):\n", 704 | " cur.execute('create table points(x int, y int)')\n", 705 | " print('created table')\n", 706 | " yield\n", 707 | " cur.execute('drop table points')\n", 708 | " print('dropped table')\n", 709 | " \n", 710 | "class contextmanager:\n", 711 | " def __init__(self, cur):\n", 712 | " self.cur = cur\n", 713 | " def __enter__(self):\n", 714 | " self.gen = temptable(self.cur)\n", 715 | " next(self.gen)\n", 716 | " def __exit__(self, *args):\n", 717 | " next(self.gen, None)\n", 718 | " \n", 719 | "with connect('test.db') as conn:\n", 720 | " cur = conn.cursor()\n", 721 | " with contextmanager(cur):\n", 722 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 723 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 724 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 725 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 726 | " for row in cur.execute(\"select x, y from points\"):\n", 727 | " print(row)\n", 728 | " for row in cur.execute('select sum(x * y) from points'):\n", 729 | " print(row)\n", 730 | "created table\n", 731 | "(1, 1)\n", 732 | "(1, 2)\n", 733 | "(2, 1)\n", 734 | "(2, 2)\n", 735 | "(9,)\n", 736 | "dropped table\n", 737 | "In [8]:\n", 738 | "class contextmanager:\n", 739 | " def __init__(self, gen):\n", 740 | " self.gen = gen\n", 741 | " def __call__(self, *args, **kwargs):\n", 742 | " self.args, self.kwargs = args, kwargs\n", 743 | " return self\n", 744 | " def __enter__(self):\n", 745 | " self.gen_inst = self.gen(*self.args, **self.kwargs)\n", 746 | " next(self.gen_inst)\n", 747 | " def __exit__(self, *args):\n", 748 | " next(self.gen_inst, None)\n", 749 | "\n", 750 | "def temptable(cur):\n", 751 | " cur.execute('create table points(x int, y int)')\n", 752 | " print('created table')\n", 753 | " yield\n", 754 | " cur.execute('drop table points')\n", 755 | " print('dropped table')\n", 756 | "temptable = contextmanager(temptable)\n", 757 | " \n", 758 | "with connect('test.db') as conn:\n", 759 | " cur = conn.cursor()\n", 760 | " with temptable(cur):\n", 761 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 762 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 763 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 764 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 765 | " for row in cur.execute(\"select x, y from points\"):\n", 766 | " print(row)\n", 767 | "created table\n", 768 | "(1, 1)\n", 769 | "(1, 2)\n", 770 | "(2, 1)\n", 771 | "(2, 2)\n", 772 | "dropped table\n", 773 | "In [12]:\n", 774 | "class contextmanager:\n", 775 | " def __init__(self, gen):\n", 776 | " self.gen = gen\n", 777 | " def __call__(self, *args, **kwargs):\n", 778 | " self.args, self.kwargs = args, kwargs\n", 779 | " return self\n", 780 | " def __enter__(self):\n", 781 | " self.gen_inst = self.gen(*self.args, **self.kwargs)\n", 782 | " next(self.gen_inst)\n", 783 | " def __exit__(self, *args):\n", 784 | " next(self.gen_inst, None)\n", 785 | " \n", 786 | "@contextmanager\n", 787 | "def temptable(cur):\n", 788 | " cur.execute('create table points(x int, y int)')\n", 789 | " print('created table')\n", 790 | " yield\n", 791 | " cur.execute('drop table points')\n", 792 | " print('dropped table')\n", 793 | " \n", 794 | "with connect('test.db') as conn:\n", 795 | " cur = conn.cursor()\n", 796 | " with temptable(cur):\n", 797 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 798 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 799 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 800 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 801 | " for row in cur.execute(\"select x, y from points\"):\n", 802 | " print(row)\n", 803 | "created table\n", 804 | "(1, 1)\n", 805 | "(1, 2)\n", 806 | "(2, 1)\n", 807 | "(2, 2)\n", 808 | "dropped table\n", 809 | "In [15]:\n", 810 | "from sqlite3 import connect\n", 811 | "from contextlib import contextmanager\n", 812 | " \n", 813 | "@contextmanager\n", 814 | "def temptable(cur):\n", 815 | " cur.execute('create table points(x int, y int)')\n", 816 | " print('created table')\n", 817 | " try:\n", 818 | " yield\n", 819 | " finally:\n", 820 | " cur.execute('drop table points')\n", 821 | " print('dropped table')\n", 822 | " \n", 823 | "with connect('test.db') as conn:\n", 824 | " cur = conn.cursor()\n", 825 | " with temptable(cur):\n", 826 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 827 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 828 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 829 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 830 | " for row in cur.execute(\"select x, y from points\"):\n", 831 | " print(row)\n", 832 | "created table\n", 833 | "(1, 1)\n", 834 | "(1, 2)\n", 835 | "(2, 1)\n", 836 | "(2, 2)\n", 837 | "dropped table" 838 | ] 839 | }, 840 | { 841 | "cell_type": "code", 842 | "execution_count": null, 843 | "metadata": {}, 844 | "outputs": [], 845 | "source": [] 846 | } 847 | ], 848 | "metadata": { 849 | "kernelspec": { 850 | "display_name": "Python 2", 851 | "language": "python", 852 | "name": "python2" 853 | }, 854 | "language_info": { 855 | "codemirror_mode": { 856 | "name": "ipython", 857 | "version": 2 858 | }, 859 | "file_extension": ".py", 860 | "mimetype": "text/x-python", 861 | "name": "python", 862 | "nbconvert_exporter": "python", 863 | "pygments_lexer": "ipython2", 864 | "version": "2.7.15" 865 | } 866 | }, 867 | "nbformat": 4, 868 | "nbformat_minor": 2 869 | } 870 | -------------------------------------------------------------------------------- /Boston house price prediction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 2", 14 | "language": "python", 15 | "name": "python2" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 2 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython2", 27 | "version": "2.7.15" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 2 32 | } 33 | -------------------------------------------------------------------------------- /IRIS data analysis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 9, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sklearn\n", 10 | "from sklearn import tree" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 10, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "features = [[140,1],[130,1],[150,0],[170,0]]\n", 20 | "labels = [0,0,1,1]" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 11, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "clf = tree.DecisionTreeClassifier()\n", 30 | "clf = clf.fit(features,labels)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 12, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "[1]\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "print clf.predict([[150,0]])" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 19, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "[0]\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "print clf.predict([[140,0]])" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "# IRIS DATASET" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 17, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "from sklearn.datasets import load_iris\n", 81 | "\n", 82 | "iris = load_iris()" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 18, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | "['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n" 95 | ] 96 | } 97 | ], 98 | "source": [ 99 | "print iris.feature_names" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 19, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "sklearn.utils.Bunch" 111 | ] 112 | }, 113 | "execution_count": 19, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | } 117 | ], 118 | "source": [ 119 | "type(iris)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 20, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "print iris.feature_names" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 34, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "[[5.1 3.5 1.4 0.2]\n", 149 | " [4.9 3. 1.4 0.2]\n", 150 | " [4.7 3.2 1.3 0.2]\n", 151 | " [4.6 3.1 1.5 0.2]\n", 152 | " [5. 3.6 1.4 0.2]]\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "print iris.data[0:5]" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 24, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "0\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "print iris.target[0]" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 38, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 187 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 188 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 189 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 190 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 191 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 192 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 193 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 194 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 195 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 196 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 197 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 198 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 199 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 200 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 201 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 202 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 203 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 204 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 205 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 206 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 207 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 208 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 209 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 210 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 211 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 212 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 213 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 214 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 215 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 216 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 217 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 218 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 219 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 220 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 221 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 222 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 223 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 224 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 225 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 226 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 227 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 228 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 229 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 230 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 231 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 232 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 233 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 234 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 235 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 236 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 237 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 238 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 239 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 240 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 241 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 242 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 243 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 244 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 245 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 246 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 247 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 248 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 249 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 250 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 251 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 252 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 253 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 254 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 255 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 256 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 257 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 258 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 259 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 260 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 261 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 262 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 263 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 264 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 265 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 266 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 267 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 268 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 269 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 270 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 271 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 272 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 273 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 274 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 275 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 276 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 277 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 278 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 279 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 280 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 281 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 282 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 283 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 284 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 285 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 286 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 287 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 288 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 289 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 290 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 291 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 292 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 293 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 294 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 295 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 296 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 297 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 298 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 299 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 300 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 301 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 302 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 303 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 304 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 305 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 306 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 307 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 308 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 309 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 310 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 311 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 312 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 313 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 314 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 315 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 316 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 317 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 318 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 319 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 320 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 321 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 322 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 323 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 324 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 325 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 326 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 327 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 328 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 329 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 330 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 331 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 332 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 333 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 334 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n", 335 | "([0, 0, 1, 1], [[140, 1], [130, 1], [150, 0], [170, 0]])\n" 336 | ] 337 | } 338 | ], 339 | "source": [ 340 | "for i in range(len(iris.target)):\n", 341 | " print(labels,features)" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 41, 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "name": "stdout", 351 | "output_type": "stream", 352 | "text": [ 353 | "[[140, 1], [130, 1], [150, 0], [170, 0]]\n", 354 | "[0, 0, 1, 1]\n" 355 | ] 356 | } 357 | ], 358 | "source": [ 359 | "print(features)\n", 360 | "print(labels)" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 26, 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "name": "stdout", 370 | "output_type": "stream", 371 | "text": [ 372 | "['setosa' 'versicolor' 'virginica']\n" 373 | ] 374 | } 375 | ], 376 | "source": [ 377 | "print iris.target_names" 378 | ] 379 | }, 380 | { 381 | "cell_type": "markdown", 382 | "metadata": {}, 383 | "source": [ 384 | "MORE!" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 42, 390 | "metadata": {}, 391 | "outputs": [], 392 | "source": [ 393 | "from sklearn import datasets" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 43, 399 | "metadata": {}, 400 | "outputs": [], 401 | "source": [ 402 | "iris = datasets.load_iris()" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 44, 408 | "metadata": {}, 409 | "outputs": [], 410 | "source": [ 411 | "x = iris.data\n", 412 | "y = iris.target" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 49, 418 | "metadata": {}, 419 | "outputs": [], 420 | "source": [ 421 | "from sklearn.cross_validation import train_test_split" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": 50, 427 | "metadata": {}, 428 | "outputs": [], 429 | "source": [ 430 | "x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=.5)" 431 | ] 432 | }, 433 | { 434 | "cell_type": "markdown", 435 | "metadata": {}, 436 | "source": [ 437 | "# using decision tree classifier" 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": 51, 443 | "metadata": {}, 444 | "outputs": [], 445 | "source": [ 446 | "from sklearn import tree" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 52, 452 | "metadata": {}, 453 | "outputs": [], 454 | "source": [ 455 | "my_classifier = tree.DecisionTreeClassifier()" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 53, 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "data": { 465 | "text/plain": [ 466 | "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n", 467 | " max_features=None, max_leaf_nodes=None,\n", 468 | " min_impurity_decrease=0.0, min_impurity_split=None,\n", 469 | " min_samples_leaf=1, min_samples_split=2,\n", 470 | " min_weight_fraction_leaf=0.0, presort=False, random_state=None,\n", 471 | " splitter='best')" 472 | ] 473 | }, 474 | "execution_count": 53, 475 | "metadata": {}, 476 | "output_type": "execute_result" 477 | } 478 | ], 479 | "source": [ 480 | "my_classifier.fit(x_train,y_train)" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 54, 486 | "metadata": {}, 487 | "outputs": [], 488 | "source": [ 489 | "predictions = my_classifier.predict(x_test)" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 55, 495 | "metadata": {}, 496 | "outputs": [ 497 | { 498 | "name": "stdout", 499 | "output_type": "stream", 500 | "text": [ 501 | "[1 1 1 2 2 2 0 2 2 1 2 2 2 1 1 1 1 1 2 0 2 0 1 0 2 2 2 0 0 0 0 0 0 0 1 1 1\n", 502 | " 1 2 0 1 2 0 0 0 2 0 0 2 2 1 0 2 2 0 2 2 0 2 0 1 1 2 0 1 1 0 2 2 1 1 1 0 1\n", 503 | " 0]\n" 504 | ] 505 | } 506 | ], 507 | "source": [ 508 | "print predictions" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 56, 514 | "metadata": {}, 515 | "outputs": [], 516 | "source": [ 517 | "from sklearn.metrics import accuracy_score" 518 | ] 519 | }, 520 | { 521 | "cell_type": "code", 522 | "execution_count": 60, 523 | "metadata": {}, 524 | "outputs": [ 525 | { 526 | "name": "stdout", 527 | "output_type": "stream", 528 | "text": [ 529 | "97.33333333333334\n" 530 | ] 531 | } 532 | ], 533 | "source": [ 534 | "print accuracy_score(y_test,predictions)*100" 535 | ] 536 | }, 537 | { 538 | "cell_type": "markdown", 539 | "metadata": {}, 540 | "source": [ 541 | "# using k neighbours" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 61, 547 | "metadata": {}, 548 | "outputs": [], 549 | "source": [ 550 | "from sklearn.neighbors import KNeighborsClassifier" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 62, 556 | "metadata": {}, 557 | "outputs": [], 558 | "source": [ 559 | "new_classifier = KNeighborsClassifier()" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 63, 565 | "metadata": {}, 566 | "outputs": [ 567 | { 568 | "data": { 569 | "text/plain": [ 570 | "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n", 571 | " metric_params=None, n_jobs=1, n_neighbors=5, p=2,\n", 572 | " weights='uniform')" 573 | ] 574 | }, 575 | "execution_count": 63, 576 | "metadata": {}, 577 | "output_type": "execute_result" 578 | } 579 | ], 580 | "source": [ 581 | "new_classifier.fit(x_train,y_train)" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 64, 587 | "metadata": {}, 588 | "outputs": [], 589 | "source": [ 590 | "predictions2 = new_classifier.predict(x_test)" 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": 65, 596 | "metadata": {}, 597 | "outputs": [ 598 | { 599 | "name": "stdout", 600 | "output_type": "stream", 601 | "text": [ 602 | "[1 1 1 2 2 2 0 2 2 1 2 2 2 1 1 1 1 1 2 0 2 0 1 0 2 2 2 0 0 0 0 0 0 0 1 1 1\n", 603 | " 1 2 0 1 2 0 0 0 2 0 0 2 2 1 0 2 2 0 2 2 0 2 0 1 1 2 0 1 1 0 2 2 1 1 1 0 1\n", 604 | " 0]\n" 605 | ] 606 | } 607 | ], 608 | "source": [ 609 | "print predictions" 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "execution_count": 66, 615 | "metadata": {}, 616 | "outputs": [], 617 | "source": [ 618 | "from sklearn.metrics import accuracy_score" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 67, 624 | "metadata": {}, 625 | "outputs": [ 626 | { 627 | "name": "stdout", 628 | "output_type": "stream", 629 | "text": [ 630 | "98.66666666666667\n" 631 | ] 632 | } 633 | ], 634 | "source": [ 635 | "print accuracy_score(y_test,predictions2)*100" 636 | ] 637 | }, 638 | { 639 | "cell_type": "markdown", 640 | "metadata": {}, 641 | "source": [ 642 | "# k nearest neighbours" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 68, 648 | "metadata": {}, 649 | "outputs": [ 650 | { 651 | "ename": "SyntaxError", 652 | "evalue": "unexpected EOF while parsing (, line 1)", 653 | "output_type": "error", 654 | "traceback": [ 655 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m class ScrappyKNN():\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n" 656 | ] 657 | } 658 | ], 659 | "source": [ 660 | "class ScrappyKNN():" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": null, 666 | "metadata": {}, 667 | "outputs": [], 668 | "source": [] 669 | } 670 | ], 671 | "metadata": { 672 | "kernelspec": { 673 | "display_name": "Python 2", 674 | "language": "python", 675 | "name": "python2" 676 | }, 677 | "language_info": { 678 | "codemirror_mode": { 679 | "name": "ipython", 680 | "version": 2 681 | }, 682 | "file_extension": ".py", 683 | "mimetype": "text/x-python", 684 | "name": "python", 685 | "nbconvert_exporter": "python", 686 | "pygments_lexer": "ipython2", 687 | "version": "2.7.15" 688 | } 689 | }, 690 | "nbformat": 4, 691 | "nbformat_minor": 2 692 | } 693 | -------------------------------------------------------------------------------- /Important details: -------------------------------------------------------------------------------- 1 | https://www.manning.com/livevideo/deep-learning-crash-courseThe following is a list of free or paid online courses on machine learning, statistics, data-mining, etc. 2 | 3 | ## Machine-Learning / Data Mining 4 | 5 | * [Artificial Intelligence (Columbia University)](https://www.edx.org/course/artificial-intelligence-ai-columbiax-csmm-101x) - free 6 | * [Machine Learning (Columbia University)](https://www.edx.org/course/machine-learning-columbiax-csmm-102x) - free 7 | * [Machine Learning (Stanford University)](https://www.coursera.org/learn/machine-learning) - free 8 | * [Neural Networks for Machine Learning (University of Toronto)](https://www.coursera.org/learn/neural-networks) - free. Also [available on YouTube](https://www.youtube.com/watch?v=cbeTc-Urqak&list=PLYvFQm7QY5Fy28dST8-qqzJjXr83NKWAr) as a playlist. 9 | * [Deep Learning Specialization (by Andrew Ng, deeplearning.ai)](https://www.coursera.org/specializations/deep-learning) - Courses: I Neural Networks and Deep Learning; II Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization; III Structuring Machine Learning Projects; IV Convolutional Neural Networks; V Sequence Models; Paid for grading/certification, financial aid available, free to audit 10 | Deep Learning Nano Degree on Udacity (https://www.udacity.com/course/deep-learning-nanodegree--nd101) - $ 11 | Intro to Deep Learning (MIT) (http://introtodeeplearning.com/) 12 | * [Stanford's CS20 Tensorflow for Deep Learning Research] (http://web.stanford.edu/class/cs20si/) 13 | fast.ai - deep learning MOOC (http://www.fast.ai/) 14 | * [Machine Learning Specialization (University of Washington)](https://www.coursera.org/specializations/machine-learning) - Courses: Machine Learning Foundations: A Case Study Approach, Machine Learning: Regression, Machine Learning: Classification, Machine Learning: Clustering & Retrieval, Machine Learning: Recommender Systems & Dimensionality Reduction,Machine Learning Capstone: An Intelligent Application with Deep Learning; free 15 | * [Machine Learning Course (2014-15 session) (by Nando de Freitas, University of Oxford)](https://www.cs.ox.ac.uk/people/nando.defreitas/machinelearning/) - Lecture slides and video recordings. 16 | * [Learning from Data (by Yaser S. Abu-Mostafa, Caltech)](http://www.work.caltech.edu/telecourse.html) - Lecture videos available 17 | * [Intro to Machine Learning](https://www.udacity.com/course/intro-to-machine-learning--ud120) - free 18 | * [Probabilistic Graphical Models (by Prof. Daphne Koller, Stanford)](https://www.coursera.org/specializations/probabilistic-graphical-models) Coursera Specialization or [this Youtube playlist](https://www.youtube.com/watch?v=WPSQfOkb1M8&list=PL50E6E80E8525B59C) if you can't afford the enrollment fee. 19 | * [Reinforcement Learning Course (by David Silver, DeepMind)](https://www.youtube.com/watch?v=2pWv7GOvuf0&list=PLzuuYNsE1EZAXYR4FJ75jcJseBmo4KQ9-) - YouTube playlist and [lecture slides](http://www0.cs.ucl.ac.uk/staff/d.silver/web/Teaching.html). 20 | * [Keras in Motion](https://www.manning.com/livevideo/keras-in-motion) $ 21 | * [Stanford's CS231n: CNNs for Visual Recognition](https://www.youtube.com/watch?v=vT1JzLTH4G4&index=1&list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv) - Spring 2017 iteration, instructors (Fei-Fei Li, Justin Johnson, Serena Yeung) , or [Winter 2016 edition](https://www.youtube.com/watch?v=NfnWJUyUJYU&list=PLkt2uSq6rBVctENoVBg1TpCC7OQi31AlC) instructors (Fei-Fei Li, Andrej Karpathy, Justin Johnson). [Course website](http://cs231n.github.io/) has supporting material. 22 | * [University of California, Berkeley's CS294: Deep Reinforcement Learning](https://www.youtube.com/watch?v=8jQIKgTzQd4&list=PLkFD6_40KJIwTmSbCv9OVJB3YaO4sFwkX) - Fall 2017 edition. [Course website](http://rll.berkeley.edu/deeprlcourse/) has lecture slides and other related material. 23 | * [Machine Learning (Georgia Tech) on Udacity](https://www.udacity.com/course/machine-learning--ud262) - free 24 | * [Reinforcement Learning (Georgia Tech) on Udacity ](https://www.udacity.com/course/reinforcement-learning--ud600) - free 25 | * [Machine Learning for Trading](https://www.udacity.com/course/machine-learning-for-trading--ud501) - free 26 | * [Mining of Massive Datasets](https://www.youtube.com/watch?v=xoA5v9AO7S0&list=PLLssT5z_DsK9JDLcT8T62VtzwyW9LNepV) (YouTube playlist)- Course [website](http://mmds.org/) has info about accompanying book, free chapters, and Stanford's [MOOC](https://lagunita.stanford.edu/courses/course-v1:ComputerScience+MMDS+SelfPaced/about) 27 | * [Machine Learning Crash Course (Google)](https://developers.google.com/machine-learning/crash-course/) - free 28 | * [Machine Learning Mini Bootcamp Course (LambdaSchool)](https://lambdaschool.com/free-course-machine-learning/) - free and $ 29 | * [Microsoft Professional Program for Artificial Intelligence](https://academy.microsoft.com/en-us/professional-program/tracks/artificial-intelligence/) - free 30 | * [Open Machine Learning Course](https://github.com/Yorko/mlcourse_open) with [articles](https://medium.com/open-machine-learning-course) on Medium 31 | * [Machine Learning A-Z (Udemy)](https://www.udemy.com/machinelearning/) - Hands-On Python & R In Data Science 32 | * [Deep Learning Crash Course](https://www.manning.com/livevideo/deep-learning-crash-course) - $ 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Nirmal Silwal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Machine-Learning 2 | hello 3 | i will be adding various python implementation of Machine Learning algorithms 4 | -------------------------------------------------------------------------------- /SUV cars data analysis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# SUV predictions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 32, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "import pandas as pd\n", 18 | "import seaborn as sns\n", 19 | "import matplotlib.pyplot as plt\n", 20 | "%matplotlib inline" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 33, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "dataset = pd.read_csv('SUV_cars_data.csv')" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 34, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/html": [ 40 | "
\n", 41 | "\n", 54 | "\n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | "
User IDGenderAgeEstimatedSalaryPurchased
015624510Male19190000
115810944Male35200000
215668575Female26430000
315603246Female27570000
415804002Male19760000
\n", 108 | "
" 109 | ], 110 | "text/plain": [ 111 | " User ID Gender Age EstimatedSalary Purchased\n", 112 | "0 15624510 Male 19 19000 0\n", 113 | "1 15810944 Male 35 20000 0\n", 114 | "2 15668575 Female 26 43000 0\n", 115 | "3 15603246 Female 27 57000 0\n", 116 | "4 15804002 Male 19 76000 0" 117 | ] 118 | }, 119 | "execution_count": 34, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "dataset.head()" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "### defining independent and dependent variable to train the model" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 35, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "x = dataset.iloc[:,[2,3]].values\n", 142 | "#we picked independent variable as Age and EstimatedSalary" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 36, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "numpy.ndarray" 154 | ] 155 | }, 156 | "execution_count": 36, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "type(x)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "we picked dependent variable as Purchased since i have to predict whether you predict the car or not using the dependent variable like Age and EstimatedSalary in our case" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 37, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "y = dataset.iloc[:,[4]].values" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 38, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "array([[ 19, 19000],\n", 190 | " [ 35, 20000],\n", 191 | " [ 26, 43000],\n", 192 | " [ 27, 57000],\n", 193 | " [ 19, 76000],\n", 194 | " [ 27, 58000],\n", 195 | " [ 27, 84000],\n", 196 | " [ 32, 150000],\n", 197 | " [ 25, 33000],\n", 198 | " [ 35, 65000],\n", 199 | " [ 26, 80000],\n", 200 | " [ 26, 52000],\n", 201 | " [ 20, 86000],\n", 202 | " [ 32, 18000],\n", 203 | " [ 18, 82000],\n", 204 | " [ 29, 80000],\n", 205 | " [ 47, 25000],\n", 206 | " [ 45, 26000],\n", 207 | " [ 46, 28000],\n", 208 | " [ 48, 29000],\n", 209 | " [ 45, 22000],\n", 210 | " [ 47, 49000],\n", 211 | " [ 48, 41000],\n", 212 | " [ 45, 22000],\n", 213 | " [ 46, 23000],\n", 214 | " [ 47, 20000],\n", 215 | " [ 49, 28000],\n", 216 | " [ 47, 30000],\n", 217 | " [ 29, 43000],\n", 218 | " [ 31, 18000],\n", 219 | " [ 31, 74000],\n", 220 | " [ 27, 137000],\n", 221 | " [ 21, 16000],\n", 222 | " [ 28, 44000],\n", 223 | " [ 27, 90000],\n", 224 | " [ 35, 27000],\n", 225 | " [ 33, 28000],\n", 226 | " [ 30, 49000],\n", 227 | " [ 26, 72000],\n", 228 | " [ 27, 31000],\n", 229 | " [ 27, 17000],\n", 230 | " [ 33, 51000],\n", 231 | " [ 35, 108000],\n", 232 | " [ 30, 15000],\n", 233 | " [ 28, 84000],\n", 234 | " [ 23, 20000],\n", 235 | " [ 25, 79000],\n", 236 | " [ 27, 54000],\n", 237 | " [ 30, 135000],\n", 238 | " [ 31, 89000],\n", 239 | " [ 24, 32000],\n", 240 | " [ 18, 44000],\n", 241 | " [ 29, 83000],\n", 242 | " [ 35, 23000],\n", 243 | " [ 27, 58000],\n", 244 | " [ 24, 55000],\n", 245 | " [ 23, 48000],\n", 246 | " [ 28, 79000],\n", 247 | " [ 22, 18000],\n", 248 | " [ 32, 117000],\n", 249 | " [ 27, 20000],\n", 250 | " [ 25, 87000],\n", 251 | " [ 23, 66000],\n", 252 | " [ 32, 120000],\n", 253 | " [ 59, 83000],\n", 254 | " [ 24, 58000],\n", 255 | " [ 24, 19000],\n", 256 | " [ 23, 82000],\n", 257 | " [ 22, 63000],\n", 258 | " [ 31, 68000],\n", 259 | " [ 25, 80000],\n", 260 | " [ 24, 27000],\n", 261 | " [ 20, 23000],\n", 262 | " [ 33, 113000],\n", 263 | " [ 32, 18000],\n", 264 | " [ 34, 112000],\n", 265 | " [ 18, 52000],\n", 266 | " [ 22, 27000],\n", 267 | " [ 28, 87000],\n", 268 | " [ 26, 17000],\n", 269 | " [ 30, 80000],\n", 270 | " [ 39, 42000],\n", 271 | " [ 20, 49000],\n", 272 | " [ 35, 88000],\n", 273 | " [ 30, 62000],\n", 274 | " [ 31, 118000],\n", 275 | " [ 24, 55000],\n", 276 | " [ 28, 85000],\n", 277 | " [ 26, 81000],\n", 278 | " [ 35, 50000],\n", 279 | " [ 22, 81000],\n", 280 | " [ 30, 116000],\n", 281 | " [ 26, 15000],\n", 282 | " [ 29, 28000],\n", 283 | " [ 29, 83000],\n", 284 | " [ 35, 44000],\n", 285 | " [ 35, 25000],\n", 286 | " [ 28, 123000],\n", 287 | " [ 35, 73000],\n", 288 | " [ 28, 37000],\n", 289 | " [ 27, 88000],\n", 290 | " [ 28, 59000],\n", 291 | " [ 32, 86000],\n", 292 | " [ 33, 149000],\n", 293 | " [ 19, 21000],\n", 294 | " [ 21, 72000],\n", 295 | " [ 26, 35000],\n", 296 | " [ 27, 89000],\n", 297 | " [ 26, 86000],\n", 298 | " [ 38, 80000],\n", 299 | " [ 39, 71000],\n", 300 | " [ 37, 71000],\n", 301 | " [ 38, 61000],\n", 302 | " [ 37, 55000],\n", 303 | " [ 42, 80000],\n", 304 | " [ 40, 57000],\n", 305 | " [ 35, 75000],\n", 306 | " [ 36, 52000],\n", 307 | " [ 40, 59000],\n", 308 | " [ 41, 59000],\n", 309 | " [ 36, 75000],\n", 310 | " [ 37, 72000],\n", 311 | " [ 40, 75000],\n", 312 | " [ 35, 53000],\n", 313 | " [ 41, 51000],\n", 314 | " [ 39, 61000],\n", 315 | " [ 42, 65000],\n", 316 | " [ 26, 32000],\n", 317 | " [ 30, 17000],\n", 318 | " [ 26, 84000],\n", 319 | " [ 31, 58000],\n", 320 | " [ 33, 31000],\n", 321 | " [ 30, 87000],\n", 322 | " [ 21, 68000],\n", 323 | " [ 28, 55000],\n", 324 | " [ 23, 63000],\n", 325 | " [ 20, 82000],\n", 326 | " [ 30, 107000],\n", 327 | " [ 28, 59000],\n", 328 | " [ 19, 25000],\n", 329 | " [ 19, 85000],\n", 330 | " [ 18, 68000],\n", 331 | " [ 35, 59000],\n", 332 | " [ 30, 89000],\n", 333 | " [ 34, 25000],\n", 334 | " [ 24, 89000],\n", 335 | " [ 27, 96000],\n", 336 | " [ 41, 30000],\n", 337 | " [ 29, 61000],\n", 338 | " [ 20, 74000],\n", 339 | " [ 26, 15000],\n", 340 | " [ 41, 45000],\n", 341 | " [ 31, 76000],\n", 342 | " [ 36, 50000],\n", 343 | " [ 40, 47000],\n", 344 | " [ 31, 15000],\n", 345 | " [ 46, 59000],\n", 346 | " [ 29, 75000],\n", 347 | " [ 26, 30000],\n", 348 | " [ 32, 135000],\n", 349 | " [ 32, 100000],\n", 350 | " [ 25, 90000],\n", 351 | " [ 37, 33000],\n", 352 | " [ 35, 38000],\n", 353 | " [ 33, 69000],\n", 354 | " [ 18, 86000],\n", 355 | " [ 22, 55000],\n", 356 | " [ 35, 71000],\n", 357 | " [ 29, 148000],\n", 358 | " [ 29, 47000],\n", 359 | " [ 21, 88000],\n", 360 | " [ 34, 115000],\n", 361 | " [ 26, 118000],\n", 362 | " [ 34, 43000],\n", 363 | " [ 34, 72000],\n", 364 | " [ 23, 28000],\n", 365 | " [ 35, 47000],\n", 366 | " [ 25, 22000],\n", 367 | " [ 24, 23000],\n", 368 | " [ 31, 34000],\n", 369 | " [ 26, 16000],\n", 370 | " [ 31, 71000],\n", 371 | " [ 32, 117000],\n", 372 | " [ 33, 43000],\n", 373 | " [ 33, 60000],\n", 374 | " [ 31, 66000],\n", 375 | " [ 20, 82000],\n", 376 | " [ 33, 41000],\n", 377 | " [ 35, 72000],\n", 378 | " [ 28, 32000],\n", 379 | " [ 24, 84000],\n", 380 | " [ 19, 26000],\n", 381 | " [ 29, 43000],\n", 382 | " [ 19, 70000],\n", 383 | " [ 28, 89000],\n", 384 | " [ 34, 43000],\n", 385 | " [ 30, 79000],\n", 386 | " [ 20, 36000],\n", 387 | " [ 26, 80000],\n", 388 | " [ 35, 22000],\n", 389 | " [ 35, 39000],\n", 390 | " [ 49, 74000],\n", 391 | " [ 39, 134000],\n", 392 | " [ 41, 71000],\n", 393 | " [ 58, 101000],\n", 394 | " [ 47, 47000],\n", 395 | " [ 55, 130000],\n", 396 | " [ 52, 114000],\n", 397 | " [ 40, 142000],\n", 398 | " [ 46, 22000],\n", 399 | " [ 48, 96000],\n", 400 | " [ 52, 150000],\n", 401 | " [ 59, 42000],\n", 402 | " [ 35, 58000],\n", 403 | " [ 47, 43000],\n", 404 | " [ 60, 108000],\n", 405 | " [ 49, 65000],\n", 406 | " [ 40, 78000],\n", 407 | " [ 46, 96000],\n", 408 | " [ 59, 143000],\n", 409 | " [ 41, 80000],\n", 410 | " [ 35, 91000],\n", 411 | " [ 37, 144000],\n", 412 | " [ 60, 102000],\n", 413 | " [ 35, 60000],\n", 414 | " [ 37, 53000],\n", 415 | " [ 36, 126000],\n", 416 | " [ 56, 133000],\n", 417 | " [ 40, 72000],\n", 418 | " [ 42, 80000],\n", 419 | " [ 35, 147000],\n", 420 | " [ 39, 42000],\n", 421 | " [ 40, 107000],\n", 422 | " [ 49, 86000],\n", 423 | " [ 38, 112000],\n", 424 | " [ 46, 79000],\n", 425 | " [ 40, 57000],\n", 426 | " [ 37, 80000],\n", 427 | " [ 46, 82000],\n", 428 | " [ 53, 143000],\n", 429 | " [ 42, 149000],\n", 430 | " [ 38, 59000],\n", 431 | " [ 50, 88000],\n", 432 | " [ 56, 104000],\n", 433 | " [ 41, 72000],\n", 434 | " [ 51, 146000],\n", 435 | " [ 35, 50000],\n", 436 | " [ 57, 122000],\n", 437 | " [ 41, 52000],\n", 438 | " [ 35, 97000],\n", 439 | " [ 44, 39000],\n", 440 | " [ 37, 52000],\n", 441 | " [ 48, 134000],\n", 442 | " [ 37, 146000],\n", 443 | " [ 50, 44000],\n", 444 | " [ 52, 90000],\n", 445 | " [ 41, 72000],\n", 446 | " [ 40, 57000],\n", 447 | " [ 58, 95000],\n", 448 | " [ 45, 131000],\n", 449 | " [ 35, 77000],\n", 450 | " [ 36, 144000],\n", 451 | " [ 55, 125000],\n", 452 | " [ 35, 72000],\n", 453 | " [ 48, 90000],\n", 454 | " [ 42, 108000],\n", 455 | " [ 40, 75000],\n", 456 | " [ 37, 74000],\n", 457 | " [ 47, 144000],\n", 458 | " [ 40, 61000],\n", 459 | " [ 43, 133000],\n", 460 | " [ 59, 76000],\n", 461 | " [ 60, 42000],\n", 462 | " [ 39, 106000],\n", 463 | " [ 57, 26000],\n", 464 | " [ 57, 74000],\n", 465 | " [ 38, 71000],\n", 466 | " [ 49, 88000],\n", 467 | " [ 52, 38000],\n", 468 | " [ 50, 36000],\n", 469 | " [ 59, 88000],\n", 470 | " [ 35, 61000],\n", 471 | " [ 37, 70000],\n", 472 | " [ 52, 21000],\n", 473 | " [ 48, 141000],\n", 474 | " [ 37, 93000],\n", 475 | " [ 37, 62000],\n", 476 | " [ 48, 138000],\n", 477 | " [ 41, 79000],\n", 478 | " [ 37, 78000],\n", 479 | " [ 39, 134000],\n", 480 | " [ 49, 89000],\n", 481 | " [ 55, 39000],\n", 482 | " [ 37, 77000],\n", 483 | " [ 35, 57000],\n", 484 | " [ 36, 63000],\n", 485 | " [ 42, 73000],\n", 486 | " [ 43, 112000],\n", 487 | " [ 45, 79000],\n", 488 | " [ 46, 117000],\n", 489 | " [ 58, 38000],\n", 490 | " [ 48, 74000],\n", 491 | " [ 37, 137000],\n", 492 | " [ 37, 79000],\n", 493 | " [ 40, 60000],\n", 494 | " [ 42, 54000],\n", 495 | " [ 51, 134000],\n", 496 | " [ 47, 113000],\n", 497 | " [ 36, 125000],\n", 498 | " [ 38, 50000],\n", 499 | " [ 42, 70000],\n", 500 | " [ 39, 96000],\n", 501 | " [ 38, 50000],\n", 502 | " [ 49, 141000],\n", 503 | " [ 39, 79000],\n", 504 | " [ 39, 75000],\n", 505 | " [ 54, 104000],\n", 506 | " [ 35, 55000],\n", 507 | " [ 45, 32000],\n", 508 | " [ 36, 60000],\n", 509 | " [ 52, 138000],\n", 510 | " [ 53, 82000],\n", 511 | " [ 41, 52000],\n", 512 | " [ 48, 30000],\n", 513 | " [ 48, 131000],\n", 514 | " [ 41, 60000],\n", 515 | " [ 41, 72000],\n", 516 | " [ 42, 75000],\n", 517 | " [ 36, 118000],\n", 518 | " [ 47, 107000],\n", 519 | " [ 38, 51000],\n", 520 | " [ 48, 119000],\n", 521 | " [ 42, 65000],\n", 522 | " [ 40, 65000],\n", 523 | " [ 57, 60000],\n", 524 | " [ 36, 54000],\n", 525 | " [ 58, 144000],\n", 526 | " [ 35, 79000],\n", 527 | " [ 38, 55000],\n", 528 | " [ 39, 122000],\n", 529 | " [ 53, 104000],\n", 530 | " [ 35, 75000],\n", 531 | " [ 38, 65000],\n", 532 | " [ 47, 51000],\n", 533 | " [ 47, 105000],\n", 534 | " [ 41, 63000],\n", 535 | " [ 53, 72000],\n", 536 | " [ 54, 108000],\n", 537 | " [ 39, 77000],\n", 538 | " [ 38, 61000],\n", 539 | " [ 38, 113000],\n", 540 | " [ 37, 75000],\n", 541 | " [ 42, 90000],\n", 542 | " [ 37, 57000],\n", 543 | " [ 36, 99000],\n", 544 | " [ 60, 34000],\n", 545 | " [ 54, 70000],\n", 546 | " [ 41, 72000],\n", 547 | " [ 40, 71000],\n", 548 | " [ 42, 54000],\n", 549 | " [ 43, 129000],\n", 550 | " [ 53, 34000],\n", 551 | " [ 47, 50000],\n", 552 | " [ 42, 79000],\n", 553 | " [ 42, 104000],\n", 554 | " [ 59, 29000],\n", 555 | " [ 58, 47000],\n", 556 | " [ 46, 88000],\n", 557 | " [ 38, 71000],\n", 558 | " [ 54, 26000],\n", 559 | " [ 60, 46000],\n", 560 | " [ 60, 83000],\n", 561 | " [ 39, 73000],\n", 562 | " [ 59, 130000],\n", 563 | " [ 37, 80000],\n", 564 | " [ 46, 32000],\n", 565 | " [ 46, 74000],\n", 566 | " [ 42, 53000],\n", 567 | " [ 41, 87000],\n", 568 | " [ 58, 23000],\n", 569 | " [ 42, 64000],\n", 570 | " [ 48, 33000],\n", 571 | " [ 44, 139000],\n", 572 | " [ 49, 28000],\n", 573 | " [ 57, 33000],\n", 574 | " [ 56, 60000],\n", 575 | " [ 49, 39000],\n", 576 | " [ 39, 71000],\n", 577 | " [ 47, 34000],\n", 578 | " [ 48, 35000],\n", 579 | " [ 48, 33000],\n", 580 | " [ 47, 23000],\n", 581 | " [ 45, 45000],\n", 582 | " [ 60, 42000],\n", 583 | " [ 39, 59000],\n", 584 | " [ 46, 41000],\n", 585 | " [ 51, 23000],\n", 586 | " [ 50, 20000],\n", 587 | " [ 36, 33000],\n", 588 | " [ 49, 36000]], dtype=int64)" 589 | ] 590 | }, 591 | "execution_count": 38, 592 | "metadata": {}, 593 | "output_type": "execute_result" 594 | } 595 | ], 596 | "source": [ 597 | "x" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 39, 603 | "metadata": {}, 604 | "outputs": [ 605 | { 606 | "data": { 607 | "text/plain": [ 608 | "array([[0],\n", 609 | " [0],\n", 610 | " [0],\n", 611 | " [0],\n", 612 | " [0],\n", 613 | " [0],\n", 614 | " [0],\n", 615 | " [1],\n", 616 | " [0],\n", 617 | " [0],\n", 618 | " [0],\n", 619 | " [0],\n", 620 | " [0],\n", 621 | " [0],\n", 622 | " [0],\n", 623 | " [0],\n", 624 | " [1],\n", 625 | " [1],\n", 626 | " [1],\n", 627 | " [1],\n", 628 | " [1],\n", 629 | " [1],\n", 630 | " [1],\n", 631 | " [1],\n", 632 | " [1],\n", 633 | " [1],\n", 634 | " [1],\n", 635 | " [1],\n", 636 | " [0],\n", 637 | " [0],\n", 638 | " [0],\n", 639 | " [1],\n", 640 | " [0],\n", 641 | " [0],\n", 642 | " [0],\n", 643 | " [0],\n", 644 | " [0],\n", 645 | " [0],\n", 646 | " [0],\n", 647 | " [0],\n", 648 | " [0],\n", 649 | " [0],\n", 650 | " [0],\n", 651 | " [0],\n", 652 | " [0],\n", 653 | " [0],\n", 654 | " [0],\n", 655 | " [0],\n", 656 | " [1],\n", 657 | " [0],\n", 658 | " [0],\n", 659 | " [0],\n", 660 | " [0],\n", 661 | " [0],\n", 662 | " [0],\n", 663 | " [0],\n", 664 | " [0],\n", 665 | " [0],\n", 666 | " [0],\n", 667 | " [0],\n", 668 | " [0],\n", 669 | " [0],\n", 670 | " [0],\n", 671 | " [1],\n", 672 | " [0],\n", 673 | " [0],\n", 674 | " [0],\n", 675 | " [0],\n", 676 | " [0],\n", 677 | " [0],\n", 678 | " [0],\n", 679 | " [0],\n", 680 | " [0],\n", 681 | " [0],\n", 682 | " [0],\n", 683 | " [1],\n", 684 | " [0],\n", 685 | " [0],\n", 686 | " [0],\n", 687 | " [0],\n", 688 | " [0],\n", 689 | " [0],\n", 690 | " [0],\n", 691 | " [0],\n", 692 | " [0],\n", 693 | " [1],\n", 694 | " [0],\n", 695 | " [0],\n", 696 | " [0],\n", 697 | " [0],\n", 698 | " [0],\n", 699 | " [0],\n", 700 | " [0],\n", 701 | " [0],\n", 702 | " [0],\n", 703 | " [0],\n", 704 | " [0],\n", 705 | " [1],\n", 706 | " [0],\n", 707 | " [0],\n", 708 | " [0],\n", 709 | " [0],\n", 710 | " [0],\n", 711 | " [1],\n", 712 | " [0],\n", 713 | " [0],\n", 714 | " [0],\n", 715 | " [0],\n", 716 | " [0],\n", 717 | " [0],\n", 718 | " [0],\n", 719 | " [0],\n", 720 | " [0],\n", 721 | " [0],\n", 722 | " [0],\n", 723 | " [0],\n", 724 | " [0],\n", 725 | " [0],\n", 726 | " [0],\n", 727 | " [0],\n", 728 | " [0],\n", 729 | " [0],\n", 730 | " [0],\n", 731 | " [0],\n", 732 | " [0],\n", 733 | " [0],\n", 734 | " [0],\n", 735 | " [0],\n", 736 | " [0],\n", 737 | " [0],\n", 738 | " [0],\n", 739 | " [0],\n", 740 | " [0],\n", 741 | " [0],\n", 742 | " [0],\n", 743 | " [0],\n", 744 | " [0],\n", 745 | " [1],\n", 746 | " [0],\n", 747 | " [0],\n", 748 | " [0],\n", 749 | " [0],\n", 750 | " [0],\n", 751 | " [0],\n", 752 | " [0],\n", 753 | " [0],\n", 754 | " [1],\n", 755 | " [0],\n", 756 | " [0],\n", 757 | " [0],\n", 758 | " [0],\n", 759 | " [0],\n", 760 | " [0],\n", 761 | " [0],\n", 762 | " [0],\n", 763 | " [0],\n", 764 | " [0],\n", 765 | " [0],\n", 766 | " [0],\n", 767 | " [1],\n", 768 | " [1],\n", 769 | " [0],\n", 770 | " [0],\n", 771 | " [0],\n", 772 | " [0],\n", 773 | " [0],\n", 774 | " [0],\n", 775 | " [0],\n", 776 | " [1],\n", 777 | " [0],\n", 778 | " [0],\n", 779 | " [0],\n", 780 | " [0],\n", 781 | " [0],\n", 782 | " [0],\n", 783 | " [0],\n", 784 | " [0],\n", 785 | " [0],\n", 786 | " [0],\n", 787 | " [0],\n", 788 | " [0],\n", 789 | " [0],\n", 790 | " [1],\n", 791 | " [0],\n", 792 | " [0],\n", 793 | " [0],\n", 794 | " [0],\n", 795 | " [0],\n", 796 | " [0],\n", 797 | " [0],\n", 798 | " [0],\n", 799 | " [0],\n", 800 | " [0],\n", 801 | " [0],\n", 802 | " [0],\n", 803 | " [0],\n", 804 | " [0],\n", 805 | " [0],\n", 806 | " [0],\n", 807 | " [0],\n", 808 | " [0],\n", 809 | " [0],\n", 810 | " [1],\n", 811 | " [0],\n", 812 | " [1],\n", 813 | " [0],\n", 814 | " [1],\n", 815 | " [0],\n", 816 | " [1],\n", 817 | " [0],\n", 818 | " [1],\n", 819 | " [1],\n", 820 | " [0],\n", 821 | " [0],\n", 822 | " [0],\n", 823 | " [1],\n", 824 | " [0],\n", 825 | " [0],\n", 826 | " [0],\n", 827 | " [1],\n", 828 | " [0],\n", 829 | " [1],\n", 830 | " [1],\n", 831 | " [1],\n", 832 | " [0],\n", 833 | " [0],\n", 834 | " [1],\n", 835 | " [1],\n", 836 | " [0],\n", 837 | " [1],\n", 838 | " [1],\n", 839 | " [0],\n", 840 | " [1],\n", 841 | " [1],\n", 842 | " [0],\n", 843 | " [1],\n", 844 | " [0],\n", 845 | " [0],\n", 846 | " [0],\n", 847 | " [1],\n", 848 | " [1],\n", 849 | " [0],\n", 850 | " [1],\n", 851 | " [1],\n", 852 | " [0],\n", 853 | " [1],\n", 854 | " [0],\n", 855 | " [1],\n", 856 | " [0],\n", 857 | " [1],\n", 858 | " [0],\n", 859 | " [0],\n", 860 | " [1],\n", 861 | " [1],\n", 862 | " [0],\n", 863 | " [1],\n", 864 | " [0],\n", 865 | " [0],\n", 866 | " [1],\n", 867 | " [1],\n", 868 | " [0],\n", 869 | " [1],\n", 870 | " [1],\n", 871 | " [0],\n", 872 | " [1],\n", 873 | " [1],\n", 874 | " [0],\n", 875 | " [0],\n", 876 | " [1],\n", 877 | " [0],\n", 878 | " [0],\n", 879 | " [1],\n", 880 | " [1],\n", 881 | " [1],\n", 882 | " [1],\n", 883 | " [1],\n", 884 | " [0],\n", 885 | " [1],\n", 886 | " [1],\n", 887 | " [1],\n", 888 | " [1],\n", 889 | " [0],\n", 890 | " [1],\n", 891 | " [1],\n", 892 | " [0],\n", 893 | " [1],\n", 894 | " [0],\n", 895 | " [1],\n", 896 | " [0],\n", 897 | " [1],\n", 898 | " [1],\n", 899 | " [1],\n", 900 | " [1],\n", 901 | " [0],\n", 902 | " [0],\n", 903 | " [0],\n", 904 | " [1],\n", 905 | " [1],\n", 906 | " [0],\n", 907 | " [1],\n", 908 | " [1],\n", 909 | " [1],\n", 910 | " [1],\n", 911 | " [1],\n", 912 | " [0],\n", 913 | " [0],\n", 914 | " [0],\n", 915 | " [1],\n", 916 | " [1],\n", 917 | " [0],\n", 918 | " [0],\n", 919 | " [1],\n", 920 | " [0],\n", 921 | " [1],\n", 922 | " [0],\n", 923 | " [1],\n", 924 | " [1],\n", 925 | " [0],\n", 926 | " [1],\n", 927 | " [0],\n", 928 | " [1],\n", 929 | " [1],\n", 930 | " [0],\n", 931 | " [1],\n", 932 | " [1],\n", 933 | " [0],\n", 934 | " [0],\n", 935 | " [0],\n", 936 | " [1],\n", 937 | " [1],\n", 938 | " [0],\n", 939 | " [1],\n", 940 | " [0],\n", 941 | " [0],\n", 942 | " [1],\n", 943 | " [0],\n", 944 | " [1],\n", 945 | " [0],\n", 946 | " [0],\n", 947 | " [1],\n", 948 | " [1],\n", 949 | " [0],\n", 950 | " [0],\n", 951 | " [1],\n", 952 | " [1],\n", 953 | " [0],\n", 954 | " [1],\n", 955 | " [1],\n", 956 | " [0],\n", 957 | " [0],\n", 958 | " [1],\n", 959 | " [0],\n", 960 | " [1],\n", 961 | " [0],\n", 962 | " [1],\n", 963 | " [1],\n", 964 | " [1],\n", 965 | " [0],\n", 966 | " [1],\n", 967 | " [0],\n", 968 | " [1],\n", 969 | " [1],\n", 970 | " [1],\n", 971 | " [0],\n", 972 | " [1],\n", 973 | " [1],\n", 974 | " [1],\n", 975 | " [1],\n", 976 | " [0],\n", 977 | " [1],\n", 978 | " [1],\n", 979 | " [1],\n", 980 | " [0],\n", 981 | " [1],\n", 982 | " [0],\n", 983 | " [1],\n", 984 | " [0],\n", 985 | " [0],\n", 986 | " [1],\n", 987 | " [1],\n", 988 | " [0],\n", 989 | " [1],\n", 990 | " [1],\n", 991 | " [1],\n", 992 | " [1],\n", 993 | " [1],\n", 994 | " [1],\n", 995 | " [0],\n", 996 | " [1],\n", 997 | " [1],\n", 998 | " [1],\n", 999 | " [1],\n", 1000 | " [1],\n", 1001 | " [1],\n", 1002 | " [0],\n", 1003 | " [1],\n", 1004 | " [1],\n", 1005 | " [1],\n", 1006 | " [0],\n", 1007 | " [1]], dtype=int64)" 1008 | ] 1009 | }, 1010 | "execution_count": 39, 1011 | "metadata": {}, 1012 | "output_type": "execute_result" 1013 | } 1014 | ], 1015 | "source": [ 1016 | "y" 1017 | ] 1018 | }, 1019 | { 1020 | "cell_type": "markdown", 1021 | "metadata": {}, 1022 | "source": [ 1023 | "now dividing the data into training and test subset" 1024 | ] 1025 | }, 1026 | { 1027 | "cell_type": "code", 1028 | "execution_count": 40, 1029 | "metadata": {}, 1030 | "outputs": [], 1031 | "source": [ 1032 | "from sklearn.cross_validation import train_test_split" 1033 | ] 1034 | }, 1035 | { 1036 | "cell_type": "code", 1037 | "execution_count": 41, 1038 | "metadata": {}, 1039 | "outputs": [], 1040 | "source": [ 1041 | "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=0)" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "code", 1046 | "execution_count": 42, 1047 | "metadata": {}, 1048 | "outputs": [], 1049 | "source": [ 1050 | "from sklearn.preprocessing import StandardScaler" 1051 | ] 1052 | }, 1053 | { 1054 | "cell_type": "code", 1055 | "execution_count": 43, 1056 | "metadata": {}, 1057 | "outputs": [ 1058 | { 1059 | "data": { 1060 | "text/html": [ 1061 | "
\n", 1062 | "\n", 1075 | "\n", 1076 | " \n", 1077 | " \n", 1078 | " \n", 1079 | " \n", 1080 | " \n", 1081 | " \n", 1082 | " \n", 1083 | " \n", 1084 | " \n", 1085 | " \n", 1086 | " \n", 1087 | " \n", 1088 | " \n", 1089 | " \n", 1090 | " \n", 1091 | " \n", 1092 | " \n", 1093 | " \n", 1094 | " \n", 1095 | " \n", 1096 | " \n", 1097 | " \n", 1098 | " \n", 1099 | " \n", 1100 | " \n", 1101 | " \n", 1102 | " \n", 1103 | " \n", 1104 | " \n", 1105 | " \n", 1106 | " \n", 1107 | " \n", 1108 | " \n", 1109 | " \n", 1110 | " \n", 1111 | " \n", 1112 | " \n", 1113 | " \n", 1114 | " \n", 1115 | " \n", 1116 | " \n", 1117 | " \n", 1118 | " \n", 1119 | " \n", 1120 | " \n", 1121 | " \n", 1122 | " \n", 1123 | " \n", 1124 | " \n", 1125 | " \n", 1126 | " \n", 1127 | " \n", 1128 | " \n", 1129 | " \n", 1130 | " \n", 1131 | " \n", 1132 | " \n", 1133 | " \n", 1134 | " \n", 1135 | " \n", 1136 | " \n", 1137 | " \n", 1138 | " \n", 1139 | " \n", 1140 | " \n", 1141 | " \n", 1142 | " \n", 1143 | " \n", 1144 | "
User IDGenderAgeEstimatedSalaryPurchased
015624510Male19190000
115810944Male35200000
215668575Female26430000
315603246Female27570000
415804002Male19760000
515728773Male27580000
615598044Female27840000
\n", 1145 | "
" 1146 | ], 1147 | "text/plain": [ 1148 | " User ID Gender Age EstimatedSalary Purchased\n", 1149 | "0 15624510 Male 19 19000 0\n", 1150 | "1 15810944 Male 35 20000 0\n", 1151 | "2 15668575 Female 26 43000 0\n", 1152 | "3 15603246 Female 27 57000 0\n", 1153 | "4 15804002 Male 19 76000 0\n", 1154 | "5 15728773 Male 27 58000 0\n", 1155 | "6 15598044 Female 27 84000 0" 1156 | ] 1157 | }, 1158 | "execution_count": 43, 1159 | "metadata": {}, 1160 | "output_type": "execute_result" 1161 | } 1162 | ], 1163 | "source": [ 1164 | "dataset.head(7)" 1165 | ] 1166 | }, 1167 | { 1168 | "cell_type": "code", 1169 | "execution_count": 44, 1170 | "metadata": {}, 1171 | "outputs": [ 1172 | { 1173 | "data": { 1174 | "text/plain": [ 1175 | "pandas.core.frame.DataFrame" 1176 | ] 1177 | }, 1178 | "execution_count": 44, 1179 | "metadata": {}, 1180 | "output_type": "execute_result" 1181 | } 1182 | ], 1183 | "source": [ 1184 | "type(dataset)" 1185 | ] 1186 | }, 1187 | { 1188 | "cell_type": "code", 1189 | "execution_count": 45, 1190 | "metadata": {}, 1191 | "outputs": [ 1192 | { 1193 | "name": "stderr", 1194 | "output_type": "stream", 1195 | "text": [ 1196 | "C:\\Users\\admin\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n", 1197 | " warnings.warn(msg, DataConversionWarning)\n", 1198 | "C:\\Users\\admin\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n", 1199 | " warnings.warn(msg, DataConversionWarning)\n", 1200 | "C:\\Users\\admin\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n", 1201 | " warnings.warn(msg, DataConversionWarning)\n" 1202 | ] 1203 | } 1204 | ], 1205 | "source": [ 1206 | "sc = StandardScaler()\n", 1207 | "x_train = sc.fit_transform(x_train)\n", 1208 | "x_test = sc.transform(x_test)" 1209 | ] 1210 | }, 1211 | { 1212 | "cell_type": "code", 1213 | "execution_count": 46, 1214 | "metadata": {}, 1215 | "outputs": [], 1216 | "source": [ 1217 | "from sklearn.linear_model import LogisticRegression" 1218 | ] 1219 | }, 1220 | { 1221 | "cell_type": "code", 1222 | "execution_count": 47, 1223 | "metadata": {}, 1224 | "outputs": [], 1225 | "source": [ 1226 | "classifier = LogisticRegression(random_state=0)" 1227 | ] 1228 | }, 1229 | { 1230 | "cell_type": "code", 1231 | "execution_count": 48, 1232 | "metadata": {}, 1233 | "outputs": [ 1234 | { 1235 | "data": { 1236 | "text/plain": [ 1237 | "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n", 1238 | " intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n", 1239 | " penalty='l2', random_state=0, solver='liblinear', tol=0.0001,\n", 1240 | " verbose=0, warm_start=False)" 1241 | ] 1242 | }, 1243 | "execution_count": 48, 1244 | "metadata": {}, 1245 | "output_type": "execute_result" 1246 | } 1247 | ], 1248 | "source": [ 1249 | "classifier" 1250 | ] 1251 | }, 1252 | { 1253 | "cell_type": "code", 1254 | "execution_count": 49, 1255 | "metadata": {}, 1256 | "outputs": [ 1257 | { 1258 | "name": "stderr", 1259 | "output_type": "stream", 1260 | "text": [ 1261 | "C:\\Users\\admin\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n", 1262 | " y = column_or_1d(y, warn=True)\n" 1263 | ] 1264 | }, 1265 | { 1266 | "data": { 1267 | "text/plain": [ 1268 | "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n", 1269 | " intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n", 1270 | " penalty='l2', random_state=0, solver='liblinear', tol=0.0001,\n", 1271 | " verbose=0, warm_start=False)" 1272 | ] 1273 | }, 1274 | "execution_count": 49, 1275 | "metadata": {}, 1276 | "output_type": "execute_result" 1277 | } 1278 | ], 1279 | "source": [ 1280 | "classifier.fit(x_train,y_train)" 1281 | ] 1282 | }, 1283 | { 1284 | "cell_type": "code", 1285 | "execution_count": 50, 1286 | "metadata": {}, 1287 | "outputs": [], 1288 | "source": [ 1289 | "predictions = classifier.predict(x_test)" 1290 | ] 1291 | }, 1292 | { 1293 | "cell_type": "code", 1294 | "execution_count": 51, 1295 | "metadata": {}, 1296 | "outputs": [ 1297 | { 1298 | "data": { 1299 | "text/plain": [ 1300 | "array([0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,\n", 1301 | " 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,\n", 1302 | " 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,\n", 1303 | " 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1,\n", 1304 | " 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1], dtype=int64)" 1305 | ] 1306 | }, 1307 | "execution_count": 51, 1308 | "metadata": {}, 1309 | "output_type": "execute_result" 1310 | } 1311 | ], 1312 | "source": [ 1313 | "predictions" 1314 | ] 1315 | }, 1316 | { 1317 | "cell_type": "code", 1318 | "execution_count": 52, 1319 | "metadata": {}, 1320 | "outputs": [], 1321 | "source": [ 1322 | "from sklearn.metrics import accuracy_score" 1323 | ] 1324 | }, 1325 | { 1326 | "cell_type": "code", 1327 | "execution_count": 53, 1328 | "metadata": {}, 1329 | "outputs": [ 1330 | { 1331 | "data": { 1332 | "text/plain": [ 1333 | "89.0" 1334 | ] 1335 | }, 1336 | "execution_count": 53, 1337 | "metadata": {}, 1338 | "output_type": "execute_result" 1339 | } 1340 | ], 1341 | "source": [ 1342 | "accuracy_score(y_test,predictions)*100" 1343 | ] 1344 | }, 1345 | { 1346 | "cell_type": "code", 1347 | "execution_count": null, 1348 | "metadata": {}, 1349 | "outputs": [], 1350 | "source": [] 1351 | } 1352 | ], 1353 | "metadata": { 1354 | "kernelspec": { 1355 | "display_name": "Python 2", 1356 | "language": "python", 1357 | "name": "python2" 1358 | }, 1359 | "language_info": { 1360 | "codemirror_mode": { 1361 | "name": "ipython", 1362 | "version": 2 1363 | }, 1364 | "file_extension": ".py", 1365 | "mimetype": "text/x-python", 1366 | "name": "python", 1367 | "nbconvert_exporter": "python", 1368 | "pygments_lexer": "ipython2", 1369 | "version": "2.7.15" 1370 | } 1371 | }, 1372 | "nbformat": 4, 1373 | "nbformat_minor": 2 1374 | } 1375 | -------------------------------------------------------------------------------- /SUV_cars_data.csv: -------------------------------------------------------------------------------- 1 | User ID,Gender,Age,EstimatedSalary,Purchased 2 | 15624510,Male,19,19000,0 3 | 15810944,Male,35,20000,0 4 | 15668575,Female,26,43000,0 5 | 15603246,Female,27,57000,0 6 | 15804002,Male,19,76000,0 7 | 15728773,Male,27,58000,0 8 | 15598044,Female,27,84000,0 9 | 15694829,Female,32,150000,1 10 | 15600575,Male,25,33000,0 11 | 15727311,Female,35,65000,0 12 | 15570769,Female,26,80000,0 13 | 15606274,Female,26,52000,0 14 | 15746139,Male,20,86000,0 15 | 15704987,Male,32,18000,0 16 | 15628972,Male,18,82000,0 17 | 15697686,Male,29,80000,0 18 | 15733883,Male,47,25000,1 19 | 15617482,Male,45,26000,1 20 | 15704583,Male,46,28000,1 21 | 15621083,Female,48,29000,1 22 | 15649487,Male,45,22000,1 23 | 15736760,Female,47,49000,1 24 | 15714658,Male,48,41000,1 25 | 15599081,Female,45,22000,1 26 | 15705113,Male,46,23000,1 27 | 15631159,Male,47,20000,1 28 | 15792818,Male,49,28000,1 29 | 15633531,Female,47,30000,1 30 | 15744529,Male,29,43000,0 31 | 15669656,Male,31,18000,0 32 | 15581198,Male,31,74000,0 33 | 15729054,Female,27,137000,1 34 | 15573452,Female,21,16000,0 35 | 15776733,Female,28,44000,0 36 | 15724858,Male,27,90000,0 37 | 15713144,Male,35,27000,0 38 | 15690188,Female,33,28000,0 39 | 15689425,Male,30,49000,0 40 | 15671766,Female,26,72000,0 41 | 15782806,Female,27,31000,0 42 | 15764419,Female,27,17000,0 43 | 15591915,Female,33,51000,0 44 | 15772798,Male,35,108000,0 45 | 15792008,Male,30,15000,0 46 | 15715541,Female,28,84000,0 47 | 15639277,Male,23,20000,0 48 | 15798850,Male,25,79000,0 49 | 15776348,Female,27,54000,0 50 | 15727696,Male,30,135000,1 51 | 15793813,Female,31,89000,0 52 | 15694395,Female,24,32000,0 53 | 15764195,Female,18,44000,0 54 | 15744919,Female,29,83000,0 55 | 15671655,Female,35,23000,0 56 | 15654901,Female,27,58000,0 57 | 15649136,Female,24,55000,0 58 | 15775562,Female,23,48000,0 59 | 15807481,Male,28,79000,0 60 | 15642885,Male,22,18000,0 61 | 15789109,Female,32,117000,0 62 | 15814004,Male,27,20000,0 63 | 15673619,Male,25,87000,0 64 | 15595135,Female,23,66000,0 65 | 15583681,Male,32,120000,1 66 | 15605000,Female,59,83000,0 67 | 15718071,Male,24,58000,0 68 | 15679760,Male,24,19000,0 69 | 15654574,Female,23,82000,0 70 | 15577178,Female,22,63000,0 71 | 15595324,Female,31,68000,0 72 | 15756932,Male,25,80000,0 73 | 15726358,Female,24,27000,0 74 | 15595228,Female,20,23000,0 75 | 15782530,Female,33,113000,0 76 | 15592877,Male,32,18000,0 77 | 15651983,Male,34,112000,1 78 | 15746737,Male,18,52000,0 79 | 15774179,Female,22,27000,0 80 | 15667265,Female,28,87000,0 81 | 15655123,Female,26,17000,0 82 | 15595917,Male,30,80000,0 83 | 15668385,Male,39,42000,0 84 | 15709476,Male,20,49000,0 85 | 15711218,Male,35,88000,0 86 | 15798659,Female,30,62000,0 87 | 15663939,Female,31,118000,1 88 | 15694946,Male,24,55000,0 89 | 15631912,Female,28,85000,0 90 | 15768816,Male,26,81000,0 91 | 15682268,Male,35,50000,0 92 | 15684801,Male,22,81000,0 93 | 15636428,Female,30,116000,0 94 | 15809823,Male,26,15000,0 95 | 15699284,Female,29,28000,0 96 | 15786993,Female,29,83000,0 97 | 15709441,Female,35,44000,0 98 | 15710257,Female,35,25000,0 99 | 15582492,Male,28,123000,1 100 | 15575694,Male,35,73000,0 101 | 15756820,Female,28,37000,0 102 | 15766289,Male,27,88000,0 103 | 15593014,Male,28,59000,0 104 | 15584545,Female,32,86000,0 105 | 15675949,Female,33,149000,1 106 | 15672091,Female,19,21000,0 107 | 15801658,Male,21,72000,0 108 | 15706185,Female,26,35000,0 109 | 15789863,Male,27,89000,0 110 | 15720943,Male,26,86000,0 111 | 15697997,Female,38,80000,0 112 | 15665416,Female,39,71000,0 113 | 15660200,Female,37,71000,0 114 | 15619653,Male,38,61000,0 115 | 15773447,Male,37,55000,0 116 | 15739160,Male,42,80000,0 117 | 15689237,Male,40,57000,0 118 | 15679297,Male,35,75000,0 119 | 15591433,Male,36,52000,0 120 | 15642725,Male,40,59000,0 121 | 15701962,Male,41,59000,0 122 | 15811613,Female,36,75000,0 123 | 15741049,Male,37,72000,0 124 | 15724423,Female,40,75000,0 125 | 15574305,Male,35,53000,0 126 | 15678168,Female,41,51000,0 127 | 15697020,Female,39,61000,0 128 | 15610801,Male,42,65000,0 129 | 15745232,Male,26,32000,0 130 | 15722758,Male,30,17000,0 131 | 15792102,Female,26,84000,0 132 | 15675185,Male,31,58000,0 133 | 15801247,Male,33,31000,0 134 | 15725660,Male,30,87000,0 135 | 15638963,Female,21,68000,0 136 | 15800061,Female,28,55000,0 137 | 15578006,Male,23,63000,0 138 | 15668504,Female,20,82000,0 139 | 15687491,Male,30,107000,1 140 | 15610403,Female,28,59000,0 141 | 15741094,Male,19,25000,0 142 | 15807909,Male,19,85000,0 143 | 15666141,Female,18,68000,0 144 | 15617134,Male,35,59000,0 145 | 15783029,Male,30,89000,0 146 | 15622833,Female,34,25000,0 147 | 15746422,Female,24,89000,0 148 | 15750839,Female,27,96000,1 149 | 15749130,Female,41,30000,0 150 | 15779862,Male,29,61000,0 151 | 15767871,Male,20,74000,0 152 | 15679651,Female,26,15000,0 153 | 15576219,Male,41,45000,0 154 | 15699247,Male,31,76000,0 155 | 15619087,Female,36,50000,0 156 | 15605327,Male,40,47000,0 157 | 15610140,Female,31,15000,0 158 | 15791174,Male,46,59000,0 159 | 15602373,Male,29,75000,0 160 | 15762605,Male,26,30000,0 161 | 15598840,Female,32,135000,1 162 | 15744279,Male,32,100000,1 163 | 15670619,Male,25,90000,0 164 | 15599533,Female,37,33000,0 165 | 15757837,Male,35,38000,0 166 | 15697574,Female,33,69000,0 167 | 15578738,Female,18,86000,0 168 | 15762228,Female,22,55000,0 169 | 15614827,Female,35,71000,0 170 | 15789815,Male,29,148000,1 171 | 15579781,Female,29,47000,0 172 | 15587013,Male,21,88000,0 173 | 15570932,Male,34,115000,0 174 | 15794661,Female,26,118000,0 175 | 15581654,Female,34,43000,0 176 | 15644296,Female,34,72000,0 177 | 15614420,Female,23,28000,0 178 | 15609653,Female,35,47000,0 179 | 15594577,Male,25,22000,0 180 | 15584114,Male,24,23000,0 181 | 15673367,Female,31,34000,0 182 | 15685576,Male,26,16000,0 183 | 15774727,Female,31,71000,0 184 | 15694288,Female,32,117000,1 185 | 15603319,Male,33,43000,0 186 | 15759066,Female,33,60000,0 187 | 15814816,Male,31,66000,0 188 | 15724402,Female,20,82000,0 189 | 15571059,Female,33,41000,0 190 | 15674206,Male,35,72000,0 191 | 15715160,Male,28,32000,0 192 | 15730448,Male,24,84000,0 193 | 15662067,Female,19,26000,0 194 | 15779581,Male,29,43000,0 195 | 15662901,Male,19,70000,0 196 | 15689751,Male,28,89000,0 197 | 15667742,Male,34,43000,0 198 | 15738448,Female,30,79000,0 199 | 15680243,Female,20,36000,0 200 | 15745083,Male,26,80000,0 201 | 15708228,Male,35,22000,0 202 | 15628523,Male,35,39000,0 203 | 15708196,Male,49,74000,0 204 | 15735549,Female,39,134000,1 205 | 15809347,Female,41,71000,0 206 | 15660866,Female,58,101000,1 207 | 15766609,Female,47,47000,0 208 | 15654230,Female,55,130000,1 209 | 15794566,Female,52,114000,0 210 | 15800890,Female,40,142000,1 211 | 15697424,Female,46,22000,0 212 | 15724536,Female,48,96000,1 213 | 15735878,Male,52,150000,1 214 | 15707596,Female,59,42000,0 215 | 15657163,Male,35,58000,0 216 | 15622478,Male,47,43000,0 217 | 15779529,Female,60,108000,1 218 | 15636023,Male,49,65000,0 219 | 15582066,Male,40,78000,0 220 | 15666675,Female,46,96000,0 221 | 15732987,Male,59,143000,1 222 | 15789432,Female,41,80000,0 223 | 15663161,Male,35,91000,1 224 | 15694879,Male,37,144000,1 225 | 15593715,Male,60,102000,1 226 | 15575002,Female,35,60000,0 227 | 15622171,Male,37,53000,0 228 | 15795224,Female,36,126000,1 229 | 15685346,Male,56,133000,1 230 | 15691808,Female,40,72000,0 231 | 15721007,Female,42,80000,1 232 | 15794253,Female,35,147000,1 233 | 15694453,Male,39,42000,0 234 | 15813113,Male,40,107000,1 235 | 15614187,Male,49,86000,1 236 | 15619407,Female,38,112000,0 237 | 15646227,Male,46,79000,1 238 | 15660541,Male,40,57000,0 239 | 15753874,Female,37,80000,0 240 | 15617877,Female,46,82000,0 241 | 15772073,Female,53,143000,1 242 | 15701537,Male,42,149000,1 243 | 15736228,Male,38,59000,0 244 | 15780572,Female,50,88000,1 245 | 15769596,Female,56,104000,1 246 | 15586996,Female,41,72000,0 247 | 15722061,Female,51,146000,1 248 | 15638003,Female,35,50000,0 249 | 15775590,Female,57,122000,1 250 | 15730688,Male,41,52000,0 251 | 15753102,Female,35,97000,1 252 | 15810075,Female,44,39000,0 253 | 15723373,Male,37,52000,0 254 | 15795298,Female,48,134000,1 255 | 15584320,Female,37,146000,1 256 | 15724161,Female,50,44000,0 257 | 15750056,Female,52,90000,1 258 | 15609637,Female,41,72000,0 259 | 15794493,Male,40,57000,0 260 | 15569641,Female,58,95000,1 261 | 15815236,Female,45,131000,1 262 | 15811177,Female,35,77000,0 263 | 15680587,Male,36,144000,1 264 | 15672821,Female,55,125000,1 265 | 15767681,Female,35,72000,0 266 | 15600379,Male,48,90000,1 267 | 15801336,Female,42,108000,1 268 | 15721592,Male,40,75000,0 269 | 15581282,Male,37,74000,0 270 | 15746203,Female,47,144000,1 271 | 15583137,Male,40,61000,0 272 | 15680752,Female,43,133000,0 273 | 15688172,Female,59,76000,1 274 | 15791373,Male,60,42000,1 275 | 15589449,Male,39,106000,1 276 | 15692819,Female,57,26000,1 277 | 15727467,Male,57,74000,1 278 | 15734312,Male,38,71000,0 279 | 15764604,Male,49,88000,1 280 | 15613014,Female,52,38000,1 281 | 15759684,Female,50,36000,1 282 | 15609669,Female,59,88000,1 283 | 15685536,Male,35,61000,0 284 | 15750447,Male,37,70000,1 285 | 15663249,Female,52,21000,1 286 | 15638646,Male,48,141000,0 287 | 15734161,Female,37,93000,1 288 | 15631070,Female,37,62000,0 289 | 15761950,Female,48,138000,1 290 | 15649668,Male,41,79000,0 291 | 15713912,Female,37,78000,1 292 | 15586757,Male,39,134000,1 293 | 15596522,Male,49,89000,1 294 | 15625395,Male,55,39000,1 295 | 15760570,Male,37,77000,0 296 | 15566689,Female,35,57000,0 297 | 15725794,Female,36,63000,0 298 | 15673539,Male,42,73000,1 299 | 15705298,Female,43,112000,1 300 | 15675791,Male,45,79000,0 301 | 15747043,Male,46,117000,1 302 | 15736397,Female,58,38000,1 303 | 15678201,Male,48,74000,1 304 | 15720745,Female,37,137000,1 305 | 15637593,Male,37,79000,1 306 | 15598070,Female,40,60000,0 307 | 15787550,Male,42,54000,0 308 | 15603942,Female,51,134000,0 309 | 15733973,Female,47,113000,1 310 | 15596761,Male,36,125000,1 311 | 15652400,Female,38,50000,0 312 | 15717893,Female,42,70000,0 313 | 15622585,Male,39,96000,1 314 | 15733964,Female,38,50000,0 315 | 15753861,Female,49,141000,1 316 | 15747097,Female,39,79000,0 317 | 15594762,Female,39,75000,1 318 | 15667417,Female,54,104000,1 319 | 15684861,Male,35,55000,0 320 | 15742204,Male,45,32000,1 321 | 15623502,Male,36,60000,0 322 | 15774872,Female,52,138000,1 323 | 15611191,Female,53,82000,1 324 | 15674331,Male,41,52000,0 325 | 15619465,Female,48,30000,1 326 | 15575247,Female,48,131000,1 327 | 15695679,Female,41,60000,0 328 | 15713463,Male,41,72000,0 329 | 15785170,Female,42,75000,0 330 | 15796351,Male,36,118000,1 331 | 15639576,Female,47,107000,1 332 | 15693264,Male,38,51000,0 333 | 15589715,Female,48,119000,1 334 | 15769902,Male,42,65000,0 335 | 15587177,Male,40,65000,0 336 | 15814553,Male,57,60000,1 337 | 15601550,Female,36,54000,0 338 | 15664907,Male,58,144000,1 339 | 15612465,Male,35,79000,0 340 | 15810800,Female,38,55000,0 341 | 15665760,Male,39,122000,1 342 | 15588080,Female,53,104000,1 343 | 15776844,Male,35,75000,0 344 | 15717560,Female,38,65000,0 345 | 15629739,Female,47,51000,1 346 | 15729908,Male,47,105000,1 347 | 15716781,Female,41,63000,0 348 | 15646936,Male,53,72000,1 349 | 15768151,Female,54,108000,1 350 | 15579212,Male,39,77000,0 351 | 15721835,Male,38,61000,0 352 | 15800515,Female,38,113000,1 353 | 15591279,Male,37,75000,0 354 | 15587419,Female,42,90000,1 355 | 15750335,Female,37,57000,0 356 | 15699619,Male,36,99000,1 357 | 15606472,Male,60,34000,1 358 | 15778368,Male,54,70000,1 359 | 15671387,Female,41,72000,0 360 | 15573926,Male,40,71000,1 361 | 15709183,Male,42,54000,0 362 | 15577514,Male,43,129000,1 363 | 15778830,Female,53,34000,1 364 | 15768072,Female,47,50000,1 365 | 15768293,Female,42,79000,0 366 | 15654456,Male,42,104000,1 367 | 15807525,Female,59,29000,1 368 | 15574372,Female,58,47000,1 369 | 15671249,Male,46,88000,1 370 | 15779744,Male,38,71000,0 371 | 15624755,Female,54,26000,1 372 | 15611430,Female,60,46000,1 373 | 15774744,Male,60,83000,1 374 | 15629885,Female,39,73000,0 375 | 15708791,Male,59,130000,1 376 | 15793890,Female,37,80000,0 377 | 15646091,Female,46,32000,1 378 | 15596984,Female,46,74000,0 379 | 15800215,Female,42,53000,0 380 | 15577806,Male,41,87000,1 381 | 15749381,Female,58,23000,1 382 | 15683758,Male,42,64000,0 383 | 15670615,Male,48,33000,1 384 | 15715622,Female,44,139000,1 385 | 15707634,Male,49,28000,1 386 | 15806901,Female,57,33000,1 387 | 15775335,Male,56,60000,1 388 | 15724150,Female,49,39000,1 389 | 15627220,Male,39,71000,0 390 | 15672330,Male,47,34000,1 391 | 15668521,Female,48,35000,1 392 | 15807837,Male,48,33000,1 393 | 15592570,Male,47,23000,1 394 | 15748589,Female,45,45000,1 395 | 15635893,Male,60,42000,1 396 | 15757632,Female,39,59000,0 397 | 15691863,Female,46,41000,1 398 | 15706071,Male,51,23000,1 399 | 15654296,Female,50,20000,1 400 | 15755018,Male,36,33000,0 401 | 15594041,Female,49,36000,1 -------------------------------------------------------------------------------- /github.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "ename": "SyntaxError", 10 | "evalue": "invalid syntax (, line 1)", 11 | "output_type": "error", 12 | "traceback": [ 13 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m What Does It Take to Be An Expert At Python\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "What Does It Take to Be An Expert At Python\n", 19 | "Notebook based off James Powell's talk at PyData 2017'\n", 20 | "https://www.youtube.com/watch?v=7lmCu8wz8ro\n", 21 | "\n", 22 | "If you want to become an expert in Python, you should definitely watch this PyData talk from James Powell.\n", 23 | "\n", 24 | "Video Index\n", 25 | "metaclasses: 18:50\n", 26 | "metaclasses(explained): 40:40\n", 27 | "decorator: 45:20\n", 28 | "generator: 1:04:30\n", 29 | "context manager: 1:22:37\n", 30 | "summary: 1:40:00\n", 31 | "Definitions Python is a language orientated around protocols - Some behavior or syntax or bytecode or some top level function and there is a way to tell python how to implement that on an arbitrary object via underscore methods. The exact correspondance is usually guessable, but if you can't guess it you can it... google python data model\n", 32 | "\n", 33 | "Metaclass Mechanism: Some hook into the class construction process. Questions: Do you have these methods implemented. Meaning: Library code & User code? How do you enforce a constraint?\n", 34 | "\n", 35 | "Decorator Hooks into idea that everything creates a structure at run time. Wrap sets of functions with a before and after behavior.\n", 36 | "\n", 37 | "Generators Take a single computation that would otherwise run eagerly from the injection of its parameters to the final computation and interleaving with other code by adding yield points where you can yield the intermediate result values or one small piece of the computation and also yield back to the caller. Think of a generator of a way to take one long piece of computation and break it up into small parts.\n", 38 | "\n", 39 | "Context managers Two structures that allow you to tie two actions together. A setup action and a teardown action and make sure they always happen in concordance with each other.\n", 40 | "\n", 41 | "In [7]:\n", 42 | "# some behavior that I want to implement -> write some __ function __\n", 43 | "# top-level function or top-level syntax -> corresponding __\n", 44 | "# x + y -> __add__\n", 45 | "# init x -> __init__\n", 46 | "# repr(x) --> __repr__\n", 47 | "# x() -> __call__\n", 48 | "\n", 49 | "class Polynomial:\n", 50 | " def __init__(self, *coeffs):\n", 51 | " self.coeffs = coeffs\n", 52 | " \n", 53 | " def __repr__(self):\n", 54 | " return 'Polynomial(*{!r})'.format(self.coeffs)\n", 55 | " \n", 56 | " def __add__(self, other):\n", 57 | " return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))\n", 58 | " \n", 59 | " def __len__(self):\n", 60 | " return len(self.coeffs)\n", 61 | " \n", 62 | " def __call__(self):\n", 63 | " pass\n", 64 | "3 Core Patterns to understand object orientation\n", 65 | "Protocol view of python\n", 66 | "Built-in inheritance protocol (where to go)\n", 67 | "Caveats around how object orientation in python works\n", 68 | "In [8]:\n", 69 | "p1 = Polynomial(1, 2, 3)\n", 70 | "p2 = Polynomial(3, 4, 3)\n", 71 | "In [9]:\n", 72 | "p1 + p2\n", 73 | "Out[9]:\n", 74 | "Polynomial(*(5, 7, 7))\n", 75 | "In [10]:\n", 76 | "len(p1)\n", 77 | "Out[10]:\n", 78 | "3\n", 79 | "Metaclasses\n", 80 | "In [26]:\n", 81 | "# File 1 - library.py\n", 82 | "\n", 83 | "class Base:\n", 84 | " def food(self):\n", 85 | " return 'foo'\n", 86 | "In [27]:\n", 87 | "# File2 - user.py\n", 88 | "\n", 89 | "assert hasattr(Base, 'foo'), \"you broke it, you fool!\"\n", 90 | "\n", 91 | "class Derived(Base):\n", 92 | " def bar(self):\n", 93 | " return self.foo\n", 94 | "---------------------------------------------------------------------------\n", 95 | "AssertionError Traceback (most recent call last)\n", 96 | " in ()\n", 97 | " 1 # File2 - user.py\n", 98 | " 2 \n", 99 | "----> 3 assert hasattr(Base, 'foo'), \"you broke it, you fool!\"\n", 100 | " 4 \n", 101 | " 5 class Derived(Base):\n", 102 | "\n", 103 | "AssertionError: you broke it, you fool!\n", 104 | "In [41]:\n", 105 | "# File 1 - library.py\n", 106 | "\n", 107 | "class Base:\n", 108 | " def foo(self):\n", 109 | " return self.bar()\n", 110 | "In [42]:\n", 111 | "# File2 - user.py\n", 112 | "\n", 113 | "assert hasattr(Base, 'foo'), \"you broke it, you fool!\"\n", 114 | "\n", 115 | "class Derived(Base):\n", 116 | " def bar(self):\n", 117 | " return 'bar'\n", 118 | "In [45]:\n", 119 | "Derived.bar\n", 120 | "Out[45]:\n", 121 | "\n", 122 | "In [18]:\n", 123 | "def _():\n", 124 | " class Base:\n", 125 | " pass\n", 126 | "\n", 127 | "from dis import dis\n", 128 | "In [19]:\n", 129 | "dis(_) # LOAD_BUILD_CLASS\n", 130 | " 2 0 LOAD_BUILD_CLASS\n", 131 | " 2 LOAD_CONST 1 (\", line 2>)\n", 132 | " 4 LOAD_CONST 2 ('Base')\n", 133 | " 6 MAKE_FUNCTION 0\n", 134 | " 8 LOAD_CONST 2 ('Base')\n", 135 | " 10 CALL_FUNCTION 2\n", 136 | " 12 STORE_FAST 0 (Base)\n", 137 | " 14 LOAD_CONST 0 (None)\n", 138 | " 16 RETURN_VALUE\n", 139 | "In [165]:\n", 140 | "# Catch Building of Classes\n", 141 | "\n", 142 | "class Base:\n", 143 | " def foo(self):\n", 144 | " return self.bar()\n", 145 | "\n", 146 | "old_bc = __build_class__\n", 147 | "def my_bc(*a, **kw):\n", 148 | " print('my buildclass ->', a, kw)\n", 149 | " return old_bc(*a, **kw)\n", 150 | "import builtins\n", 151 | "builtins.__build_class__ = my_bc\n", 152 | "In [1]:\n", 153 | "# Catch Building of Classes\n", 154 | "\n", 155 | "class Base:\n", 156 | " def foo(self):\n", 157 | " return self.bar()\n", 158 | "\n", 159 | "old_bc = __build_class__\n", 160 | "def my_bc(fun, name, base=None, **kw):\n", 161 | " if base is Base:\n", 162 | " print('Check if bar method defined')\n", 163 | " if base is not None:\n", 164 | " return old_bc(fun, name, base, **kw)\n", 165 | " return old_bc(fun, name, **kw)\n", 166 | "\n", 167 | "import builtins\n", 168 | "builtins.__build_class__ = my_bc\n", 169 | "In [80]:\n", 170 | "import builtins\n", 171 | "In [79]:\n", 172 | "import importlib\n", 173 | "importlib.reload(builtins)\n", 174 | "Out[79]:\n", 175 | "\n", 176 | "In [2]:\n", 177 | "class BaseMeta(type):\n", 178 | " def __new__(cls, name, bases, body):\n", 179 | " print('BaseMeta.__new__', cls, name, bases, body)\n", 180 | " return super().__new__(cls, name, bases, body)\n", 181 | "\n", 182 | "class Base(metaclass=BaseMeta):\n", 183 | " def foo(self):\n", 184 | " return self.bar()\n", 185 | "BaseMeta.__new__ Base () {'__module__': '__main__', '__qualname__': 'Base', 'foo': }\n", 186 | "In [167]:\n", 187 | "class BaseMeta(type):\n", 188 | " def __new__(cls, name, bases, body):\n", 189 | " if not 'bar' in body:\n", 190 | " raise TypeError('bad user class')\n", 191 | " return super().__new__(cls, name, bases, body)\n", 192 | "\n", 193 | "class Base(metaclass=BaseMeta):\n", 194 | " def foo(self):\n", 195 | " return self.bar()\n", 196 | "my buildclass -> (, 'BaseMeta', ) {}\n", 197 | "---------------------------------------------------------------------------\n", 198 | "RecursionError Traceback (most recent call last)\n", 199 | " in ()\n", 200 | "----> 1 class BaseMeta(type):\n", 201 | " 2 def __new__(cls, name, bases, body):\n", 202 | " 3 if not 'bar' in body:\n", 203 | " 4 raise TypeError('bad user class')\n", 204 | " 5 return super().__new__(cls, name, bases, body)\n", 205 | "\n", 206 | " in my_bc(*a, **kw)\n", 207 | " 8 def my_bc(*a, **kw):\n", 208 | " 9 print('my buildclass ->', a, kw)\n", 209 | "---> 10 return old_bc(*a, **kw)\n", 210 | " 11 import builtins\n", 211 | " 12 builtins.__build_class__ = my_bc\n", 212 | "\n", 213 | " in my_bc(fun, name, base, **kw)\n", 214 | " 10 print('Check if bar method defined')\n", 215 | " 11 if base is not None:\n", 216 | "---> 12 return old_bc(fun, name, base, **kw)\n", 217 | " 13 return old_bc(fun, name, **kw)\n", 218 | " 14 \n", 219 | "\n", 220 | "... last 1 frames repeated, from the frame below ...\n", 221 | "\n", 222 | " in my_bc(fun, name, base, **kw)\n", 223 | " 10 print('Check if bar method defined')\n", 224 | " 11 if base is not None:\n", 225 | "---> 12 return old_bc(fun, name, base, **kw)\n", 226 | " 13 return old_bc(fun, name, **kw)\n", 227 | " 14 \n", 228 | "\n", 229 | "RecursionError: maximum recursion depth exceeded\n", 230 | "In [5]:\n", 231 | "class BaseMeta(type):\n", 232 | " def __new__(cls, name, bases, body):\n", 233 | " if name != 'Base' and not 'bar' in body:\n", 234 | " raise TypeError('bad user class')\n", 235 | " return super().__new__(cls, name, bases, body)\n", 236 | "\n", 237 | "class Base(metaclass=BaseMeta):\n", 238 | " def foo(self):\n", 239 | " return self.bar()\n", 240 | " \n", 241 | " def __init_subclass__(*a, **kw):\n", 242 | " print('init_subclass', a, kw)\n", 243 | " return super().__init_subclass__(*a, **kw)\n", 244 | "In [8]:\n", 245 | "help(Base.__init_subclass__)\n", 246 | "Help on method __init_subclass__ in module __main__:\n", 247 | "\n", 248 | "__init_subclass__(*a, **kw) method of __main__.BaseMeta instance\n", 249 | " This method is called when a class is subclassed.\n", 250 | " \n", 251 | " The default implementation does nothing. It may be\n", 252 | " overridden to extend subclasses.\n", 253 | "\n", 254 | "Decorators\n", 255 | "In [12]:\n", 256 | "# dec.py\n", 257 | "In [77]:\n", 258 | "def add(x, y=10):\n", 259 | " return x + y\n", 260 | "In [14]:\n", 261 | "add(10, 20)\n", 262 | "Out[14]:\n", 263 | "30\n", 264 | "In [15]:\n", 265 | "add\n", 266 | "Out[15]:\n", 267 | "\n", 268 | "In [28]:\n", 269 | "# Name of function\n", 270 | "add.__name__\n", 271 | "Out[28]:\n", 272 | "'add'\n", 273 | "In [27]:\n", 274 | "# What module function is assigned to\n", 275 | "add.__module__\n", 276 | "Out[27]:\n", 277 | "'__main__'\n", 278 | "In [26]:\n", 279 | "# Default values\n", 280 | "add.__defaults__\n", 281 | "Out[26]:\n", 282 | "(10,)\n", 283 | "In [25]:\n", 284 | "# Byte code for function\n", 285 | "add.__code__.co_code\n", 286 | "Out[25]:\n", 287 | "b'|\\x00|\\x01\\x17\\x00S\\x00'\n", 288 | "In [24]:\n", 289 | "# Variable names function interacts with\n", 290 | "add.__code__.co_varnames\n", 291 | "Out[24]:\n", 292 | "('x', 'y')\n", 293 | "What's your source code?\n", 294 | "In [31]:\n", 295 | "from inspect import getsource\n", 296 | "In [32]:\n", 297 | "getsource(add)\n", 298 | "Out[32]:\n", 299 | "'def add(x, y=10):\\n return x + y\\n'\n", 300 | "In [33]:\n", 301 | "print(getsource(add))\n", 302 | "def add(x, y=10):\n", 303 | " return x + y\n", 304 | "\n", 305 | "In [35]:\n", 306 | "# What file are you in? \n", 307 | "from inspect import getfile\n", 308 | "getfile(add)\n", 309 | "Out[35]:\n", 310 | "''\n", 311 | "In [37]:\n", 312 | "from inspect import getmodule\n", 313 | "getmodule(add)\n", 314 | "Out[37]:\n", 315 | "\n", 316 | "In [38]:\n", 317 | "print('add(10)', add(10))\n", 318 | "print('add(20, 30)', add(20, 30))\n", 319 | "print('add(\"a\", \"b\")', add(\"a\", \"b\"))\n", 320 | "add(10) 20\n", 321 | "add(20, 30) 50\n", 322 | "add(\"a\", \"b\") ab\n", 323 | "In [40]:\n", 324 | "#Count how long it took to run\n", 325 | "In [41]:\n", 326 | "def add_timer(x, y=10):\n", 327 | " before = time()\n", 328 | " rv = x + y\n", 329 | " after = time()\n", 330 | " print('elapsed:', after - before)\n", 331 | " return rv\n", 332 | "In [42]:\n", 333 | "print('add(10)', add_timer(10))\n", 334 | "print('add(20, 30)', add_timer(20, 30))\n", 335 | "print('add(\"a\", \"b\")', add_timer(\"a\", \"b\"))\n", 336 | "elapsed: 0.0\n", 337 | "add(10) 20\n", 338 | "elapsed: 9.5367431640625e-07\n", 339 | "add(20, 30) 50\n", 340 | "elapsed: 9.5367431640625e-07\n", 341 | "add(\"a\", \"b\") ab\n", 342 | "But what if we have multiple functions that require timing?\n", 343 | "In [49]:\n", 344 | "def sub(x, y=10):\n", 345 | " return x - y\n", 346 | "In [46]:\n", 347 | "print('sub(10)', sub(10))\n", 348 | "print('sub(20, 30)', sub(20, 30))\n", 349 | "sub(10) 0\n", 350 | "sub(20, 30) -10\n", 351 | "In [55]:\n", 352 | "def timer(func, x, y=10):\n", 353 | " before = time()\n", 354 | " rv = func(x, y)\n", 355 | " after = time()\n", 356 | " print('elapsed', after - before)\n", 357 | " return rv\n", 358 | "In [56]:\n", 359 | "print('add(10)', timer(add, 10))\n", 360 | "print('add(20, 30)', timer(add, 20, 30))\n", 361 | "print('add(\"a\", \"b\")', timer(add, \"a\", \"b\"))\n", 362 | "elapsed 9.5367431640625e-07\n", 363 | "add(10) 20\n", 364 | "elapsed 9.5367431640625e-07\n", 365 | "add(20, 30) 50\n", 366 | "elapsed 9.5367431640625e-07\n", 367 | "add(\"a\", \"b\") ab\n", 368 | "In [74]:\n", 369 | "def timer(func):\n", 370 | " def f(x, y=10):\n", 371 | " before = time()\n", 372 | " rv = func(x, y)\n", 373 | " after = time()\n", 374 | " print('elapsed', after - before)\n", 375 | " return rv\n", 376 | " return f\n", 377 | "In [78]:\n", 378 | "add = timer(add)\n", 379 | "In [79]:\n", 380 | "print('add(10)', add(10))\n", 381 | "print('add(20, 30)', add(20, 30))\n", 382 | "print('add(\"a\", \"b\")', add(\"a\", \"b\"))\n", 383 | "elapsed 9.5367431640625e-07\n", 384 | "add(10) 20\n", 385 | "elapsed 9.5367431640625e-07\n", 386 | "add(20, 30) 50\n", 387 | "elapsed 9.5367431640625e-07\n", 388 | "add(\"a\", \"b\") ab\n", 389 | "In [80]:\n", 390 | "# Don't need to do add = timer(add) with decorators...\n", 391 | "In [87]:\n", 392 | "@timer\n", 393 | "def add_dec(x, y=10):\n", 394 | " return x + y\n", 395 | "\n", 396 | "@timer\n", 397 | "def sub_dec(x, y=10):\n", 398 | " return x - y\n", 399 | "In [88]:\n", 400 | "print('add(10)', add_dec(10))\n", 401 | "print('add(20, 30)', add_dec(20, 30))\n", 402 | "print('add(\"a\", \"b\")', add_dec(\"a\", \"b\"))\n", 403 | "print('sub(10)', sub_dec(10))\n", 404 | "print('sub(20, 30)', sub_dec(20, 30))\n", 405 | "elapsed 9.5367431640625e-07\n", 406 | "add(10) 20\n", 407 | "elapsed 1.1920928955078125e-06\n", 408 | "add(20, 30) 50\n", 409 | "elapsed 1.1920928955078125e-06\n", 410 | "add(\"a\", \"b\") ab\n", 411 | "elapsed 9.5367431640625e-07\n", 412 | "sub(10) 0\n", 413 | "elapsed 0.0\n", 414 | "sub(20, 30) -10\n", 415 | "In [91]:\n", 416 | "# Don't hardcode parameters in decorator functions\n", 417 | "def timer_k(func):\n", 418 | " def f(*args, **kwargs):\n", 419 | " before = time()\n", 420 | " rv = func(*args, **kwargs)\n", 421 | " after = time()\n", 422 | " print('elapsed', after - before)\n", 423 | " return rv\n", 424 | " return f\n", 425 | "In [92]:\n", 426 | "@timer_k\n", 427 | "def add_dec(x, y=10):\n", 428 | " return x + y\n", 429 | "\n", 430 | "@timer_k\n", 431 | "def sub_dec(x, y=10):\n", 432 | " return x - y\n", 433 | "\n", 434 | "print('add(10)', add_dec(10))\n", 435 | "print('add(20, 30)', add_dec(20, 30))\n", 436 | "print('add(\"a\", \"b\")', add_dec(\"a\", \"b\"))\n", 437 | "print('sub(10)', sub_dec(10))\n", 438 | "print('sub(20, 30)', sub_dec(20, 30))\n", 439 | "elapsed 9.5367431640625e-07\n", 440 | "add(10) 20\n", 441 | "elapsed 9.5367431640625e-07\n", 442 | "add(20, 30) 50\n", 443 | "elapsed 9.5367431640625e-07\n", 444 | "add(\"a\", \"b\") ab\n", 445 | "elapsed 0.0\n", 446 | "sub(10) 0\n", 447 | "elapsed 9.5367431640625e-07\n", 448 | "sub(20, 30) -10\n", 449 | "In [93]:\n", 450 | "# What if I want to run a function n number of times\n", 451 | "In [94]:\n", 452 | "# Let's have add run 3 times in a row and sub run twice in a row\n", 453 | "In [97]:\n", 454 | "n = 2\n", 455 | "\n", 456 | "def ntimes(f):\n", 457 | " def wrapper(*args, **kwargs):\n", 458 | " for _ in range(n):\n", 459 | " print('running {.__name__}'.format(f))\n", 460 | " rv = f(*args, **kwargs)\n", 461 | " return rv\n", 462 | " return wrapper\n", 463 | " \n", 464 | " \n", 465 | "@ntimes\n", 466 | "def add_dec(x, y=10):\n", 467 | " return x + y\n", 468 | "\n", 469 | "@ntimes\n", 470 | "def sub_dec(x, y=10):\n", 471 | " return x - y\n", 472 | "In [98]:\n", 473 | "print('add(10)', add_dec(10))\n", 474 | "print('add(20, 30)', add_dec(20, 30))\n", 475 | "print('add(\"a\", \"b\")', add_dec(\"a\", \"b\"))\n", 476 | "print('sub(10)', sub_dec(10))\n", 477 | "print('sub(20, 30)', sub_dec(20, 30))\n", 478 | "running add_dec\n", 479 | "running add_dec\n", 480 | "add(10) 20\n", 481 | "running add_dec\n", 482 | "running add_dec\n", 483 | "add(20, 30) 50\n", 484 | "running add_dec\n", 485 | "running add_dec\n", 486 | "add(\"a\", \"b\") ab\n", 487 | "running sub_dec\n", 488 | "running sub_dec\n", 489 | "sub(10) 0\n", 490 | "running sub_dec\n", 491 | "running sub_dec\n", 492 | "sub(20, 30) -10\n", 493 | "Higher Order Decorators\n", 494 | "In [103]:\n", 495 | "def ntimes(n):\n", 496 | " def inner(f):\n", 497 | " def wrapper(*args, **kwargs):\n", 498 | " for _ in range(n):\n", 499 | " print('running {.__name__}'.format(f))\n", 500 | " rv = f(*args, **kwargs)\n", 501 | " return rv\n", 502 | " return wrapper\n", 503 | " return inner\n", 504 | " \n", 505 | " \n", 506 | "@ntimes(2)\n", 507 | "def add_hdec(x, y=10):\n", 508 | " return x + y\n", 509 | "\n", 510 | "@ntimes(4)\n", 511 | "def sub_hdec(x, y=10):\n", 512 | " return x - y\n", 513 | "In [104]:\n", 514 | "print('add(10)', add_hdec(10))\n", 515 | "print('add(20, 30)', add_hdec(20, 30))\n", 516 | "print('add(\"a\", \"b\")', add_hdec(\"a\", \"b\"))\n", 517 | "print('sub(10)', sub_hdec(10))\n", 518 | "print('sub(20, 30)', sub_hdec(20, 30))\n", 519 | "running add_hdec\n", 520 | "running add_hdec\n", 521 | "add(10) 20\n", 522 | "running add_hdec\n", 523 | "running add_hdec\n", 524 | "add(20, 30) 50\n", 525 | "running add_hdec\n", 526 | "running add_hdec\n", 527 | "add(\"a\", \"b\") ab\n", 528 | "running sub_hdec\n", 529 | "running sub_hdec\n", 530 | "running sub_hdec\n", 531 | "running sub_hdec\n", 532 | "sub(10) 0\n", 533 | "running sub_hdec\n", 534 | "running sub_hdec\n", 535 | "running sub_hdec\n", 536 | "running sub_hdec\n", 537 | "sub(20, 30) -10\n", 538 | "Generators\n", 539 | "In [105]:\n", 540 | "# gen.py - use whenever sequencing is needd\n", 541 | "In [109]:\n", 542 | "# top-level syntax, function -> underscore method\n", 543 | "# x() __call__\n", 544 | "\n", 545 | "def add1(x, y):\n", 546 | " return x + y\n", 547 | "\n", 548 | "class Adder:\n", 549 | " def __call__(self, x, y):\n", 550 | " return x + y\n", 551 | "add2 = Adder()\n", 552 | "In [110]:\n", 553 | "add1(10, 20)\n", 554 | "Out[110]:\n", 555 | "30\n", 556 | "In [111]:\n", 557 | "add2(10, 20)\n", 558 | "Out[111]:\n", 559 | "30\n", 560 | "In [113]:\n", 561 | "# top-level syntax, function -> underscore method\n", 562 | "# x() __call__\n", 563 | "\n", 564 | "def add1(x, y):\n", 565 | " return x + y\n", 566 | "\n", 567 | "class Adder:\n", 568 | " def __init__(self):\n", 569 | " self.z = 0\n", 570 | " \n", 571 | " def __call__(self, x, y):\n", 572 | " self.z += 1\n", 573 | " return x + y + self.z\n", 574 | "\n", 575 | "add2 = Adder()\n", 576 | "In [130]:\n", 577 | "from time import sleep\n", 578 | "# This example has storage... and has eager return of the result sets\n", 579 | "def compute():\n", 580 | " rv = []\n", 581 | " for i in range(10):\n", 582 | " sleep(.5)\n", 583 | " rv.append(i)\n", 584 | " return rv\n", 585 | "In [120]:\n", 586 | "compute()\n", 587 | "Out[120]:\n", 588 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 589 | "Wasteful because we have to wait for the entire action to complete and be read into memory, when we really just care about each number (one by one)\n", 590 | "\n", 591 | "In [129]:\n", 592 | "class Compute:\n", 593 | " def __call__(self):\n", 594 | " rv = []\n", 595 | " for i in range(100000):\n", 596 | " sleep(5)\n", 597 | " rv.append(i)\n", 598 | " return rv\n", 599 | " \n", 600 | " def __iter__(self):\n", 601 | " self.last = 0\n", 602 | " return self\n", 603 | " \n", 604 | " def __next__(self):\n", 605 | " rv = self.last\n", 606 | " self.last += 1\n", 607 | " if self.last > 10:\n", 608 | " raise StopIteration()\n", 609 | " sleep(.5)\n", 610 | " return self.last\n", 611 | " \n", 612 | "compute = Compute()\n", 613 | "\n", 614 | "# THIS IS UGLY... now let's make a generator\n", 615 | "In [131]:\n", 616 | "#This is a generator... don't eagerly compute. Return to user as they ask for it...\n", 617 | "\n", 618 | "def compute():\n", 619 | " for i in range(10):\n", 620 | " sleep(.5)\n", 621 | " yield i\n", 622 | "In [125]:\n", 623 | "# for x in xs:\n", 624 | "# pass\n", 625 | "\n", 626 | "# xi = iter(xs) -> __iter__\n", 627 | "# while True:\n", 628 | "# x = next(xi) -> __next__\n", 629 | "In [136]:\n", 630 | "for val in compute():\n", 631 | " print(val)\n", 632 | "0\n", 633 | "1\n", 634 | "2\n", 635 | "3\n", 636 | "4\n", 637 | "5\n", 638 | "6\n", 639 | "7\n", 640 | "8\n", 641 | "9\n", 642 | "In [133]:\n", 643 | "class Api:\n", 644 | " def run_this_first(self):\n", 645 | " first()\n", 646 | " def run_this_second(self):\n", 647 | " second()\n", 648 | " def run_this_last(self):\n", 649 | " last()\n", 650 | "In [137]:\n", 651 | "def api():\n", 652 | " first()\n", 653 | " yield\n", 654 | " second()\n", 655 | " yield\n", 656 | " last()\n", 657 | "Context Manager\n", 658 | "In [2]:\n", 659 | "# cty.py\n", 660 | "\n", 661 | "from sqlite3 import connect\n", 662 | "In [156]:\n", 663 | "# with ctx() as x:\n", 664 | "# pass\n", 665 | "\n", 666 | "# x = ctx().__enter__\n", 667 | "# try:\n", 668 | "# pass\n", 669 | "# finally:\n", 670 | "# x.__exit__\n", 671 | "\n", 672 | "class temptable:\n", 673 | " def __init__(self, cur):\n", 674 | " self.cur = cur\n", 675 | " def __enter__(self):\n", 676 | " print('__enter__')\n", 677 | " self.cur.execute('create table points(x int, y int)')\n", 678 | " def __exit__(self, *args):\n", 679 | " print('__exit__')\n", 680 | " self.cur.execute('drop table points')\n", 681 | "\n", 682 | "with connect('test.db') as conn:\n", 683 | " cur = conn.cursor()\n", 684 | " with temptable(cur):\n", 685 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 686 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 687 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 688 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 689 | " for row in cur.execute(\"select x, y from points\"):\n", 690 | " print(row)\n", 691 | " for row in cur.execute('select sum(x * y) from points'):\n", 692 | " print(row)\n", 693 | "__enter__\n", 694 | "(1, 1)\n", 695 | "(1, 2)\n", 696 | "(2, 1)\n", 697 | "(2, 2)\n", 698 | "(9,)\n", 699 | "__exit__\n", 700 | "In [162]:\n", 701 | "rm test.db\n", 702 | "In [164]:\n", 703 | "def temptable(cur):\n", 704 | " cur.execute('create table points(x int, y int)')\n", 705 | " print('created table')\n", 706 | " yield\n", 707 | " cur.execute('drop table points')\n", 708 | " print('dropped table')\n", 709 | " \n", 710 | "class contextmanager:\n", 711 | " def __init__(self, cur):\n", 712 | " self.cur = cur\n", 713 | " def __enter__(self):\n", 714 | " self.gen = temptable(self.cur)\n", 715 | " next(self.gen)\n", 716 | " def __exit__(self, *args):\n", 717 | " next(self.gen, None)\n", 718 | " \n", 719 | "with connect('test.db') as conn:\n", 720 | " cur = conn.cursor()\n", 721 | " with contextmanager(cur):\n", 722 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 723 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 724 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 725 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 726 | " for row in cur.execute(\"select x, y from points\"):\n", 727 | " print(row)\n", 728 | " for row in cur.execute('select sum(x * y) from points'):\n", 729 | " print(row)\n", 730 | "created table\n", 731 | "(1, 1)\n", 732 | "(1, 2)\n", 733 | "(2, 1)\n", 734 | "(2, 2)\n", 735 | "(9,)\n", 736 | "dropped table\n", 737 | "In [8]:\n", 738 | "class contextmanager:\n", 739 | " def __init__(self, gen):\n", 740 | " self.gen = gen\n", 741 | " def __call__(self, *args, **kwargs):\n", 742 | " self.args, self.kwargs = args, kwargs\n", 743 | " return self\n", 744 | " def __enter__(self):\n", 745 | " self.gen_inst = self.gen(*self.args, **self.kwargs)\n", 746 | " next(self.gen_inst)\n", 747 | " def __exit__(self, *args):\n", 748 | " next(self.gen_inst, None)\n", 749 | "\n", 750 | "def temptable(cur):\n", 751 | " cur.execute('create table points(x int, y int)')\n", 752 | " print('created table')\n", 753 | " yield\n", 754 | " cur.execute('drop table points')\n", 755 | " print('dropped table')\n", 756 | "temptable = contextmanager(temptable)\n", 757 | " \n", 758 | "with connect('test.db') as conn:\n", 759 | " cur = conn.cursor()\n", 760 | " with temptable(cur):\n", 761 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 762 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 763 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 764 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 765 | " for row in cur.execute(\"select x, y from points\"):\n", 766 | " print(row)\n", 767 | "created table\n", 768 | "(1, 1)\n", 769 | "(1, 2)\n", 770 | "(2, 1)\n", 771 | "(2, 2)\n", 772 | "dropped table\n", 773 | "In [12]:\n", 774 | "class contextmanager:\n", 775 | " def __init__(self, gen):\n", 776 | " self.gen = gen\n", 777 | " def __call__(self, *args, **kwargs):\n", 778 | " self.args, self.kwargs = args, kwargs\n", 779 | " return self\n", 780 | " def __enter__(self):\n", 781 | " self.gen_inst = self.gen(*self.args, **self.kwargs)\n", 782 | " next(self.gen_inst)\n", 783 | " def __exit__(self, *args):\n", 784 | " next(self.gen_inst, None)\n", 785 | " \n", 786 | "@contextmanager\n", 787 | "def temptable(cur):\n", 788 | " cur.execute('create table points(x int, y int)')\n", 789 | " print('created table')\n", 790 | " yield\n", 791 | " cur.execute('drop table points')\n", 792 | " print('dropped table')\n", 793 | " \n", 794 | "with connect('test.db') as conn:\n", 795 | " cur = conn.cursor()\n", 796 | " with temptable(cur):\n", 797 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 798 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 799 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 800 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 801 | " for row in cur.execute(\"select x, y from points\"):\n", 802 | " print(row)\n", 803 | "created table\n", 804 | "(1, 1)\n", 805 | "(1, 2)\n", 806 | "(2, 1)\n", 807 | "(2, 2)\n", 808 | "dropped table\n", 809 | "In [15]:\n", 810 | "from sqlite3 import connect\n", 811 | "from contextlib import contextmanager\n", 812 | " \n", 813 | "@contextmanager\n", 814 | "def temptable(cur):\n", 815 | " cur.execute('create table points(x int, y int)')\n", 816 | " print('created table')\n", 817 | " try:\n", 818 | " yield\n", 819 | " finally:\n", 820 | " cur.execute('drop table points')\n", 821 | " print('dropped table')\n", 822 | " \n", 823 | "with connect('test.db') as conn:\n", 824 | " cur = conn.cursor()\n", 825 | " with temptable(cur):\n", 826 | " cur.execute('insert into points (x, y) values(1, 1)')\n", 827 | " cur.execute('insert into points (x, y) values(1, 2)')\n", 828 | " cur.execute('insert into points (x, y) values(2, 1)')\n", 829 | " cur.execute('insert into points (x, y) values(2, 2)')\n", 830 | " for row in cur.execute(\"select x, y from points\"):\n", 831 | " print(row)\n", 832 | "created table\n", 833 | "(1, 1)\n", 834 | "(1, 2)\n", 835 | "(2, 1)\n", 836 | "(2, 2)\n", 837 | "dropped table" 838 | ] 839 | }, 840 | { 841 | "cell_type": "code", 842 | "execution_count": null, 843 | "metadata": {}, 844 | "outputs": [], 845 | "source": [] 846 | } 847 | ], 848 | "metadata": { 849 | "kernelspec": { 850 | "display_name": "Python 2", 851 | "language": "python", 852 | "name": "python2" 853 | }, 854 | "language_info": { 855 | "codemirror_mode": { 856 | "name": "ipython", 857 | "version": 2 858 | }, 859 | "file_extension": ".py", 860 | "mimetype": "text/x-python", 861 | "name": "python", 862 | "nbconvert_exporter": "python", 863 | "pygments_lexer": "ipython2", 864 | "version": "2.7.15" 865 | } 866 | }, 867 | "nbformat": 4, 868 | "nbformat_minor": 2 869 | } 870 | -------------------------------------------------------------------------------- /headbrain.csv: -------------------------------------------------------------------------------- 1 | Gender,Age Range,Head Size(cm^3),Brain Weight(grams) 2 | 1,1,4512,1530 3 | 1,1,3738,1297 4 | 1,1,4261,1335 5 | 1,1,3777,1282 6 | 1,1,4177,1590 7 | 1,1,3585,1300 8 | 1,1,3785,1400 9 | 1,1,3559,1255 10 | 1,1,3613,1355 11 | 1,1,3982,1375 12 | 1,1,3443,1340 13 | 1,1,3993,1380 14 | 1,1,3640,1355 15 | 1,1,4208,1522 16 | 1,1,3832,1208 17 | 1,1,3876,1405 18 | 1,1,3497,1358 19 | 1,1,3466,1292 20 | 1,1,3095,1340 21 | 1,1,4424,1400 22 | 1,1,3878,1357 23 | 1,1,4046,1287 24 | 1,1,3804,1275 25 | 1,1,3710,1270 26 | 1,1,4747,1635 27 | 1,1,4423,1505 28 | 1,1,4036,1490 29 | 1,1,4022,1485 30 | 1,1,3454,1310 31 | 1,1,4175,1420 32 | 1,1,3787,1318 33 | 1,1,3796,1432 34 | 1,1,4103,1364 35 | 1,1,4161,1405 36 | 1,1,4158,1432 37 | 1,1,3814,1207 38 | 1,1,3527,1375 39 | 1,1,3748,1350 40 | 1,1,3334,1236 41 | 1,1,3492,1250 42 | 1,1,3962,1350 43 | 1,1,3505,1320 44 | 1,1,4315,1525 45 | 1,1,3804,1570 46 | 1,1,3863,1340 47 | 1,1,4034,1422 48 | 1,1,4308,1506 49 | 1,1,3165,1215 50 | 1,1,3641,1311 51 | 1,1,3644,1300 52 | 1,1,3891,1224 53 | 1,1,3793,1350 54 | 1,1,4270,1335 55 | 1,1,4063,1390 56 | 1,1,4012,1400 57 | 1,1,3458,1225 58 | 1,1,3890,1310 59 | 1,2,4166,1560 60 | 1,2,3935,1330 61 | 1,2,3669,1222 62 | 1,2,3866,1415 63 | 1,2,3393,1175 64 | 1,2,4442,1330 65 | 1,2,4253,1485 66 | 1,2,3727,1470 67 | 1,2,3329,1135 68 | 1,2,3415,1310 69 | 1,2,3372,1154 70 | 1,2,4430,1510 71 | 1,2,4381,1415 72 | 1,2,4008,1468 73 | 1,2,3858,1390 74 | 1,2,4121,1380 75 | 1,2,4057,1432 76 | 1,2,3824,1240 77 | 1,2,3394,1195 78 | 1,2,3558,1225 79 | 1,2,3362,1188 80 | 1,2,3930,1252 81 | 1,2,3835,1315 82 | 1,2,3830,1245 83 | 1,2,3856,1430 84 | 1,2,3249,1279 85 | 1,2,3577,1245 86 | 1,2,3933,1309 87 | 1,2,3850,1412 88 | 1,2,3309,1120 89 | 1,2,3406,1220 90 | 1,2,3506,1280 91 | 1,2,3907,1440 92 | 1,2,4160,1370 93 | 1,2,3318,1192 94 | 1,2,3662,1230 95 | 1,2,3899,1346 96 | 1,2,3700,1290 97 | 1,2,3779,1165 98 | 1,2,3473,1240 99 | 1,2,3490,1132 100 | 1,2,3654,1242 101 | 1,2,3478,1270 102 | 1,2,3495,1218 103 | 1,2,3834,1430 104 | 1,2,3876,1588 105 | 1,2,3661,1320 106 | 1,2,3618,1290 107 | 1,2,3648,1260 108 | 1,2,4032,1425 109 | 1,2,3399,1226 110 | 1,2,3916,1360 111 | 1,2,4430,1620 112 | 1,2,3695,1310 113 | 1,2,3524,1250 114 | 1,2,3571,1295 115 | 1,2,3594,1290 116 | 1,2,3383,1290 117 | 1,2,3499,1275 118 | 1,2,3589,1250 119 | 1,2,3900,1270 120 | 1,2,4114,1362 121 | 1,2,3937,1300 122 | 1,2,3399,1173 123 | 1,2,4200,1256 124 | 1,2,4488,1440 125 | 1,2,3614,1180 126 | 1,2,4051,1306 127 | 1,2,3782,1350 128 | 1,2,3391,1125 129 | 1,2,3124,1165 130 | 1,2,4053,1312 131 | 1,2,3582,1300 132 | 1,2,3666,1270 133 | 1,2,3532,1335 134 | 1,2,4046,1450 135 | 1,2,3667,1310 136 | 2,1,2857,1027 137 | 2,1,3436,1235 138 | 2,1,3791,1260 139 | 2,1,3302,1165 140 | 2,1,3104,1080 141 | 2,1,3171,1127 142 | 2,1,3572,1270 143 | 2,1,3530,1252 144 | 2,1,3175,1200 145 | 2,1,3438,1290 146 | 2,1,3903,1334 147 | 2,1,3899,1380 148 | 2,1,3401,1140 149 | 2,1,3267,1243 150 | 2,1,3451,1340 151 | 2,1,3090,1168 152 | 2,1,3413,1322 153 | 2,1,3323,1249 154 | 2,1,3680,1321 155 | 2,1,3439,1192 156 | 2,1,3853,1373 157 | 2,1,3156,1170 158 | 2,1,3279,1265 159 | 2,1,3707,1235 160 | 2,1,4006,1302 161 | 2,1,3269,1241 162 | 2,1,3071,1078 163 | 2,1,3779,1520 164 | 2,1,3548,1460 165 | 2,1,3292,1075 166 | 2,1,3497,1280 167 | 2,1,3082,1180 168 | 2,1,3248,1250 169 | 2,1,3358,1190 170 | 2,1,3803,1374 171 | 2,1,3566,1306 172 | 2,1,3145,1202 173 | 2,1,3503,1240 174 | 2,1,3571,1316 175 | 2,1,3724,1280 176 | 2,1,3615,1350 177 | 2,1,3203,1180 178 | 2,1,3609,1210 179 | 2,1,3561,1127 180 | 2,1,3979,1324 181 | 2,1,3533,1210 182 | 2,1,3689,1290 183 | 2,1,3158,1100 184 | 2,1,4005,1280 185 | 2,1,3181,1175 186 | 2,1,3479,1160 187 | 2,1,3642,1205 188 | 2,1,3632,1163 189 | 2,2,3069,1022 190 | 2,2,3394,1243 191 | 2,2,3703,1350 192 | 2,2,3165,1237 193 | 2,2,3354,1204 194 | 2,2,3000,1090 195 | 2,2,3687,1355 196 | 2,2,3556,1250 197 | 2,2,2773,1076 198 | 2,2,3058,1120 199 | 2,2,3344,1220 200 | 2,2,3493,1240 201 | 2,2,3297,1220 202 | 2,2,3360,1095 203 | 2,2,3228,1235 204 | 2,2,3277,1105 205 | 2,2,3851,1405 206 | 2,2,3067,1150 207 | 2,2,3692,1305 208 | 2,2,3402,1220 209 | 2,2,3995,1296 210 | 2,2,3318,1175 211 | 2,2,2720,955 212 | 2,2,2937,1070 213 | 2,2,3580,1320 214 | 2,2,2939,1060 215 | 2,2,2989,1130 216 | 2,2,3586,1250 217 | 2,2,3156,1225 218 | 2,2,3246,1180 219 | 2,2,3170,1178 220 | 2,2,3268,1142 221 | 2,2,3389,1130 222 | 2,2,3381,1185 223 | 2,2,2864,1012 224 | 2,2,3740,1280 225 | 2,2,3479,1103 226 | 2,2,3647,1408 227 | 2,2,3716,1300 228 | 2,2,3284,1246 229 | 2,2,4204,1380 230 | 2,2,3735,1350 231 | 2,2,3218,1060 232 | 2,2,3685,1350 233 | 2,2,3704,1220 234 | 2,2,3214,1110 235 | 2,2,3394,1215 236 | 2,2,3233,1104 237 | 2,2,3352,1170 238 | 2,2,3391,1120 239 | -------------------------------------------------------------------------------- /iris.csv: -------------------------------------------------------------------------------- 1 | sepal_length,sepal_width,petal_length,petal_width,species 2 | 5.1,3.5,1.4,0.2,setosa 3 | 4.9,3.0,1.4,0.2,setosa 4 | 4.7,3.2,1.3,0.2,setosa 5 | 4.6,3.1,1.5,0.2,setosa 6 | 5.0,3.6,1.4,0.2,setosa 7 | 5.4,3.9,1.7,0.4,setosa 8 | 4.6,3.4,1.4,0.3,setosa 9 | 5.0,3.4,1.5,0.2,setosa 10 | 4.4,2.9,1.4,0.2,setosa 11 | 4.9,3.1,1.5,0.1,setosa 12 | 5.4,3.7,1.5,0.2,setosa 13 | 4.8,3.4,1.6,0.2,setosa 14 | 4.8,3.0,1.4,0.1,setosa 15 | 4.3,3.0,1.1,0.1,setosa 16 | 5.8,4.0,1.2,0.2,setosa 17 | 5.7,4.4,1.5,0.4,setosa 18 | 5.4,3.9,1.3,0.4,setosa 19 | 5.1,3.5,1.4,0.3,setosa 20 | 5.7,3.8,1.7,0.3,setosa 21 | 5.1,3.8,1.5,0.3,setosa 22 | 5.4,3.4,1.7,0.2,setosa 23 | 5.1,3.7,1.5,0.4,setosa 24 | 4.6,3.6,1.0,0.2,setosa 25 | 5.1,3.3,1.7,0.5,setosa 26 | 4.8,3.4,1.9,0.2,setosa 27 | 5.0,3.0,1.6,0.2,setosa 28 | 5.0,3.4,1.6,0.4,setosa 29 | 5.2,3.5,1.5,0.2,setosa 30 | 5.2,3.4,1.4,0.2,setosa 31 | 4.7,3.2,1.6,0.2,setosa 32 | 4.8,3.1,1.6,0.2,setosa 33 | 5.4,3.4,1.5,0.4,setosa 34 | 5.2,4.1,1.5,0.1,setosa 35 | 5.5,4.2,1.4,0.2,setosa 36 | 4.9,3.1,1.5,0.1,setosa 37 | 5.0,3.2,1.2,0.2,setosa 38 | 5.5,3.5,1.3,0.2,setosa 39 | 4.9,3.1,1.5,0.1,setosa 40 | 4.4,3.0,1.3,0.2,setosa 41 | 5.1,3.4,1.5,0.2,setosa 42 | 5.0,3.5,1.3,0.3,setosa 43 | 4.5,2.3,1.3,0.3,setosa 44 | 4.4,3.2,1.3,0.2,setosa 45 | 5.0,3.5,1.6,0.6,setosa 46 | 5.1,3.8,1.9,0.4,setosa 47 | 4.8,3.0,1.4,0.3,setosa 48 | 5.1,3.8,1.6,0.2,setosa 49 | 4.6,3.2,1.4,0.2,setosa 50 | 5.3,3.7,1.5,0.2,setosa 51 | 5.0,3.3,1.4,0.2,setosa 52 | 7.0,3.2,4.7,1.4,versicolor 53 | 6.4,3.2,4.5,1.5,versicolor 54 | 6.9,3.1,4.9,1.5,versicolor 55 | 5.5,2.3,4.0,1.3,versicolor 56 | 6.5,2.8,4.6,1.5,versicolor 57 | 5.7,2.8,4.5,1.3,versicolor 58 | 6.3,3.3,4.7,1.6,versicolor 59 | 4.9,2.4,3.3,1.0,versicolor 60 | 6.6,2.9,4.6,1.3,versicolor 61 | 5.2,2.7,3.9,1.4,versicolor 62 | 5.0,2.0,3.5,1.0,versicolor 63 | 5.9,3.0,4.2,1.5,versicolor 64 | 6.0,2.2,4.0,1.0,versicolor 65 | 6.1,2.9,4.7,1.4,versicolor 66 | 5.6,2.9,3.6,1.3,versicolor 67 | 6.7,3.1,4.4,1.4,versicolor 68 | 5.6,3.0,4.5,1.5,versicolor 69 | 5.8,2.7,4.1,1.0,versicolor 70 | 6.2,2.2,4.5,1.5,versicolor 71 | 5.6,2.5,3.9,1.1,versicolor 72 | 5.9,3.2,4.8,1.8,versicolor 73 | 6.1,2.8,4.0,1.3,versicolor 74 | 6.3,2.5,4.9,1.5,versicolor 75 | 6.1,2.8,4.7,1.2,versicolor 76 | 6.4,2.9,4.3,1.3,versicolor 77 | 6.6,3.0,4.4,1.4,versicolor 78 | 6.8,2.8,4.8,1.4,versicolor 79 | 6.7,3.0,5.0,1.7,versicolor 80 | 6.0,2.9,4.5,1.5,versicolor 81 | 5.7,2.6,3.5,1.0,versicolor 82 | 5.5,2.4,3.8,1.1,versicolor 83 | 5.5,2.4,3.7,1.0,versicolor 84 | 5.8,2.7,3.9,1.2,versicolor 85 | 6.0,2.7,5.1,1.6,versicolor 86 | 5.4,3.0,4.5,1.5,versicolor 87 | 6.0,3.4,4.5,1.6,versicolor 88 | 6.7,3.1,4.7,1.5,versicolor 89 | 6.3,2.3,4.4,1.3,versicolor 90 | 5.6,3.0,4.1,1.3,versicolor 91 | 5.5,2.5,4.0,1.3,versicolor 92 | 5.5,2.6,4.4,1.2,versicolor 93 | 6.1,3.0,4.6,1.4,versicolor 94 | 5.8,2.6,4.0,1.2,versicolor 95 | 5.0,2.3,3.3,1.0,versicolor 96 | 5.6,2.7,4.2,1.3,versicolor 97 | 5.7,3.0,4.2,1.2,versicolor 98 | 5.7,2.9,4.2,1.3,versicolor 99 | 6.2,2.9,4.3,1.3,versicolor 100 | 5.1,2.5,3.0,1.1,versicolor 101 | 5.7,2.8,4.1,1.3,versicolor 102 | 6.3,3.3,6.0,2.5,virginica 103 | 5.8,2.7,5.1,1.9,virginica 104 | 7.1,3.0,5.9,2.1,virginica 105 | 6.3,2.9,5.6,1.8,virginica 106 | 6.5,3.0,5.8,2.2,virginica 107 | 7.6,3.0,6.6,2.1,virginica 108 | 4.9,2.5,4.5,1.7,virginica 109 | 7.3,2.9,6.3,1.8,virginica 110 | 6.7,2.5,5.8,1.8,virginica 111 | 7.2,3.6,6.1,2.5,virginica 112 | 6.5,3.2,5.1,2.0,virginica 113 | 6.4,2.7,5.3,1.9,virginica 114 | 6.8,3.0,5.5,2.1,virginica 115 | 5.7,2.5,5.0,2.0,virginica 116 | 5.8,2.8,5.1,2.4,virginica 117 | 6.4,3.2,5.3,2.3,virginica 118 | 6.5,3.0,5.5,1.8,virginica 119 | 7.7,3.8,6.7,2.2,virginica 120 | 7.7,2.6,6.9,2.3,virginica 121 | 6.0,2.2,5.0,1.5,virginica 122 | 6.9,3.2,5.7,2.3,virginica 123 | 5.6,2.8,4.9,2.0,virginica 124 | 7.7,2.8,6.7,2.0,virginica 125 | 6.3,2.7,4.9,1.8,virginica 126 | 6.7,3.3,5.7,2.1,virginica 127 | 7.2,3.2,6.0,1.8,virginica 128 | 6.2,2.8,4.8,1.8,virginica 129 | 6.1,3.0,4.9,1.8,virginica 130 | 6.4,2.8,5.6,2.1,virginica 131 | 7.2,3.0,5.8,1.6,virginica 132 | 7.4,2.8,6.1,1.9,virginica 133 | 7.9,3.8,6.4,2.0,virginica 134 | 6.4,2.8,5.6,2.2,virginica 135 | 6.3,2.8,5.1,1.5,virginica 136 | 6.1,2.6,5.6,1.4,virginica 137 | 7.7,3.0,6.1,2.3,virginica 138 | 6.3,3.4,5.6,2.4,virginica 139 | 6.4,3.1,5.5,1.8,virginica 140 | 6.0,3.0,4.8,1.8,virginica 141 | 6.9,3.1,5.4,2.1,virginica 142 | 6.7,3.1,5.6,2.4,virginica 143 | 6.9,3.1,5.1,2.3,virginica 144 | 5.8,2.7,5.1,1.9,virginica 145 | 6.8,3.2,5.9,2.3,virginica 146 | 6.7,3.3,5.7,2.5,virginica 147 | 6.7,3.0,5.2,2.3,virginica 148 | 6.3,2.5,5.0,1.9,virginica 149 | 6.5,3.0,5.2,2.0,virginica 150 | 6.2,3.4,5.4,2.3,virginica 151 | 5.9,3.0,5.1,1.8,virginica 152 | -------------------------------------------------------------------------------- /linear regression practice.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Linear Regression" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 152, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "import pandas as pd\n", 18 | "import matplotlib.pyplot as plt" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 153, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "data = pd.read_csv('headbrain.csv')" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 154, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/html": [ 38 | "
\n", 39 | "\n", 52 | "\n", 53 | " \n", 54 | " \n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | "
GenderAge RangeHead Size(cm^3)Brain Weight(grams)
01145121530
11137381297
21142611335
31137771282
41141771590
\n", 100 | "
" 101 | ], 102 | "text/plain": [ 103 | " Gender Age Range Head Size(cm^3) Brain Weight(grams)\n", 104 | "0 1 1 4512 1530\n", 105 | "1 1 1 3738 1297\n", 106 | "2 1 1 4261 1335\n", 107 | "3 1 1 3777 1282\n", 108 | "4 1 1 4177 1590" 109 | ] 110 | }, 111 | "execution_count": 154, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "data.head(5)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 155, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "(237, 4)" 129 | ] 130 | }, 131 | "execution_count": 155, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "data.shape" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 156, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "data": { 147 | "text/plain": [ 148 | "Index([u'Gender', u'Age Range', u'Head Size(cm^3)', u'Brain Weight(grams)'], dtype='object')" 149 | ] 150 | }, 151 | "execution_count": 156, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "data.columns" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "collecting x and y values" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 157, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "x = data['Head Size(cm^3)'].values\n", 174 | "y = data['Brain Weight(grams)'].values" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 158, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "data": { 184 | "text/plain": [ 185 | "array([4512, 3738, 4261, 3777, 4177, 3585, 3785, 3559, 3613, 3982, 3443,\n", 186 | " 3993, 3640, 4208, 3832, 3876, 3497, 3466, 3095, 4424, 3878, 4046,\n", 187 | " 3804, 3710, 4747, 4423, 4036, 4022, 3454, 4175, 3787, 3796, 4103,\n", 188 | " 4161, 4158, 3814, 3527, 3748, 3334, 3492, 3962, 3505, 4315, 3804,\n", 189 | " 3863, 4034, 4308, 3165, 3641, 3644, 3891, 3793, 4270, 4063, 4012,\n", 190 | " 3458, 3890, 4166, 3935, 3669, 3866, 3393, 4442, 4253, 3727, 3329,\n", 191 | " 3415, 3372, 4430, 4381, 4008, 3858, 4121, 4057, 3824, 3394, 3558,\n", 192 | " 3362, 3930, 3835, 3830, 3856, 3249, 3577, 3933, 3850, 3309, 3406,\n", 193 | " 3506, 3907, 4160, 3318, 3662, 3899, 3700, 3779, 3473, 3490, 3654,\n", 194 | " 3478, 3495, 3834, 3876, 3661, 3618, 3648, 4032, 3399, 3916, 4430,\n", 195 | " 3695, 3524, 3571, 3594, 3383, 3499, 3589, 3900, 4114, 3937, 3399,\n", 196 | " 4200, 4488, 3614, 4051, 3782, 3391, 3124, 4053, 3582, 3666, 3532,\n", 197 | " 4046, 3667, 2857, 3436, 3791, 3302, 3104, 3171, 3572, 3530, 3175,\n", 198 | " 3438, 3903, 3899, 3401, 3267, 3451, 3090, 3413, 3323, 3680, 3439,\n", 199 | " 3853, 3156, 3279, 3707, 4006, 3269, 3071, 3779, 3548, 3292, 3497,\n", 200 | " 3082, 3248, 3358, 3803, 3566, 3145, 3503, 3571, 3724, 3615, 3203,\n", 201 | " 3609, 3561, 3979, 3533, 3689, 3158, 4005, 3181, 3479, 3642, 3632,\n", 202 | " 3069, 3394, 3703, 3165, 3354, 3000, 3687, 3556, 2773, 3058, 3344,\n", 203 | " 3493, 3297, 3360, 3228, 3277, 3851, 3067, 3692, 3402, 3995, 3318,\n", 204 | " 2720, 2937, 3580, 2939, 2989, 3586, 3156, 3246, 3170, 3268, 3389,\n", 205 | " 3381, 2864, 3740, 3479, 3647, 3716, 3284, 4204, 3735, 3218, 3685,\n", 206 | " 3704, 3214, 3394, 3233, 3352, 3391], dtype=int64)" 207 | ] 208 | }, 209 | "execution_count": 158, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "x" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "### mean calculation" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 159, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "x_mean = np.mean(x)\n", 232 | "y_mean = np.mean(y)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 160, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "data": { 242 | "text/plain": [ 243 | "3633.9915611814345" 244 | ] 245 | }, 246 | "execution_count": 160, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "x_mean" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 161, 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "data": { 262 | "text/plain": [ 263 | "1282.873417721519" 264 | ] 265 | }, 266 | "execution_count": 161, 267 | "metadata": {}, 268 | "output_type": "execute_result" 269 | } 270 | ], 271 | "source": [ 272 | "y_mean" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "### total number of values" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 162, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [ 288 | "n = len(x)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 163, 294 | "metadata": {}, 295 | "outputs": [ 296 | { 297 | "data": { 298 | "text/plain": [ 299 | "237" 300 | ] 301 | }, 302 | "execution_count": 163, 303 | "metadata": {}, 304 | "output_type": "execute_result" 305 | } 306 | ], 307 | "source": [ 308 | "n" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 164, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "data": { 318 | "text/plain": [ 319 | "16" 320 | ] 321 | }, 322 | "execution_count": 164, 323 | "metadata": {}, 324 | "output_type": "execute_result" 325 | } 326 | ], 327 | "source": [ 328 | "np.power(2,4)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "markdown", 333 | "metadata": {}, 334 | "source": [ 335 | "### using the formula to calculate b0 (slope) and b1(intercept)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 165, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "name": "stdout", 345 | "output_type": "stream", 346 | "text": [ 347 | "0.26342933948939945\n" 348 | ] 349 | } 350 | ], 351 | "source": [ 352 | "up = 0\n", 353 | "down = 0\n", 354 | "sum = 0\n", 355 | "square_sum = 0\n", 356 | "for i in range(n):\n", 357 | " x_diff = x[i] - x_mean\n", 358 | " y_diff = y[i] - y_mean\n", 359 | " sum = sum + (x_diff * y_diff)\n", 360 | " square_sum = square_sum + np.power(x_diff,2)\n", 361 | "\n", 362 | "slope = sum / square_sum\n", 363 | "print(slope)" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "### to find intercept" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 166, 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "341.40682022382975\n", 383 | "325.57342104944223\n" 384 | ] 385 | } 386 | ], 387 | "source": [ 388 | "#y=mx+c\n", 389 | "#c=y-mx\n", 390 | "\n", 391 | "intercept = y[0] - slope * x[0]\n", 392 | "intercept1 = y_mean - slope * x_mean\n", 393 | "print(intercept)\n", 394 | "print(intercept1)" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 167, 400 | "metadata": {}, 401 | "outputs": [ 402 | { 403 | "name": "stdout", 404 | "output_type": "stream", 405 | "text": [ 406 | "('slope is:', 0.26342933948939945, 'intercept is:', 325.57342104944223)\n" 407 | ] 408 | } 409 | ], 410 | "source": [ 411 | "print('slope is:',slope,'intercept is:',intercept1)" 412 | ] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "metadata": {}, 417 | "source": [ 418 | "### Our line of best fit is:" 419 | ] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "metadata": {}, 424 | "source": [ 425 | "### y=mx+c\n", 426 | "brainWeight = slope * headSize + intercept\n", 427 | "\n", 428 | "brainWeight = 0.26342933948939945 * headSize + 325.57342104944223 " 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 168, 434 | "metadata": {}, 435 | "outputs": [ 436 | { 437 | "data": { 438 | "text/plain": [ 439 | "1320.546036300904" 440 | ] 441 | }, 442 | "execution_count": 168, 443 | "metadata": {}, 444 | "output_type": "execute_result" 445 | } 446 | ], 447 | "source": [ 448 | "bw = 0.26342933948939945 * 3777 + 325.57342104944223\n", 449 | "bw" 450 | ] 451 | }, 452 | { 453 | "cell_type": "markdown", 454 | "metadata": {}, 455 | "source": [ 456 | "# plotting" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": 169, 462 | "metadata": {}, 463 | "outputs": [], 464 | "source": [ 465 | "max_x = np.max(x) + 100\n", 466 | "min_x = np.min(x) - 100" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 179, 472 | "metadata": {}, 473 | "outputs": [ 474 | { 475 | "name": "stdout", 476 | "output_type": "stream", 477 | "text": [ 478 | "4847\n", 479 | "2620\n" 480 | ] 481 | } 482 | ], 483 | "source": [ 484 | "print(max_x)\n", 485 | "print(min_x)" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": 172, 491 | "metadata": {}, 492 | "outputs": [], 493 | "source": [ 494 | "x1 = np.linspace(min_x,max_x,1000)\n", 495 | "y1 = intercept + slope * x1" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 173, 501 | "metadata": {}, 502 | "outputs": [ 503 | { 504 | "data": { 505 | "text/plain": [ 506 | "" 507 | ] 508 | }, 509 | "execution_count": 173, 510 | "metadata": {}, 511 | "output_type": "execute_result" 512 | }, 513 | { 514 | "data": { 515 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEKCAYAAAAFJbKyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xl8VNX9//HXh7BFEVlVFBBUlDWEEEBBFm0RN7BuVcGKK60V6fK1teqv1WqrtlK1Ll/9UoviBlhXVFTUqoCCEJQdFVTUIC0CsgnBAJ/fH/cmTCaTZBIySWbm/Xw85sHcc+/cOXMZ+My553zOMXdHREQkXvVquwIiIpJcFDhERKRSFDhERKRSFDhERKRSFDhERKRSFDhERKRSFDhERKRSFDhERKRSFDhERKRS6td2BRKhVatW3qFDh9quhohIUlmwYMF6d29d0XEpGTg6dOhAXl5ebVdDRCSpmNkX8RynW1UiIlIpChwiIlIpChwiIlIpKdnHEUthYSH5+fkUFBTUdlVkHzVu3Ji2bdvSoEGD2q6KSFpKm8CRn5/PAQccQIcOHTCz2q6OVJG7s2HDBvLz8+nYsWNtV0ckLaXNraqCggJatmypoJHkzIyWLVuq5ShSi9ImcAAKGilCf48itSutAoeIiOw7BY4alJGRQXZ2Nt27d2f48OFs2rSptqtUwqmnnlotdbrpppsYP358qfL+/fvv87lFZK/vHrqRTecdw6Zzj2LTecfw3UM31sj7KnD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NifGKuxea2RKC6XVeDcuXAB2KDnL3S8IpW+4FziOYAFCkFAUOSRf1gOPcfUdkoZndC7zl7mdasL7L2xG7y71dE7ZS+gI/IJjfaSzBTLSR529DMBfUCN+70FTMulRgZ8Tz3ZT+t2vl1HdnWN89Zlboe+cZ2hN9HnffHd6a+w0KHFIG9XFIuphB8B87AOGEmBC0ONaEzy+OOH4mMCo8tjuQFX3CcOTRge4+Hfgl4W2piP0NCJZrvdbdP4mjLvtiDjDYzDqG52xRwfGR9TQzO6roOTCc4BaeSEwKHJIuxgG5YSf2cvbOiPpX4DYze5fgNk6RB4Am4S2q3wLzYpzzAOCl8Jh3CKbzj9SfYLGgP0Z0kB9aTl2qzN2/AcYAz5rZImBqJV5uBLfrlhDcvmoD3LyvdZLUpdlxRUSkUtTiEBGRSlHgEBGRSlHgEBGRSlHgEBGRSlHgEBGRSlHgEBGRSlHgEBGRSlHgEBGRSvn/WatZjBx09ZAAAAAASUVORK5CYII=\n", 516 | "text/plain": [ 517 | "
" 518 | ] 519 | }, 520 | "metadata": {}, 521 | "output_type": "display_data" 522 | } 523 | ], 524 | "source": [ 525 | "plt.plot(x1,y1,color='#58b970',label = 'Regression Line')\n", 526 | "plt.scatter(x1,y1,color='#ef5423',label='Scatter plot')\n", 527 | "plt.xlabel('Head size in cm3')\n", 528 | "plt.ylabel('Brain weight in grams')\n", 529 | "plt.legend()\n", 530 | "plt.show" 531 | ] 532 | }, 533 | { 534 | "cell_type": "markdown", 535 | "metadata": {}, 536 | "source": [ 537 | "### predicting score of model" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 174, 543 | "metadata": {}, 544 | "outputs": [], 545 | "source": [ 546 | "from sklearn.linear_model import LinearRegression\n", 547 | "from sklearn.metrics import mean_squared_error" 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "execution_count": 175, 553 | "metadata": {}, 554 | "outputs": [], 555 | "source": [ 556 | "x = x.reshape((n,1))\n", 557 | "reg = LinearRegression()" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 176, 563 | "metadata": {}, 564 | "outputs": [], 565 | "source": [ 566 | "reg = reg.fit(x,y)" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 177, 572 | "metadata": {}, 573 | "outputs": [], 574 | "source": [ 575 | "y_predict = reg.predict(x)\n", 576 | "r2_score = reg.score(x,y)" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": 178, 582 | "metadata": {}, 583 | "outputs": [ 584 | { 585 | "data": { 586 | "text/plain": [ 587 | "0.639311719957" 588 | ] 589 | }, 590 | "execution_count": 178, 591 | "metadata": {}, 592 | "output_type": "execute_result" 593 | } 594 | ], 595 | "source": [ 596 | "r2_score" 597 | ] 598 | }, 599 | { 600 | "cell_type": "code", 601 | "execution_count": null, 602 | "metadata": {}, 603 | "outputs": [], 604 | "source": [] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": null, 609 | "metadata": {}, 610 | "outputs": [], 611 | "source": [] 612 | } 613 | ], 614 | "metadata": { 615 | "kernelspec": { 616 | "display_name": "Python 2", 617 | "language": "python", 618 | "name": "python2" 619 | }, 620 | "language_info": { 621 | "codemirror_mode": { 622 | "name": "ipython", 623 | "version": 2 624 | }, 625 | "file_extension": ".py", 626 | "mimetype": "text/x-python", 627 | "name": "python", 628 | "nbconvert_exporter": "python", 629 | "pygments_lexer": "ipython2", 630 | "version": "2.7.15" 631 | } 632 | }, 633 | "nbformat": 4, 634 | "nbformat_minor": 2 635 | } 636 | -------------------------------------------------------------------------------- /numpy basic.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 11, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "[0 1 2 3 4 5 6 7 8 9]\n", 13 | "('dimension of v is', 1)\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "import numpy as np\n", 19 | "v=np.array([0,1,2,3,4,5,6,7,8,9])\n", 20 | "print(v)\n", 21 | "print('dimension of v is', v.ndim)" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 18, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "array([1, 3, 5, 7, 9])" 33 | ] 34 | }, 35 | "execution_count": 18, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "odd=v[1::2]\n", 42 | "odd" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 65, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "[9 8 7 6 5 4 3 2 1 0]\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "backward=v[::-1]\n", 60 | "print(backward)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [] 69 | } 70 | ], 71 | "metadata": { 72 | "kernelspec": { 73 | "display_name": "Python 2", 74 | "language": "python", 75 | "name": "python2" 76 | }, 77 | "language_info": { 78 | "codemirror_mode": { 79 | "name": "ipython", 80 | "version": 2 81 | }, 82 | "file_extension": ".py", 83 | "mimetype": "text/x-python", 84 | "name": "python", 85 | "nbconvert_exporter": "python", 86 | "pygments_lexer": "ipython2", 87 | "version": "2.7.15" 88 | } 89 | }, 90 | "nbformat": 4, 91 | "nbformat_minor": 2 92 | } 93 | -------------------------------------------------------------------------------- /numpy1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 19, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "cvalues = [20.1, 20.8, 21.9, 22.5, 22.7, 22.3, 21.8, 21.2, 20.9, 20.1]" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 20, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "list" 30 | ] 31 | }, 32 | "execution_count": 20, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "type(cvalues)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 21, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "c = np.array(cvalues)\n", 56 | "print(type(c))" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 22, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "[20.1 20.8 21.9 22.5 22.7 22.3 21.8 21.2 20.9 20.1]\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "print(c)" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 23, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "[68.18 69.44 71.42 72.5 72.86 72.14 71.24 70.16 69.62 68.18]\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "print(c* 9/5 + 32)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 24, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "[68.18, 69.44, 71.42, 72.5, 72.86, 72.14, 71.24000000000001, 70.16, 69.62, 68.18]\n" 103 | ] 104 | }, 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "list" 109 | ] 110 | }, 111 | "execution_count": 24, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "fvalues=[x*9/5 +32 for x in cvalues]\n", 118 | "print(fvalues)\n", 119 | "type(fvalues)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 25, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "import matplotlib.pyplot as mp" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 39, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAD8CAYAAABw1c+bAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xd8VuX5x/HPlU0mIyFAEggrQJiBgAwB60DEgYLIcKBWsFbrrgN/tnZYZ6mg1r0lOAAnCKKiCEJCwoYwwkgCgRBWEkb2/fsjT1tsg3kCSe5nXO/XixfJ4Tnhm0f4cjznXPcRYwxKKaW8h4/tAEoppRqXFr9SSnkZLX6llPIyWvxKKeVltPiVUsrLaPErpZSX0eJXSikvo8WvlFJeRotfKaW8jJ/tADWJjIw08fHxtmMopZTbyMjIOGiMiXLmtS5Z/PHx8aSnp9uOoZRSbkNEsp19rZ7qUUopL6PFr5RSXkaLXymlvIwWv1JKeRktfqWU8jJa/Eop5WW0+JVSysu45H38StWnisoqvsnMx8/HhwsTo23HUco6LX7lsY6XVvBRei5vLt9F7uGTAPz+4i789ryOiIjldErZo8WvPM7+whLe/mk3KanZFJVU0LdtUx6+pBtfb9rPM4u2cqCohD9c3h1fHy1/5Z20+JXH2JxXxOs/7uSL9XlUVhku7t6KW4Z2oF+7ZgCM7N6KluFBvLp0JwXHSpl+TR+C/H0tp1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AyjTHTbeexyRjzsDEm1hgTT/Wfi++MMR5/RHc6xpj9QK6IdHFsugDYbDGSbTnAQBEJdvy9uQAvuNhd61o97sAYUyEidwCLqL4q/6YxZpPlWDYNAa4HNojIWse2acaYBRYzKdfxO2CW4yBpJ3CT5TzWGGNSRWQO1SsHVwBr8IIpXp3cVUopL+Mpp3qUUko5SYtfKaW8jBa/Ukp5GS1+pZTyMlr8SinlZbT4lVLKy2jxK6WUl9HiV0opL/P/NkGHDoA8tZIAAAAASUVORK5CYII=\n", 139 | "text/plain": [ 140 | "
" 141 | ] 142 | }, 143 | "metadata": {}, 144 | "output_type": "display_data" 145 | } 146 | ], 147 | "source": [ 148 | "mp.plot(cvalues)\n", 149 | "mp.show()" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "# counting total elements in list\n", 157 | "for i in cvalues: \n", 158 | " sum=sum+1 \n", 159 | "print(sum)\n", 160 | "print(cvalues.count(20.1))" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 34, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "0\n", 173 | "10\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "print(cvalues.count(cvalues))\n", 179 | "print(len(cvalues)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 53, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "name": "stdout", 189 | "output_type": "stream", 190 | "text": [ 191 | "('size of empty list is:', 36)\n", 192 | "48\n", 193 | "36\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "from sys import getsizeof as size\n", 199 | "lis = [24, 12, 57]\n", 200 | "l=[]\n", 201 | "print(\"size of empty list is:\", size(l))\n", 202 | "print(size(lis))\n", 203 | "sizeofelement = len(lis) * size(lis[0])\n", 204 | "print(sizeofelement)\n" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 58, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "name": "stdout", 214 | "output_type": "stream", 215 | "text": [ 216 | "60\n" 217 | ] 218 | } 219 | ], 220 | "source": [ 221 | "d = np.array([24,12,57])\n", 222 | "print(size(d))" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 59, 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "name": "stdout", 232 | "output_type": "stream", 233 | "text": [ 234 | "48\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "e = np.array([])\n", 240 | "print(size(e))" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 104, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "name": "stdout", 250 | "output_type": "stream", 251 | "text": [ 252 | "0.00500011444092\n", 253 | "0.0870001316071\n", 254 | "('numpy array is ', 0.0820000171661377, 'times faster than python array')\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "import time\n", 260 | "r=1000000\n", 261 | "def pythonic():\n", 262 | " t1 = time.time()\n", 263 | " a = b = range(r) \n", 264 | " z=[a[i]+b[i] for i in range(len(a))]\n", 265 | " print(time.time()-t1)\n", 266 | " return time.time()-t1\n", 267 | "\n", 268 | "import time\n", 269 | "def numpic():\n", 270 | " t2 = time.time()\n", 271 | " a = np.arange(1000000)\n", 272 | " b = np.arange(1000000)\n", 273 | " z = a + b\n", 274 | " print(time.time()-t2)\n", 275 | " return time.time()-t2\n", 276 | "n1 = numpic()\n", 277 | "n2 = pythonic()\n", 278 | "print('numpy array is ', n2-n1, 'times faster than python array')\n", 279 | "\n", 280 | "\n" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 111, 286 | "metadata": {}, 287 | "outputs": [ 288 | { 289 | "ename": "SyntaxError", 290 | "evalue": "invalid syntax (, line 1)", 291 | "output_type": "error", 292 | "traceback": [ 293 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m s = {*()}\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "s = {*()}\n", 299 | "print(type(s))" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 117, 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "ename": "SyntaxError", 309 | "evalue": "invalid syntax (, line 1)", 310 | "output_type": "error", 311 | "traceback": [ 312 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m s = {*()}\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "s = {*()}" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [] 326 | } 327 | ], 328 | "metadata": { 329 | "kernelspec": { 330 | "display_name": "Python 2", 331 | "language": "python", 332 | "name": "python2" 333 | }, 334 | "language_info": { 335 | "codemirror_mode": { 336 | "name": "ipython", 337 | "version": 2 338 | }, 339 | "file_extension": ".py", 340 | "mimetype": "text/x-python", 341 | "name": "python", 342 | "nbconvert_exporter": "python", 343 | "pygments_lexer": "ipython2", 344 | "version": "2.7.15" 345 | } 346 | }, 347 | "nbformat": 4, 348 | "nbformat_minor": 2 349 | } 350 | -------------------------------------------------------------------------------- /numpy2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "0\n", 13 | "42\n", 14 | "()\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "import numpy as np\n", 20 | "x = np.array(42)\n", 21 | "print(np.ndim(x))\n", 22 | "print(x)\n", 23 | "print(np.shape(x))" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 3, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "\n", 36 | "1\n" 37 | ] 38 | }, 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "(7,)" 43 | ] 44 | }, 45 | "execution_count": 3, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "a=np.array([1,2,3,4,5,6,7])\n", 52 | "print(type(a))\n", 53 | "print(np.ndim(a))\n", 54 | "np.shape(a)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 40, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "2\n", 67 | "('shape before modification is', (2, 3))\n", 68 | "[[1 2 3]\n", 69 | " [4 5 6]]\n", 70 | "after reshaping\n", 71 | "[[1 2]\n", 72 | " [3 4]\n", 73 | " [5 6]]\n", 74 | "('shape after modification is', (3, 2))\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "b=np.array([ [1,2,3],\n", 80 | " [4,5,6] ])\n", 81 | "print(np.ndim(b))\n", 82 | "print('shape before modification is',np.shape(b))\n", 83 | "print(b)\n", 84 | "print('after reshaping')\n", 85 | "b.shape=(3,2)\n", 86 | "print(b)\n", 87 | "print('shape after modification is',np.shape(b))" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 28, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "3\n" 100 | ] 101 | }, 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "(2, 2, 2)" 106 | ] 107 | }, 108 | "execution_count": 28, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "c=[ [[1,2],[3,4]],\n", 115 | " [[5,6],[7,8]] ]\n", 116 | "print(np.ndim(c))\n", 117 | "np.shape(c)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 41, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "[[1 2]\n", 130 | " [3 4]\n", 131 | " [5 6]]\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "change_array = np.array([1,2,3,4,5,6])\n", 137 | "change_array.shape = (3, 2)\n", 138 | "print change_array " 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 44, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "1\n", 151 | "5\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "o = np.array([1,2,3,4,5])\n", 157 | "print(o[0])\n", 158 | "print(o[-1])" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 49, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "[3, 4]\n" 171 | ] 172 | } 173 | ], 174 | "source": [ 175 | "c=[ [[1,2],[3,4]],\n", 176 | " [[5,6],[7,8]] ]\n", 177 | "print(c[0][1])" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 67, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "[0 1 2 3 4 5 6 7 8 9]\n", 190 | "[1 2 3 4]\n", 191 | "[4 5 6 7]\n", 192 | "8\n", 193 | "[2 3 4 5 6 7 8]\n", 194 | "[0 1 2]\n", 195 | "[8 9]\n", 196 | "5\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "s = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n", 202 | "print(s[:])\n", 203 | "print(s[1:5])\n", 204 | "print(s[4:8])\n", 205 | "print(s[-2])\n", 206 | "print(s[2:-1])\n", 207 | "print(s[:3])\n", 208 | "print(s[8:])\n", 209 | "print(s[len(s)/2])" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 101, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "name": "stdout", 219 | "output_type": "stream", 220 | "text": [ 221 | "2\n", 222 | "(5, 5)\n", 223 | "[33 34 35]\n", 224 | "[13 23 33 43 53]\n", 225 | "13\n", 226 | "23\n", 227 | "33\n", 228 | "43\n", 229 | "53\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "t = np.array([\n", 235 | "[11, 12, 13, 14, 15],\n", 236 | "[21, 22, 23, 24, 25],\n", 237 | "[31, 32, 33, 34, 35],\n", 238 | "[41, 42, 43, 44, 45],\n", 239 | "[51, 52, 53, 54, 55]])\n", 240 | "\n", 241 | "print(np.ndim(t))\n", 242 | "print(np.shape(t))\n", 243 | "print(t[2,2:])\n", 244 | "val=t[:,2]\n", 245 | "print(val)\n", 246 | "# displaying column result verically\n", 247 | "for i in val:\n", 248 | " print(i)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 94, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "data": { 258 | "text/plain": [ 259 | "[[1, 4, 7], [2, 5, 8], [3, 6, 9]]" 260 | ] 261 | }, 262 | "execution_count": 94, 263 | "metadata": {}, 264 | "output_type": "execute_result" 265 | } 266 | ], 267 | "source": [ 268 | "l = [[1,2,3],[4,5,6],[7,8,9]]\n", 269 | "map(list, zip(*l))" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 93, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "data": { 279 | "text/plain": [ 280 | "[[1, 4, 7], [2, 5, 8], [3, 6, 9]]" 281 | ] 282 | }, 283 | "execution_count": 93, 284 | "metadata": {}, 285 | "output_type": "execute_result" 286 | } 287 | ], 288 | "source": [ 289 | "np.array(l).T.tolist()" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 111, 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "name": "stdout", 299 | "output_type": "stream", 300 | "text": [ 301 | "[[14]\n", 302 | " [24]\n", 303 | " [34]\n", 304 | " [44]\n", 305 | " [54]]\n", 306 | "[14 24 34 44 54]\n", 307 | "[14]\n", 308 | "[24]\n", 309 | "[34]\n", 310 | "[44]\n", 311 | "[54]\n" 312 | ] 313 | } 314 | ], 315 | "source": [ 316 | "p = np.array([\n", 317 | "[11, 12, 13, 14, 15],\n", 318 | "[21, 22, 23, 24, 25],\n", 319 | "[31, 32, 33, 34, 35],\n", 320 | "[41, 42, 43, 44, 45],\n", 321 | "[51, 52, 53, 54, 55]])\n", 322 | "g=p[:,3:4]\n", 323 | "print(g)\n", 324 | "# OR\n", 325 | "print(p[:,3])\n", 326 | "# OR\n", 327 | "for i in g:\n", 328 | " print(i)\n" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 10, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "name": "stdout", 338 | "output_type": "stream", 339 | "text": [ 340 | "\n", 341 | "int32\n" 342 | ] 343 | }, 344 | { 345 | "data": { 346 | "text/plain": [ 347 | "array([[0, 1, 2],\n", 348 | " [3, 4, 5]])" 349 | ] 350 | }, 351 | "execution_count": 10, 352 | "metadata": {}, 353 | "output_type": "execute_result" 354 | } 355 | ], 356 | "source": [ 357 | "a = np.arange(6)\n", 358 | "print(type(a))\n", 359 | "print(a.dtype)\n", 360 | "a.shape=(3,2)\n", 361 | "b=a.reshape(2,3)\n", 362 | "b" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 11, 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "name": "stdout", 372 | "output_type": "stream", 373 | "text": [ 374 | "[1 1 1 1]\n", 375 | "[[[[1. 1. 1. 1.]\n", 376 | " [1. 1. 1. 1.]\n", 377 | " [1. 1. 1. 1.]]\n", 378 | "\n", 379 | " [[1. 1. 1. 1.]\n", 380 | " [1. 1. 1. 1.]\n", 381 | " [1. 1. 1. 1.]]]]\n", 382 | "[[0 0 0]\n", 383 | " [0 0 0]]\n" 384 | ] 385 | } 386 | ], 387 | "source": [ 388 | "x=np.array([1,2,3,4])\n", 389 | "a=np.ones_like(x)\n", 390 | "print(a)\n", 391 | "s=np.ones(x)\n", 392 | "print(s)\n", 393 | "t=np.array([[4,5,6],[1,2,3]])\n", 394 | "b=np.zeros_like(t)\n", 395 | "print(b)" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 12, 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "[[ 1 2 34 5]\n", 408 | " [54 43 5 6]]\n", 409 | "[[ 0 2 34 5]\n", 410 | " [54 43 5 6]]\n" 411 | ] 412 | } 413 | ], 414 | "source": [ 415 | "import numpy as np\n", 416 | "x=np.array([[1,2,34,5],[54,43,5,6]],order='K')\n", 417 | "y=x.copy()\n", 418 | "y[0][0]=0\n", 419 | "print(x)\n", 420 | "print(y)" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 14, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "data": { 430 | "text/plain": [ 431 | "array([[1, 0, 0],\n", 432 | " [0, 1, 0],\n", 433 | " [0, 0, 1]])" 434 | ] 435 | }, 436 | "execution_count": 14, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "import numpy as np\n", 443 | "np.identity(3,dtype=int)" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 33, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "data": { 453 | "text/plain": [ 454 | "array([[0, 0, 0],\n", 455 | " [1, 0, 0],\n", 456 | " [0, 1, 0],\n", 457 | " [0, 0, 1]])" 458 | ] 459 | }, 460 | "execution_count": 33, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "np.eye(4,3,k=-1,dtype=int)" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 27, 472 | "metadata": {}, 473 | "outputs": [ 474 | { 475 | "data": { 476 | "text/plain": [ 477 | "array([[0, 1, 0],\n", 478 | " [0, 0, 1],\n", 479 | " [0, 0, 0]])" 480 | ] 481 | }, 482 | "execution_count": 27, 483 | "metadata": {}, 484 | "output_type": "execute_result" 485 | } 486 | ], 487 | "source": [ 488 | "np.eye(3,k=1,dtype=int)" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": null, 494 | "metadata": {}, 495 | "outputs": [], 496 | "source": [] 497 | } 498 | ], 499 | "metadata": { 500 | "kernelspec": { 501 | "display_name": "Python 2", 502 | "language": "python", 503 | "name": "python2" 504 | }, 505 | "language_info": { 506 | "codemirror_mode": { 507 | "name": "ipython", 508 | "version": 2 509 | }, 510 | "file_extension": ".py", 511 | "mimetype": "text/x-python", 512 | "name": "python", 513 | "nbconvert_exporter": "python", 514 | "pygments_lexer": "ipython2", 515 | "version": "2.7.15" 516 | } 517 | }, 518 | "nbformat": 4, 519 | "nbformat_minor": 2 520 | } 521 | --------------------------------------------------------------------------------