{"id":2464,"date":"2026-02-02T07:15:27","date_gmt":"2026-02-02T07:15:27","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2464"},"modified":"2026-02-02T07:15:27","modified_gmt":"2026-02-02T07:15:27","slug":"chapter-3-numpy-introduction","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-3-numpy-introduction\/","title":{"rendered":"Chapter 3: NumPy Introduction"},"content":{"rendered":"<h3 dir=\"auto\">NumPy \u2013 The Very First Honest Explanation<\/h3>\n<p dir=\"auto\"><strong>NumPy = Numerical Python<\/strong><\/p>\n<p dir=\"auto\">It is <strong>the most important library<\/strong> in the entire Python data\/science\/machine-learning world.<\/p>\n<p dir=\"auto\">Almost everything serious that happens with numbers in Python uses NumPy under the hood:<\/p>\n<ul dir=\"auto\">\n<li>pandas (data frames\/tables)<\/li>\n<li>matplotlib, seaborn, plotly (plotting)<\/li>\n<li>scikit-learn (machine learning)<\/li>\n<li>tensorflow, pytorch, jax (deep learning)<\/li>\n<li>scipy (scientific computing)<\/li>\n<li>opencv (image &amp; video processing)<\/li>\n<li>statsmodels, pingouin (statistics)<\/li>\n<li>financial libraries, physics simulations, bioinformatics\u2026<\/li>\n<\/ul>\n<p dir=\"auto\">If you want to do <strong>anything serious with numbers<\/strong> in Python, you <strong>must<\/strong> learn NumPy first.<\/p>\n<p dir=\"auto\"><strong>The most important mindset change you need right now:<\/strong><\/p>\n<p dir=\"auto\">Python lists \u2192 good for general things, shopping lists, names, mixed types \u2192 very slow when doing math on thousands\/millions of numbers<\/p>\n<p dir=\"auto\">NumPy arrays \u2192 made only for numbers (all elements same type) \u2192 extremely fast mathematical operations \u2192 thinks in <strong>whole arrays<\/strong> instead of item-by-item<\/p>\n<p dir=\"auto\">This single difference makes code <strong>10\u2013100\u00d7 faster<\/strong> and usually much shorter and cleaner.<\/p>\n<h3 dir=\"auto\">Let\u2019s Start \u2013 First Code You Should Type<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>import numpy as np<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Almost every person in data science \/ ML \/ scientific computing uses np as the short name. Just accept it \u2014 it\u2019s the universal convention.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Let's create our very first NumPy array\r\nscores = np.array([78, 92, 65, 84, 71, 88])\r\nprint(scores)\r\n# output: [78 92 65 84 71 88]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Compare with normal Python list:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>python_list = [78, 92, 65, 84, 71, 88]\r\nprint(python_list)\r\n# output: [78, 92, 65, 84, 71, 88]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">They look almost the same\u2026 but they are <strong>very different<\/strong> inside.<\/p>\n<h3 dir=\"auto\">Why NumPy Feels So Different \u2013 First Magic Example<\/h3>\n<p dir=\"auto\">Let\u2019s say we want to give everyone +5 bonus points.<\/p>\n<p dir=\"auto\"><strong>Normal Python way<\/strong> (slow and ugly when list is big):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>new_scores = []\r\nfor score in python_list:\r\n    new_scores.append(score + 5)\r\nprint(new_scores)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>NumPy way<\/strong> (clean &amp; fast):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>bonus = scores + 5\r\nprint(bonus)\r\n# [83 97 70 89 76 93]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>No loop!<\/strong> NumPy did the addition on <strong>every element automatically<\/strong>.<\/p>\n<p dir=\"auto\">This is called <strong>vectorization<\/strong> \u2014 and it is the #1 reason people love NumPy.<\/p>\n<h3 dir=\"auto\">Most Common First Arrays You Will Create<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># 1. From a list (most common way at beginning)\r\na = np.array([1.5, 2.7, 3.2, 4.0])\r\n\r\n# 2. All zeros (very useful to initialize)\r\nzeros = np.zeros(10)          # 1D\r\nzeros2d = np.zeros((4, 6))    # 4 rows, 6 columns\r\n\r\n# 3. All ones\r\nones = np.ones((3, 5))\r\n\r\n# 4. Fill with same number\r\nprices = np.full((2, 8), 99.99)\r\n\r\n# 5. Sequence of numbers (very common!)\r\nar = np.arange(0, 20, 2)          # 0,2,4,...,18\r\nlin = np.linspace(0, 1, 11)       # 0.0, 0.1, ..., 1.0\r\n\r\n# 6. Random numbers \u2013 you will use these A LOT\r\nnp.random.seed(42)                # makes random results repeatable\r\n\r\nuniform = np.random.rand(5)       # 5 random numbers between 0 and 1\r\nnormal  = np.random.randn(1000)   # 1000 numbers from normal distribution\r\ndice    = np.random.randint(1, 7, size=20)  # 20 dice rolls<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">The 4 Most Important Properties \u2013 Check These Every Time<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>data = np.random.randint(0, 100, size=(3, 4))\r\n\r\nprint(data)\r\n# example output:\r\n# [[45 68 37 91]\r\n#  [12 73  8 29]\r\n#  [64 19 83 52]]\r\n\r\nprint(data.shape)     # (3, 4)          \u2190 MOST IMPORTANT LINE\r\nprint(data.ndim)      # 2               \u2190 how many dimensions\r\nprint(data.size)      # 12              \u2190 total numbers\r\nprint(data.dtype)     # int64           \u2190 what type are the numbers<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Real-world examples you will see very soon:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Machine learning dataset\r\nX.shape \u2192 (number_of_samples, number_of_features)    # e.g. (10000, 784)\r\n\r\n# Images (very common)\r\nimage.shape \u2192 (height, width, 3)           # RGB image\r\nbatch_images.shape \u2192 (32, 224, 224, 3)     # 32 images of 224\u00d7224 pixels<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">First Useful Math You Can Do Immediately<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>temps = np.array([23.4, 25.1, 19.8, 28.7, 22.0])\r\n\r\nprint(temps * 1.8 + 32)               # convert \u00b0C \u2192 \u00b0F\r\n# [74.12  77.18  67.64  83.66  71.6 ]\r\n\r\nprint(temps ** 2)                     # square each value\r\nprint(np.sqrt(temps))                 # square root\r\nprint(np.round(temps))                # round to nearest integer\r\nprint(temps &gt; 24)                     # boolean array: True\/False<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">All these operations happen <strong>element by element<\/strong> \u2014 no loops needed.<\/p>\n<h3 dir=\"auto\">Very Common Beginner Mistake #1 \u2013 Copying<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>a = np.array([10, 20, 30, 40])\r\nb = a                # \u2190 DANGER! This is NOT a real copy\r\n\r\nb[0] = 999\r\nprint(a)             # [999  20  30  40]   \u2190 a also changed!<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Correct ways to copy<\/strong>:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>c = a.copy()              # safest and clearest\r\nd = np.copy(a)\r\ne = a[:]                  # usually works, but be careful with slices<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Rule to remember<\/strong>: b = a \u2192 same array (just two names for same data) b = a.copy() \u2192 new independent array<\/p>\n<h3 dir=\"auto\">Quick Summary \u2013 Your First NumPy Survival Kit<\/h3>\n<p dir=\"auto\">What you should be able to do after this introduction:<\/p>\n<ul dir=\"auto\">\n<li>Import NumPy<\/li>\n<li>Create 1D and 2D arrays<\/li>\n<li>Use np.zeros, np.ones, np.arange, np.linspace<\/li>\n<li>Create random numbers (rand, randn, randint)<\/li>\n<li>Check .shape, .ndim, .dtype<\/li>\n<li>Do math on whole arrays (+, -, *, \/, **, sqrt, round\u2026)<\/li>\n<li>Understand why we avoid loops<\/li>\n<li>Know how to safely copy arrays<\/li>\n<\/ul>\n<h3 dir=\"auto\">Where should we go next?<\/h3>\n<p dir=\"auto\">Pick one (or tell me what you feel you need most):<\/p>\n<ol dir=\"auto\">\n<li>More about <strong>creating arrays<\/strong> (many more realistic examples)<\/li>\n<li><strong>Indexing &amp; slicing<\/strong> in detail (very important)<\/li>\n<li><strong>Broadcasting<\/strong> (the magic that makes shapes work together)<\/li>\n<li><strong>Boolean masks<\/strong> and filtering data<\/li>\n<li>First <strong>statistics<\/strong> (mean, std, min, max, percentiles\u2026)<\/li>\n<li><strong>Reshaping<\/strong> arrays (very common in machine learning)<\/li>\n<li><strong>Common beginner mistakes<\/strong> and how to avoid them<\/li>\n<li>Small <strong>realistic mini-project<\/strong> (e.g. grade calculator, simple data cleaning)<\/li>\n<\/ol>\n<p dir=\"auto\">Just say a number or describe what feels most useful right now \u2014 we continue from exactly where you are.<\/p>\n<p dir=\"auto\">You\u2019re doing great \u2014 let\u2019s keep going! \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy \u2013 The Very First Honest Explanation NumPy = Numerical Python It is the most important library in the entire Python data\/science\/machine-learning world. Almost everything serious that happens with numbers in Python uses NumPy&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-2464","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2464"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2464\/revisions"}],"predecessor-version":[{"id":2465,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2464\/revisions\/2465"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}