|
407 | 407 | "print(p)"
|
408 | 408 | ]
|
409 | 409 | },
|
| 410 | + { |
| 411 | + "cell_type": "markdown", |
| 412 | + "metadata": {}, |
| 413 | + "source": [ |
| 414 | + "### create an array by repetition" |
| 415 | + ] |
| 416 | + }, |
| 417 | + { |
| 418 | + "cell_type": "code", |
| 419 | + "execution_count": 17, |
| 420 | + "metadata": {}, |
| 421 | + "outputs": [ |
| 422 | + { |
| 423 | + "name": "stdout", |
| 424 | + "output_type": "stream", |
| 425 | + "text": [ |
| 426 | + "[0 0 0 1 1 1 2 2 2]\n", |
| 427 | + "[[1 2]\n", |
| 428 | + " [3 4]\n", |
| 429 | + " [3 4]]\n" |
| 430 | + ] |
| 431 | + } |
| 432 | + ], |
| 433 | + "source": [ |
| 434 | + "# repeat each element of an array by a specified number of times\n", |
| 435 | + "# np.repeat(iterable, reps, axis=None)\n", |
| 436 | + "arr = [0, 1, 2]\n", |
| 437 | + "print(np.repeat(arr, 3)) # or np.repeat(range(3), 3)\n", |
| 438 | + "\n", |
| 439 | + "# repeat along a specified axis with specified number of repetitions\n", |
| 440 | + "arr = [[1,2], [3,4]]\n", |
| 441 | + "print(np.repeat(arr, [1,2], axis=0))" |
| 442 | + ] |
| 443 | + }, |
| 444 | + { |
| 445 | + "cell_type": "code", |
| 446 | + "execution_count": 18, |
| 447 | + "metadata": {}, |
| 448 | + "outputs": [ |
| 449 | + { |
| 450 | + "name": "stdout", |
| 451 | + "output_type": "stream", |
| 452 | + "text": [ |
| 453 | + "[0 1 2 0 1 2 0 1 2]\n" |
| 454 | + ] |
| 455 | + } |
| 456 | + ], |
| 457 | + "source": [ |
| 458 | + "# repeat an array by a specified number of times\n", |
| 459 | + "arr = [0, 1, 2]\n", |
| 460 | + "print(np.tile(arr, 3))\n", |
| 461 | + "\n", |
| 462 | + "# " |
| 463 | + ] |
| 464 | + }, |
| 465 | + { |
| 466 | + "cell_type": "code", |
| 467 | + "execution_count": null, |
| 468 | + "metadata": {}, |
| 469 | + "outputs": [], |
| 470 | + "source": [] |
| 471 | + }, |
410 | 472 | {
|
411 | 473 | "cell_type": "markdown",
|
412 | 474 | "metadata": {},
|
|
0 commit comments