|
496 | 496 | "greet(\"Nora\")" |
497 | 497 | ] |
498 | 498 | }, |
| 499 | + { |
| 500 | + "cell_type": "code", |
| 501 | + "execution_count": 22, |
| 502 | + "metadata": { |
| 503 | + "collapsed": false |
| 504 | + }, |
| 505 | + "outputs": [ |
| 506 | + { |
| 507 | + "name": "stdout", |
| 508 | + "output_type": "stream", |
| 509 | + "text": [ |
| 510 | + "{'and': 4, 'learning.': 1, '(the': 1, 'family': 1, 'be': 1, 'other.': 1, 'experience,': 1, 'unknown.Artificial': 1, 'number': 1, 'numeric': 1, 'connections': 1, 'as': 1, 'brain)': 1, 'are': 4, 'learning': 1, 'in': 1, 'based': 1, 'tuned': 1, 'nets': 1, 'networks': 3, '(ANNs)': 1, 'functions': 1, 'depend': 1, 'capable': 1, 'nervous': 1, 'exchange': 1, 'generally': 2, 'approximate': 1, 'artificial': 1, 'machine': 1, 'to': 2, 'systems': 2, 'which': 1, 'between': 1, 'adaptive': 1, '\"neurons\"': 1, 'inputs': 2, 'used': 1, 'that': 2, 'models': 1, 'each': 1, 'animals,': 1, 'particular': 1, 'The': 1, 'estimate': 1, 'by': 1, 'a': 2, 'on': 2, 'central': 1, 'cognitive': 1, 'neural': 4, 'of': 5, 'inspired': 1, 'presented': 1, 'messages': 1, 'science,': 1, 'interconnected': 1, 'large': 1, 'weights': 1, 'can': 2, 'have': 1, 'In': 1, 'biological': 1, 'the': 1, 'or': 1, 'making': 1}\n" |
| 511 | + ] |
| 512 | + } |
| 513 | + ], |
| 514 | + "source": [ |
| 515 | + "# A function can \"return\" an object.\n", |
| 516 | + "# We provide an example here\n", |
| 517 | + "\n", |
| 518 | + "# text below is from https://en.wikipedia.org/wiki/Artificial_neural_network\n", |
| 519 | + "sentences=[\"In machine learning and cognitive science, artificial neural networks (ANNs)\\\n", |
| 520 | + " are a family of models inspired by biological neural networks (the central nervous systems of animals, \\\n", |
| 521 | + " in particular the brain) and are used to estimate or approximate functions that can depend on a large\\\n", |
| 522 | + " number of inputs and are generally unknown.\"\n", |
| 523 | + " \"Artificial neural networks are generally presented as systems of interconnected \\\"neurons\\\" which \\\n", |
| 524 | + " exchange messages between each other. The connections have numeric weights that can be tuned based \\\n", |
| 525 | + " on experience, making neural nets adaptive to inputs and capable of learning.\"]\n", |
| 526 | + "def get_dict(sentences):\n", |
| 527 | + " \"\"\"\n", |
| 528 | + " arguments:\n", |
| 529 | + " input: @sentences: a list of sentences\n", |
| 530 | + " returns: a dictionary of the words in the sentences.\n", |
| 531 | + " dict key is a word and value is word frequency\n", |
| 532 | + " \"\"\"\n", |
| 533 | + " word_freq={}\n", |
| 534 | + " for sent in sentences:\n", |
| 535 | + " words=sent.split()\n", |
| 536 | + " for w in words:\n", |
| 537 | + " if w in word_freq:\n", |
| 538 | + " word_freq[w]+=1\n", |
| 539 | + " else:\n", |
| 540 | + " word_freq[w]=1\n", |
| 541 | + " return word_freq\n", |
| 542 | + " \n", |
| 543 | + " \n", |
| 544 | + "my_word_freq_dict=get_dict(sentences)\n", |
| 545 | + "print my_word_freq_dict" |
| 546 | + ] |
| 547 | + }, |
| 548 | + { |
| 549 | + "cell_type": "code", |
| 550 | + "execution_count": 63, |
| 551 | + "metadata": { |
| 552 | + "collapsed": false |
| 553 | + }, |
| 554 | + "outputs": [ |
| 555 | + { |
| 556 | + "name": "stdout", |
| 557 | + "output_type": "stream", |
| 558 | + "text": [ |
| 559 | + "and 4\n", |
| 560 | + "are 4\n", |
| 561 | + "networks 3\n", |
| 562 | + "neural 4\n", |
| 563 | + "of 5\n" |
| 564 | + ] |
| 565 | + } |
| 566 | + ], |
| 567 | + "source": [ |
| 568 | + "# Here's the same function as above, but using python's \"defaultdict\"\n", |
| 569 | + "from collections import defaultdict\n", |
| 570 | + "sentences=[\"In machine learning and cognitive science, artificial neural networks (ANNs)\\\n", |
| 571 | + " are a family of models inspired by biological neural networks (the central nervous systems of animals, \\\n", |
| 572 | + " in particular the brain) and are used to estimate or approximate functions that can depend on a large\\\n", |
| 573 | + " number of inputs and are generally unknown.\"\n", |
| 574 | + " \"Artificial neural networks are generally presented as systems of interconnected \\\"neurons\\\" which \\\n", |
| 575 | + " exchange messages between each other. The connections have numeric weights that can be tuned based \\\n", |
| 576 | + " on experience, making neural nets adaptive to inputs and capable of learning.\"]\n", |
| 577 | + "\n", |
| 578 | + "def get_dict(sentences):\n", |
| 579 | + " \"\"\"\n", |
| 580 | + " arguments:\n", |
| 581 | + " input: @sentences: a list of sentences\n", |
| 582 | + " returns: a dictionary of the words in the sentences.\n", |
| 583 | + " dict key is a word and value is word frequency\n", |
| 584 | + " \"\"\"\n", |
| 585 | + " word_freq=defaultdict(int)\n", |
| 586 | + " for sent in sentences:\n", |
| 587 | + " words=sent.split()\n", |
| 588 | + " for w in words:\n", |
| 589 | + " word_freq[w]+=1\n", |
| 590 | + " return word_freq\n", |
| 591 | + " \n", |
| 592 | + "my_word_freq_dict=get_dict(sentences)\n", |
| 593 | + "# Let's print only keys with values > 2 this time\n", |
| 594 | + "for k in my_word_freq_dict:\n", |
| 595 | + " if my_word_freq_dict[k] > 2:\n", |
| 596 | + " print k, my_word_freq_dict[k]" |
| 597 | + ] |
| 598 | + }, |
499 | 599 | { |
500 | 600 | "cell_type": "markdown", |
501 | 601 | "metadata": {}, |
|
0 commit comments