Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5b8490e

Browse files
numpy
numpy
1 parent 770afdb commit 5b8490e

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

‎dataAnalysis/Magical-numpy/numpy基础.ipynb‎

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,214 @@
773773
"# 5. 矩阵分割"
774774
]
775775
},
776+
{
777+
"cell_type": "code",
778+
"execution_count": 12,
779+
"metadata": {},
780+
"outputs": [
781+
{
782+
"name": "stdout",
783+
"output_type": "stream",
784+
"text": [
785+
"[[ 0 1 2 3]\n",
786+
" [ 4 5 6 7]\n",
787+
" [ 8 9 10 11]]\n"
788+
]
789+
}
790+
],
791+
"source": [
792+
"import numpy as np\n",
793+
"a = np.arange(12).reshape((3,4))\n",
794+
"print(a)"
795+
]
796+
},
797+
{
798+
"cell_type": "code",
799+
"execution_count": 13,
800+
"metadata": {},
801+
"outputs": [
802+
{
803+
"name": "stdout",
804+
"output_type": "stream",
805+
"text": [
806+
"[array([[0, 1],\n",
807+
" [4, 5],\n",
808+
" [8, 9]]), array([[ 2, 3],\n",
809+
" [ 6, 7],\n",
810+
" [10, 11]])]\n"
811+
]
812+
}
813+
],
814+
"source": [
815+
"print(np.split(a, 2,axis=1))"
816+
]
817+
},
818+
{
819+
"cell_type": "code",
820+
"execution_count": 15,
821+
"metadata": {},
822+
"outputs": [
823+
{
824+
"name": "stdout",
825+
"output_type": "stream",
826+
"text": [
827+
"[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]\n"
828+
]
829+
}
830+
],
831+
"source": [
832+
"print(np.split(a, 3, axis=0))"
833+
]
834+
},
835+
{
836+
"cell_type": "markdown",
837+
"metadata": {},
838+
"source": [
839+
"# 6. 矩阵复制"
840+
]
841+
},
842+
{
843+
"cell_type": "code",
844+
"execution_count": 16,
845+
"metadata": {},
846+
"outputs": [
847+
{
848+
"name": "stdout",
849+
"output_type": "stream",
850+
"text": [
851+
"[0 1 2 3]\n"
852+
]
853+
}
854+
],
855+
"source": [
856+
"import numpy as np\n",
857+
"a = np.arange(4)\n",
858+
"print(a)"
859+
]
860+
},
861+
{
862+
"cell_type": "code",
863+
"execution_count": 17,
864+
"metadata": {},
865+
"outputs": [
866+
{
867+
"name": "stdout",
868+
"output_type": "stream",
869+
"text": [
870+
"[12 1 2 3]\n"
871+
]
872+
}
873+
],
874+
"source": [
875+
"#当使用=时相当于多个指针指向同一变量\n",
876+
"b = a\n",
877+
"b[0] = 12\n",
878+
"print(a)"
879+
]
880+
},
881+
{
882+
"cell_type": "code",
883+
"execution_count": 18,
884+
"metadata": {},
885+
"outputs": [
886+
{
887+
"name": "stdout",
888+
"output_type": "stream",
889+
"text": [
890+
"[12 1 2 3]\n"
891+
]
892+
}
893+
],
894+
"source": [
895+
"#使用copy函数则会开辟一块新的空间,将原数据复制一份\n",
896+
"c = a.copy()\n",
897+
"print(c)"
898+
]
899+
},
900+
{
901+
"cell_type": "code",
902+
"execution_count": 19,
903+
"metadata": {},
904+
"outputs": [
905+
{
906+
"name": "stdout",
907+
"output_type": "stream",
908+
"text": [
909+
"[11 1 2 3]\n",
910+
"[12 1 2 3]\n"
911+
]
912+
}
913+
],
914+
"source": [
915+
"c[0] = 11\n",
916+
"print(c)\n",
917+
"print(a)"
918+
]
919+
},
920+
{
921+
"cell_type": "markdown",
922+
"metadata": {},
923+
"source": [
924+
"# 7. 广播"
925+
]
926+
},
927+
{
928+
"cell_type": "code",
929+
"execution_count": 23,
930+
"metadata": {},
931+
"outputs": [
932+
{
933+
"name": "stdout",
934+
"output_type": "stream",
935+
"text": [
936+
"a = [[ 0 1 2 3]\n",
937+
" [ 4 5 6 7]\n",
938+
" [ 8 9 10 11]]\n",
939+
"b = [1 2 3 4]\n"
940+
]
941+
}
942+
],
943+
"source": [
944+
"#numpy中最为重要的就是广播机制,当两个矩阵维度不同时仍然可以进行运算操作\n",
945+
"a = np.arange(12).reshape((3,4))\n",
946+
"b = np.array([1,2,3,4])\n",
947+
"print(\"a = \", a)\n",
948+
"print(\"b = \", b)"
949+
]
950+
},
951+
{
952+
"cell_type": "code",
953+
"execution_count": 24,
954+
"metadata": {},
955+
"outputs": [
956+
{
957+
"name": "stdout",
958+
"output_type": "stream",
959+
"text": [
960+
"[[ 1 3 5 7]\n",
961+
" [ 5 7 9 11]\n",
962+
" [ 9 11 13 15]]\n"
963+
]
964+
}
965+
],
966+
"source": [
967+
"print(a+b)#此时numpy会自动将b与a中的每行分别相加"
968+
]
969+
},
970+
{
971+
"cell_type": "code",
972+
"execution_count": null,
973+
"metadata": {},
974+
"outputs": [],
975+
"source": []
976+
},
977+
{
978+
"cell_type": "code",
979+
"execution_count": null,
980+
"metadata": {},
981+
"outputs": [],
982+
"source": []
983+
},
776984
{
777985
"cell_type": "code",
778986
"execution_count": null,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /