|
33 | 33 | },
|
34 | 34 | {
|
35 | 35 | "cell_type": "code",
|
36 |
| - "execution_count": 52, |
| 36 | + "execution_count": 1, |
37 | 37 | "metadata": {},
|
38 | 38 | "outputs": [],
|
39 | 39 | "source": [
|
|
45 | 45 | },
|
46 | 46 | {
|
47 | 47 | "cell_type": "code",
|
48 |
| - "execution_count": 53, |
| 48 | + "execution_count": 2, |
49 | 49 | "metadata": {},
|
50 | 50 | "outputs": [
|
51 | 51 | {
|
|
54 | 54 | "'1.15.3'"
|
55 | 55 | ]
|
56 | 56 | },
|
57 |
| - "execution_count": 53, |
| 57 | + "execution_count": 2, |
58 | 58 | "metadata": {},
|
59 | 59 | "output_type": "execute_result"
|
60 | 60 | }
|
|
2893 | 2893 | "Theta(-1.2), Theta(2.6)"
|
2894 | 2894 | ]
|
2895 | 2895 | },
|
| 2896 | + { |
| 2897 | + "cell_type": "markdown", |
| 2898 | + "metadata": {}, |
| 2899 | + "source": [ |
| 2900 | + "--------------------------\n", |
| 2901 | + "--------------------------\n", |
| 2902 | + "\n", |
| 2903 | + "## **Advanced NumPy**" |
| 2904 | + ] |
| 2905 | + }, |
| 2906 | + { |
| 2907 | + "cell_type": "code", |
| 2908 | + "execution_count": 6, |
| 2909 | + "metadata": {}, |
| 2910 | + "outputs": [], |
| 2911 | + "source": [ |
| 2912 | + "from IPython.display import Image\n", |
| 2913 | + "from IPython.core.display import display, HTML" |
| 2914 | + ] |
| 2915 | + }, |
| 2916 | + { |
| 2917 | + "cell_type": "code", |
| 2918 | + "execution_count": 18, |
| 2919 | + "metadata": {}, |
| 2920 | + "outputs": [ |
| 2921 | + { |
| 2922 | + "data": { |
| 2923 | + "text/html": [ |
| 2924 | + "<img src=\"https://i.stack.imgur.com/p2PGi.png\"/>" |
| 2925 | + ], |
| 2926 | + "text/plain": [ |
| 2927 | + "<IPython.core.display.Image object>" |
| 2928 | + ] |
| 2929 | + }, |
| 2930 | + "execution_count": 18, |
| 2931 | + "metadata": {}, |
| 2932 | + "output_type": "execute_result" |
| 2933 | + } |
| 2934 | + ], |
| 2935 | + "source": [ |
| 2936 | + "axis_visual = \"https://i.stack.imgur.com/p2PGi.png\"\n", |
| 2937 | + "Image(url=axis_visual)" |
| 2938 | + ] |
| 2939 | + }, |
| 2940 | + { |
| 2941 | + "cell_type": "code", |
| 2942 | + "execution_count": 19, |
| 2943 | + "metadata": {}, |
| 2944 | + "outputs": [ |
| 2945 | + { |
| 2946 | + "data": { |
| 2947 | + "text/html": [ |
| 2948 | + "<img src=\"https://www.oreilly.com/library/view/elegant-scipy/9781491922927/assets/elsp_0105.png\"/>" |
| 2949 | + ], |
| 2950 | + "text/plain": [ |
| 2951 | + "<IPython.core.display.Image object>" |
| 2952 | + ] |
| 2953 | + }, |
| 2954 | + "execution_count": 19, |
| 2955 | + "metadata": {}, |
| 2956 | + "output_type": "execute_result" |
| 2957 | + } |
| 2958 | + ], |
| 2959 | + "source": [ |
| 2960 | + "arr_visual = \"https://www.oreilly.com/library/view/elegant-scipy/9781491922927/assets/elsp_0105.png\"\n", |
| 2961 | + "Image(url=arr_visual)" |
| 2962 | + ] |
| 2963 | + }, |
| 2964 | + { |
| 2965 | + "cell_type": "markdown", |
| 2966 | + "metadata": {}, |
| 2967 | + "source": [ |
| 2968 | + "### **Computing statistics across axes**" |
| 2969 | + ] |
| 2970 | + }, |
| 2971 | + { |
| 2972 | + "cell_type": "code", |
| 2973 | + "execution_count": 8, |
| 2974 | + "metadata": {}, |
| 2975 | + "outputs": [ |
| 2976 | + { |
| 2977 | + "data": { |
| 2978 | + "text/plain": [ |
| 2979 | + "array([[ 0, 1, 2, 3, 4, 5],\n", |
| 2980 | + " [ 6, 7, 8, 9, 10, 11],\n", |
| 2981 | + " [12, 13, 14, 15, 16, 17],\n", |
| 2982 | + " [18, 19, 20, 21, 22, 23],\n", |
| 2983 | + " [24, 25, 26, 27, 28, 29]])" |
| 2984 | + ] |
| 2985 | + }, |
| 2986 | + "execution_count": 8, |
| 2987 | + "metadata": {}, |
| 2988 | + "output_type": "execute_result" |
| 2989 | + } |
| 2990 | + ], |
| 2991 | + "source": [ |
| 2992 | + "arr = np.arange(5 * 6).reshape(5, 6)\n", |
| 2993 | + "arr" |
| 2994 | + ] |
| 2995 | + }, |
| 2996 | + { |
| 2997 | + "cell_type": "code", |
| 2998 | + "execution_count": 13, |
| 2999 | + "metadata": {}, |
| 3000 | + "outputs": [ |
| 3001 | + { |
| 3002 | + "data": { |
| 3003 | + "text/plain": [ |
| 3004 | + "array([[12., 13., 14., 15., 16., 17.]])" |
| 3005 | + ] |
| 3006 | + }, |
| 3007 | + "execution_count": 13, |
| 3008 | + "metadata": {}, |
| 3009 | + "output_type": "execute_result" |
| 3010 | + } |
| 3011 | + ], |
| 3012 | + "source": [ |
| 3013 | + "arr.mean(axis=0, keepdims=True)" |
| 3014 | + ] |
| 3015 | + }, |
| 3016 | + { |
| 3017 | + "cell_type": "code", |
| 3018 | + "execution_count": 15, |
| 3019 | + "metadata": {}, |
| 3020 | + "outputs": [], |
| 3021 | + "source": [ |
| 3022 | + "# what would be the result for:\n", |
| 3023 | + "avg = arr.mean(axis=1, keepdims=True)\n", |
| 3024 | + "\n", |
| 3025 | + "# similarly, max, min, std, etc." |
| 3026 | + ] |
| 3027 | + }, |
| 3028 | + { |
| 3029 | + "cell_type": "markdown", |
| 3030 | + "metadata": {}, |
| 3031 | + "source": [ |
| 3032 | + "### **Broadcasting**" |
| 3033 | + ] |
| 3034 | + }, |
| 3035 | + { |
| 3036 | + "cell_type": "code", |
| 3037 | + "execution_count": 50, |
| 3038 | + "metadata": {}, |
| 3039 | + "outputs": [ |
| 3040 | + { |
| 3041 | + "data": { |
| 3042 | + "text/html": [ |
| 3043 | + "<img src=\"https://jakevdp.github.io/PythonDataScienceHandbook/figures/02.05-broadcasting.png\" width=\"720\" height=\"480\"/>" |
| 3044 | + ], |
| 3045 | + "text/plain": [ |
| 3046 | + "<IPython.core.display.Image object>" |
| 3047 | + ] |
| 3048 | + }, |
| 3049 | + "execution_count": 50, |
| 3050 | + "metadata": {}, |
| 3051 | + "output_type": "execute_result" |
| 3052 | + } |
| 3053 | + ], |
| 3054 | + "source": [ |
| 3055 | + "bcast_visual = \"https://jakevdp.github.io/PythonDataScienceHandbook/figures/02.05-broadcasting.png\"\n", |
| 3056 | + "Image(url=bcast_visual, width=720, height=480)" |
| 3057 | + ] |
| 3058 | + }, |
| 3059 | + { |
| 3060 | + "cell_type": "markdown", |
| 3061 | + "metadata": {}, |
| 3062 | + "source": [ |
| 3063 | + "### **RandomState**\n", |
| 3064 | + "\n", |
| 3065 | + "For reproducing the results, fix the seed:\n", |
| 3066 | + "\n", |
| 3067 | + " A fixed seed and a fixed series of calls to 'RandomState' methods using the same parameters will always produce the same results up to roundoff error except when the values were incorrect." |
| 3068 | + ] |
| 3069 | + }, |
| 3070 | + { |
| 3071 | + "cell_type": "code", |
| 3072 | + "execution_count": 72, |
| 3073 | + "metadata": {}, |
| 3074 | + "outputs": [ |
| 3075 | + { |
| 3076 | + "data": { |
| 3077 | + "text/plain": [ |
| 3078 | + "array([[2, 3, 1, 3, 3, 0],\n", |
| 3079 | + " [1, 1, 1, 3, 2, 1],\n", |
| 3080 | + " [4, 3, 0, 2, 4, 4]])" |
| 3081 | + ] |
| 3082 | + }, |
| 3083 | + "execution_count": 72, |
| 3084 | + "metadata": {}, |
| 3085 | + "output_type": "execute_result" |
| 3086 | + } |
| 3087 | + ], |
| 3088 | + "source": [ |
| 3089 | + "rng = np.random.RandomState(seed=42)\n", |
| 3090 | + "data = rng.randint(-1, 5, (3, 6))\n", |
| 3091 | + "data" |
| 3092 | + ] |
| 3093 | + }, |
| 3094 | + { |
| 3095 | + "cell_type": "markdown", |
| 3096 | + "metadata": {}, |
| 3097 | + "source": [ |
| 3098 | + "### **Sampling from Distributions**" |
| 3099 | + ] |
| 3100 | + }, |
| 3101 | + { |
| 3102 | + "cell_type": "code", |
| 3103 | + "execution_count": 75, |
| 3104 | + "metadata": {}, |
| 3105 | + "outputs": [ |
| 3106 | + { |
| 3107 | + "data": { |
| 3108 | + "text/plain": [ |
| 3109 | + "array([[[ 0.49671415, -0.1382643 ],\n", |
| 3110 | + " [ 0.64768854, 1.52302986],\n", |
| 3111 | + " [-0.23415337, -0.23413696],\n", |
| 3112 | + " [ 1.57921282, 0.76743473]],\n", |
| 3113 | + "\n", |
| 3114 | + " [[-0.46947439, 0.54256004],\n", |
| 3115 | + " [-0.46341769, -0.46572975],\n", |
| 3116 | + " [ 0.24196227, -1.91328024],\n", |
| 3117 | + " [-1.72491783, -0.56228753]],\n", |
| 3118 | + "\n", |
| 3119 | + " [[-1.01283112, 0.31424733],\n", |
| 3120 | + " [-0.90802408, -1.4123037 ],\n", |
| 3121 | + " [ 1.46564877, -0.2257763 ],\n", |
| 3122 | + " [ 0.0675282 , -1.42474819]]])" |
| 3123 | + ] |
| 3124 | + }, |
| 3125 | + "execution_count": 75, |
| 3126 | + "metadata": {}, |
| 3127 | + "output_type": "execute_result" |
| 3128 | + } |
| 3129 | + ], |
| 3130 | + "source": [ |
| 3131 | + "# for reproducibility\n", |
| 3132 | + "rng = np.random.RandomState(seed=42)\n", |
| 3133 | + "\n", |
| 3134 | + "std_normal_dist = rng.standard_normal(size=(3, 4, 2))\n", |
| 3135 | + "std_normal_dist" |
| 3136 | + ] |
| 3137 | + }, |
| 3138 | + { |
| 3139 | + "cell_type": "code", |
| 3140 | + "execution_count": 78, |
| 3141 | + "metadata": {}, |
| 3142 | + "outputs": [ |
| 3143 | + { |
| 3144 | + "data": { |
| 3145 | + "text/plain": [ |
| 3146 | + "array([[[ 0.49671415, -0.1382643 ],\n", |
| 3147 | + " [ 0.64768854, 1.52302986],\n", |
| 3148 | + " [-0.23415337, -0.23413696],\n", |
| 3149 | + " [ 1.57921282, 0.76743473]],\n", |
| 3150 | + "\n", |
| 3151 | + " [[-0.46947439, 0.54256004],\n", |
| 3152 | + " [-0.46341769, -0.46572975],\n", |
| 3153 | + " [ 0.24196227, -1.91328024],\n", |
| 3154 | + " [-1.72491783, -0.56228753]],\n", |
| 3155 | + "\n", |
| 3156 | + " [[-1.01283112, 0.31424733],\n", |
| 3157 | + " [-0.90802408, -1.4123037 ],\n", |
| 3158 | + " [ 1.46564877, -0.2257763 ],\n", |
| 3159 | + " [ 0.0675282 , -1.42474819]]])" |
| 3160 | + ] |
| 3161 | + }, |
| 3162 | + "execution_count": 78, |
| 3163 | + "metadata": {}, |
| 3164 | + "output_type": "execute_result" |
| 3165 | + } |
| 3166 | + ], |
| 3167 | + "source": [ |
| 3168 | + "# if reproducibility matters ...\n", |
| 3169 | + "rng = np.random.RandomState(seed=42)\n", |
| 3170 | + "\n", |
| 3171 | + "# an array of 10 points randomly sampled from a normal distribution\n", |
| 3172 | + "# loc=mean, scale=std deviation\n", |
| 3173 | + "rng.normal(loc=0.0, scale=1.0, size=(3, 4, 2))" |
| 3174 | + ] |
| 3175 | + }, |
| 3176 | + { |
| 3177 | + "cell_type": "code", |
| 3178 | + "execution_count": 77, |
| 3179 | + "metadata": {}, |
| 3180 | + "outputs": [ |
| 3181 | + { |
| 3182 | + "data": { |
| 3183 | + "text/plain": [ |
| 3184 | + "array([[[0.37454012, 0.95071431],\n", |
| 3185 | + " [0.73199394, 0.59865848],\n", |
| 3186 | + " [0.15601864, 0.15599452],\n", |
| 3187 | + " [0.05808361, 0.86617615]],\n", |
| 3188 | + "\n", |
| 3189 | + " [[0.60111501, 0.70807258],\n", |
| 3190 | + " [0.02058449, 0.96990985],\n", |
| 3191 | + " [0.83244264, 0.21233911],\n", |
| 3192 | + " [0.18182497, 0.18340451]],\n", |
| 3193 | + "\n", |
| 3194 | + " [[0.30424224, 0.52475643],\n", |
| 3195 | + " [0.43194502, 0.29122914],\n", |
| 3196 | + " [0.61185289, 0.13949386],\n", |
| 3197 | + " [0.29214465, 0.36636184]]])" |
| 3198 | + ] |
| 3199 | + }, |
| 3200 | + "execution_count": 77, |
| 3201 | + "metadata": {}, |
| 3202 | + "output_type": "execute_result" |
| 3203 | + } |
| 3204 | + ], |
| 3205 | + "source": [ |
| 3206 | + "# uniform distribution\n", |
| 3207 | + "rng = np.random.RandomState(seed=42)\n", |
| 3208 | + "\n", |
| 3209 | + "rng.uniform(low=0, high=1.0, size=(3, 4, 2))" |
| 3210 | + ] |
| 3211 | + }, |
| 3212 | + { |
| 3213 | + "cell_type": "code", |
| 3214 | + "execution_count": null, |
| 3215 | + "metadata": {}, |
| 3216 | + "outputs": [], |
| 3217 | + "source": [] |
| 3218 | + }, |
2896 | 3219 | {
|
2897 | 3220 | "cell_type": "markdown",
|
2898 | 3221 | "metadata": {},
|
|
2906 | 3229 | "- https://docs.scipy.org/doc/numpy/reference/\n",
|
2907 | 3230 | "- Your own imagination & dexterity!"
|
2908 | 3231 | ]
|
| 3232 | + }, |
| 3233 | + { |
| 3234 | + "cell_type": "code", |
| 3235 | + "execution_count": null, |
| 3236 | + "metadata": {}, |
| 3237 | + "outputs": [], |
| 3238 | + "source": [] |
2909 | 3239 | }
|
2910 | 3240 | ],
|
2911 | 3241 | "metadata": {
|
|
0 commit comments