Featured image of post Numpy应用

Numpy应用

本文详细介绍了如何使用Numpy中的现有方法。

{ “cells”: [ { “cell_type”: “code”, “id”: “initial_id”, “metadata”: { “collapsed”: true, “ExecuteTime”: { “end_time”: “2025-12-02T13:24:43.698426Z”, “start_time”: “2025-12-02T13:24:43.562632Z” } }, “source”: [ “import numpy as np\n”, “\n”, “a = np.array([1,2,3])\n”, “\n”, “print(a)” ], “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “[1 2 3]\n” ] } ], “execution_count”: 2 }, { “metadata”: { “ExecuteTime”: { “end_time”: “2025-12-02T06:43:07.210254Z”, “start_time”: “2025-12-02T06:43:07.205078Z” } }, “cell_type”: “code”, “source”: [ “temps = np.array([28, 30, 29, 31, 32, 30, 29])\n”, “print(temps)\n”, “print(‘平均气温: ‘,’%.3f’%np.mean(temps))” ], “id”: “272fc7725db6cf66”, “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “[28 30 29 31 32 30 29]\n”, “平均气温: 29.857\n” ] } ], “execution_count”: 6 }, { “metadata”: { “ExecuteTime”: { “end_time”: “2025-12-02T13:30:56.404483Z”, “start_time”: “2025-12-02T13:30:56.398949Z” } }, “cell_type”: “code”, “source”: [ “arr = np.arange(1, 13)\n”, “print(arr)\n”, “brr = np.reshape(arr, (3, 4))\n”, “print(brr)\n”, “print(‘每行的和’, np.sum(brr, axis=1))\n”, “print(‘每列的和’, np.sum(brr, axis=0))\n”, “print(np.reshape(brr,(12)))\n” ], “id”: “763fe59e63ac8037”, “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “[ 1 2 3 4 5 6 7 8 9 10 11 12]\n”, “[[ 1 2 3 4]\n”, " [ 5 6 7 8]\n", " [ 9 10 11 12]]\n", “每行的和 [10 26 42]\n”, “每列的和 [15 18 21 24]\n” ] } ], “execution_count”: 6 }, { “metadata”: { “ExecuteTime”: { “end_time”: “2025-12-02T13:34:38.613534Z”, “start_time”: “2025-12-02T13:34:38.608127Z” } }, “cell_type”: “code”, “source”: [ “np.random.seed(0)\n”, “arr = np.random.randint(0, 20, (5, 5))\n”, “print(arr)\n”, “print(arr[arr > 10])\n”, “arr[arr > 10] = 0\n”, “print(arr)” ], “id”: “679002567acd44cf”, “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “[[12 15 0 3 3]\n”, " [ 7 9 19 18 4]\n", " [ 6 12 1 6 7]\n", " [14 17 5 13 8]\n", " [ 9 19 16 19 5]]\n", “[12 15 19 18 12 14 17 13 19 16 19]\n”, “[[0 0 0 3 3]\n”, " [7 9 0 0 4]\n", " [6 0 1 6 7]\n", " [0 0 5 0 8]\n", " [9 0 0 0 5]]\n" ] } ], “execution_count”: 12 }, { “metadata”: { “ExecuteTime”: { “end_time”: “2025-12-02T13:40:40.797491Z”, “start_time”: “2025-12-02T13:40:40.791003Z” } }, “cell_type”: “code”, “source”: [ “arr = np.array([120, 135, 110, 125, 130, 140])\n”, “print(arr)\n”, “print(‘销售额的总和: ‘, np.sum(arr))\n”, “print(‘销售额的均值: ‘, ‘%.2f’%np.mean(arr))\n”, “print(‘销售额的方差: ‘, ‘%.2f’%np.var(arr))\n”, “print(‘销售额最高的月份: ‘, np.argmax(arr) + 1)\n”, “print(‘销售额最低的月份: ‘, np.argmin(arr) + 1)” ], “id”: “d1c8543983210811”, “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “[120 135 110 125 130 140]\n”, “销售额的总和: 760\n”, “销售额的均值: 126.67\n”, “销售额的方差: 97.22\n”, “销售额最高的月份: 6\n”, “销售额最低的月份: 3\n” ] } ], “execution_count”: 15 }, { “metadata”: { “ExecuteTime”: { “end_time”: “2025-12-02T13:48:06.397821Z”, “start_time”: “2025-12-02T13:48:06.391050Z” } }, “cell_type”: “code”, “source”: [ “arr = np.array([1, 2, 3])\n”, “brr = np.array([4, 5, 6])\n”, “crr = np.concatenate([arr, brr])\n”, “print(crr)\n”, “drr = np.stack([arr, brr], axis=0)\n”, “print(drr)” ], “id”: “eb19da13927cd88a”, “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “[1 2 3 4 5 6]\n”, “[[1 2 3]\n”, " [4 5 6]]\n" ] } ], “execution_count”: 22 }, { “metadata”: { “ExecuteTime”: { “end_time”: “2025-12-02T14:01:04.800240Z”, “start_time”: “2025-12-02T14:01:04.794506Z” } }, “cell_type”: “code”, “source”: [ “arr = np.array([2, 1, 2, 3, 1, 4, 3])\n”, “u_arr, counts = np.unique(arr, return_counts=True) # 去重\n”, “print(u_arr)\n”, “print(counts)\n”, “\n”, “for i in range(len(u_arr)):\n”, " # print(str(u_arr[i]) + ‘:’ + str(len(arr[arr == u_arr[i]])))\n", " print(u_arr[i], ‘:’, len(arr[arr == u_arr[i]]))\n" ], “id”: “be1732eafc359ab9”, “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “[1 2 3 4]\n”, “[2 2 2 1]\n”, “1 : 2\n”, “2 : 2\n”, “3 : 2\n”, “4 : 1\n” ] } ], “execution_count”: 37 }, { “metadata”: { “ExecuteTime”: { “end_time”: “2025-12-02T14:07:05.780338Z”, “start_time”: “2025-12-02T14:07:05.772126Z” } }, “cell_type”: “code”, “source”: [ “money = np.array([20, 25, 22, 30, 28])\n”, “cost = np.array([15, 18, 16, 22 ,20])\n”, “profit = money - cost\n”, “print(‘每天的利润’, profit)\n”, “print(‘利润的平均值: ‘, np.mean(profit))\n”, “print(‘利润的标准差: ‘, np.std(profit))\n”, “print(‘利润最高的天数: ‘, np.where(profit == np.max(profit))[0] + 1)” ], “id”: “c5a73ebddb0db89”, “outputs”: [ { “name”: “stdout”, “output_type”: “stream”, “text”: [ “每天的利润 [5 7 6 8 8]\n”, “利润的平均值: 6.8\n”, “利润的标准差: 1.16619037896906\n”, “利润最高的天数: (array([3, 4], dtype=int64),)\n” ] } ], “execution_count”: 50 } ], “metadata”: { “kernelspec”: { “display_name”: “Python 3”, “language”: “python”, “name”: “python3” }, “language_info”: { “codemirror_mode”: { “name”: “ipython”, “version”: 2 }, “file_extension”: “.py”, “mimetype”: “text/x-python”, “name”: “python”, “nbconvert_exporter”: “python”, “pygments_lexer”: “ipython2”, “version”: “2.7.6” } }, “nbformat”: 4, “nbformat_minor”: 5 }

✨ 本站由 Hugo + Stack 主题搭建 | 不忘初心,慢慢成长 ✨
使用 Hugo 构建
主题 StackJimmy 设计