|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:9e62469f6250ac6d58e6d3d2c67a5995d1505778b147ae918d71f4ebc1c12fb6" |
| 4 | + "signature": "sha256:8294645ddf3a6997a4764ca0bb61458953a6e579d858ecbf59e69882af05d0df" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
65 | 65 | "- [Dictionary operations](#dict_ops) \n", |
66 | 66 | " - [Adding elements to a dictionary](#adding_dict_elements)\n", |
67 | 67 | "- [Comprehensions vs. for-loops](#comprehensions)\n", |
68 | | - "- [Copying files by searching directory trees](#find_copy)" |
| 68 | + "- [Copying files by searching directory trees](#find_copy)\n", |
| 69 | + "- [Returning column vectors slicing through a numpy array](#row_vectors)" |
69 | 70 | ] |
70 | 71 | }, |
71 | 72 | { |
|
1410 | 1411 | "prompt_number": 35 |
1411 | 1412 | }, |
1412 | 1413 | { |
1413 | | - "cell_type": "code", |
1414 | | - "collapsed": false, |
1415 | | - "input": [], |
1416 | | - "language": "python", |
| 1414 | + "cell_type": "markdown", |
1417 | 1415 | "metadata": {}, |
1418 | | - "outputs": [] |
| 1416 | + "source": [ |
| 1417 | + "I have to say that I am really positively surprised. The shell's `find` scales even better than expected!" |
| 1418 | + ] |
1419 | 1419 | }, |
1420 | 1420 | { |
1421 | 1421 | "cell_type": "markdown", |
1422 | 1422 | "metadata": {}, |
1423 | 1423 | "source": [ |
1424 | | - "I have to say that I am really positively surprised. The shell's `find` scales even better than expected!" |
| 1424 | + "<br>\n", |
| 1425 | + "<br>\n", |
| 1426 | + "<a name='row_vectors'></a>" |
| 1427 | + ] |
| 1428 | + }, |
| 1429 | + { |
| 1430 | + "cell_type": "markdown", |
| 1431 | + "metadata": {}, |
| 1432 | + "source": [ |
| 1433 | + "# Returning column vectors slicing through a numpy array" |
1425 | 1434 | ] |
1426 | 1435 | }, |
| 1436 | + { |
| 1437 | + "cell_type": "markdown", |
| 1438 | + "metadata": {}, |
| 1439 | + "source": [ |
| 1440 | + "Given a numpy matrix, I want to iterate through it and return each column as a 1-column vector. \n", |
| 1441 | + "E.g., if I want to return the 1st column from matrix A below\n", |
| 1442 | + "\n", |
| 1443 | + "<pre>\n", |
| 1444 | + "A = np.array([ [1,2,3], [4,5,6], [7,8,9] ])\n", |
| 1445 | + ">>> A\n", |
| 1446 | + "array([[1, 2, 3],\n", |
| 1447 | + " [4, 5, 6],\n", |
| 1448 | + " [7, 8, 9]])</pre>\n", |
| 1449 | + "\n", |
| 1450 | + "I want my result to be:\n", |
| 1451 | + "<pre>\n", |
| 1452 | + "array([[1],\n", |
| 1453 | + " [4],\n", |
| 1454 | + " [7]])</pre>\n", |
| 1455 | + "\n", |
| 1456 | + "with `.shape` = `(3,1)`\n", |
| 1457 | + "\n", |
| 1458 | + "\n", |
| 1459 | + "However, the default behavior of numpy is to return the column as a row vector:\n", |
| 1460 | + "\n", |
| 1461 | + "<pre>\n", |
| 1462 | + ">>> A[:,0]\n", |
| 1463 | + "array([1, 4, 7])\n", |
| 1464 | + ">>> A[:,0].shape\n", |
| 1465 | + "(3,)\n", |
| 1466 | + "</pre>" |
| 1467 | + ] |
| 1468 | + }, |
| 1469 | + { |
| 1470 | + "cell_type": "code", |
| 1471 | + "collapsed": false, |
| 1472 | + "input": [ |
| 1473 | + "import numpy as np\n", |
| 1474 | + "\n", |
| 1475 | + "def colvec_method1(A):\n", |
| 1476 | + " for col in A.T:\n", |
| 1477 | + " colvec = row[:,np.newaxis]\n", |
| 1478 | + " yield colvec" |
| 1479 | + ], |
| 1480 | + "language": "python", |
| 1481 | + "metadata": {}, |
| 1482 | + "outputs": [], |
| 1483 | + "prompt_number": 83 |
| 1484 | + }, |
| 1485 | + { |
| 1486 | + "cell_type": "code", |
| 1487 | + "collapsed": false, |
| 1488 | + "input": [ |
| 1489 | + "def colvec_method2(A):\n", |
| 1490 | + " for idx in range(A.shape[1]):\n", |
| 1491 | + " colvec = A[:,idx:idx+1]\n", |
| 1492 | + " yield colvec" |
| 1493 | + ], |
| 1494 | + "language": "python", |
| 1495 | + "metadata": {}, |
| 1496 | + "outputs": [], |
| 1497 | + "prompt_number": 82 |
| 1498 | + }, |
| 1499 | + { |
| 1500 | + "cell_type": "code", |
| 1501 | + "collapsed": false, |
| 1502 | + "input": [ |
| 1503 | + "def colvec_method3(A):\n", |
| 1504 | + " for idx in range(A.shape[1]):\n", |
| 1505 | + " colvec = A[:,idx].reshape(A.shape[0],1)\n", |
| 1506 | + " yield colvec" |
| 1507 | + ], |
| 1508 | + "language": "python", |
| 1509 | + "metadata": {}, |
| 1510 | + "outputs": [], |
| 1511 | + "prompt_number": 81 |
| 1512 | + }, |
| 1513 | + { |
| 1514 | + "cell_type": "code", |
| 1515 | + "collapsed": false, |
| 1516 | + "input": [ |
| 1517 | + "def colvec_method4(A):\n", |
| 1518 | + " for idx in range(A.shape[1]):\n", |
| 1519 | + " colvec = np.vstack(A[:,idx])\n", |
| 1520 | + " yield colvec" |
| 1521 | + ], |
| 1522 | + "language": "python", |
| 1523 | + "metadata": {}, |
| 1524 | + "outputs": [], |
| 1525 | + "prompt_number": 79 |
| 1526 | + }, |
| 1527 | + { |
| 1528 | + "cell_type": "code", |
| 1529 | + "collapsed": false, |
| 1530 | + "input": [ |
| 1531 | + "def colvec_method5(A):\n", |
| 1532 | + " for idx in range(A.shape[1]):\n", |
| 1533 | + " colvec = np.row_stack(A[:,idx])\n", |
| 1534 | + " yield colvec" |
| 1535 | + ], |
| 1536 | + "language": "python", |
| 1537 | + "metadata": {}, |
| 1538 | + "outputs": [], |
| 1539 | + "prompt_number": 77 |
| 1540 | + }, |
| 1541 | + { |
| 1542 | + "cell_type": "code", |
| 1543 | + "collapsed": false, |
| 1544 | + "input": [ |
| 1545 | + "def colvec_method6(A):\n", |
| 1546 | + " for idx in range(A.shape[1]):\n", |
| 1547 | + " colvec = np.column_stack((A[:,idx],))\n", |
| 1548 | + " yield colvec" |
| 1549 | + ], |
| 1550 | + "language": "python", |
| 1551 | + "metadata": {}, |
| 1552 | + "outputs": [], |
| 1553 | + "prompt_number": 74 |
| 1554 | + }, |
| 1555 | + { |
| 1556 | + "cell_type": "code", |
| 1557 | + "collapsed": false, |
| 1558 | + "input": [ |
| 1559 | + "def test_method(method, A):\n", |
| 1560 | + " for i in method(A): \n", |
| 1561 | + " assert i.shape == (A.shape[0],1), \"{}, {}\".format(i.shape, A.shape[0],1)" |
| 1562 | + ], |
| 1563 | + "language": "python", |
| 1564 | + "metadata": {}, |
| 1565 | + "outputs": [], |
| 1566 | + "prompt_number": 69 |
| 1567 | + }, |
| 1568 | + { |
| 1569 | + "cell_type": "code", |
| 1570 | + "collapsed": false, |
| 1571 | + "input": [ |
| 1572 | + "import timeit\n", |
| 1573 | + "\n", |
| 1574 | + "A = np.random.random((300, 3))\n", |
| 1575 | + "\n", |
| 1576 | + "for method in [\n", |
| 1577 | + " colvec_method1, colvec_method2, \n", |
| 1578 | + " colvec_method3, colvec_method4, \n", |
| 1579 | + " colvec_method5, colvec_method6]:\n", |
| 1580 | + " print('\\nTest:', method.__name__)\n", |
| 1581 | + " %timeit test_method(colvec_method2, A)" |
| 1582 | + ], |
| 1583 | + "language": "python", |
| 1584 | + "metadata": {}, |
| 1585 | + "outputs": [ |
| 1586 | + { |
| 1587 | + "output_type": "stream", |
| 1588 | + "stream": "stdout", |
| 1589 | + "text": [ |
| 1590 | + "\n", |
| 1591 | + "Test: colvec_method1\n", |
| 1592 | + "100000 loops, best of 3: 17.7 \u00b5s per loop" |
| 1593 | + ] |
| 1594 | + }, |
| 1595 | + { |
| 1596 | + "output_type": "stream", |
| 1597 | + "stream": "stdout", |
| 1598 | + "text": [ |
| 1599 | + "\n", |
| 1600 | + "\n", |
| 1601 | + "Test: colvec_method2\n", |
| 1602 | + "10000 loops, best of 3: 16.4 \u00b5s per loop" |
| 1603 | + ] |
| 1604 | + }, |
| 1605 | + { |
| 1606 | + "output_type": "stream", |
| 1607 | + "stream": "stdout", |
| 1608 | + "text": [ |
| 1609 | + "\n", |
| 1610 | + "\n", |
| 1611 | + "Test: colvec_method3\n", |
| 1612 | + "100000 loops, best of 3: 17.3 \u00b5s per loop" |
| 1613 | + ] |
| 1614 | + }, |
| 1615 | + { |
| 1616 | + "output_type": "stream", |
| 1617 | + "stream": "stdout", |
| 1618 | + "text": [ |
| 1619 | + "\n", |
| 1620 | + "\n", |
| 1621 | + "Test: colvec_method4\n", |
| 1622 | + "10000 loops, best of 3: 16.4 \u00b5s per loop" |
| 1623 | + ] |
| 1624 | + }, |
| 1625 | + { |
| 1626 | + "output_type": "stream", |
| 1627 | + "stream": "stdout", |
| 1628 | + "text": [ |
| 1629 | + "\n", |
| 1630 | + "\n", |
| 1631 | + "Test: colvec_method5\n", |
| 1632 | + "100000 loops, best of 3: 17.1 \u00b5s per loop" |
| 1633 | + ] |
| 1634 | + }, |
| 1635 | + { |
| 1636 | + "output_type": "stream", |
| 1637 | + "stream": "stdout", |
| 1638 | + "text": [ |
| 1639 | + "\n", |
| 1640 | + "\n", |
| 1641 | + "Test: colvec_method6\n", |
| 1642 | + "100000 loops, best of 3: 16.6 \u00b5s per loop" |
| 1643 | + ] |
| 1644 | + }, |
| 1645 | + { |
| 1646 | + "output_type": "stream", |
| 1647 | + "stream": "stdout", |
| 1648 | + "text": [ |
| 1649 | + "\n" |
| 1650 | + ] |
| 1651 | + } |
| 1652 | + ], |
| 1653 | + "prompt_number": 86 |
| 1654 | + }, |
1427 | 1655 | { |
1428 | 1656 | "cell_type": "code", |
1429 | 1657 | "collapsed": false, |
|
0 commit comments