NumPy Random: Numbers, Ranges, Seeds and Arrays

NumPy random: the Command Prompt showing how to generate a random number between 0 and 10 with the integers method

NumPy random numbers come from a generator you create once and then ask for values: The older np.random.rand() and np.random.randint() still work and appear in most tutorials. They differ in one respect that matters, covered further down. Starting with the question most people arrive with: a random number in a given range. Output is from … Read more >>

Inverse of a Matrix in Python (NumPy and Pure Python)

Inverse of matrix in Python: Command Prompt output of numpy.linalg.inv and an identity check

Use numpy.linalg.inv() to get the inverse of a matrix in Python. It returns the matrix that, multiplied by the original, gives the identity matrix: Every result here was checked on Python 3.12.5 with NumPy 2.5.3, including the error you get for a matrix that can’t be inverted. You’ll see how to invert and verify a … Read more >>

Build a Simple MNIST Convnet in Keras (Step by Step)

MNIST convnet in Keras: predictions on the first 12 MNIST test digits with the model's confidence

A reader asked me for the smallest Keras model that reads handwritten digits well enough to trust. The answer is a simple MNIST convnet: two Conv2D + MaxPooling2D blocks and one Dense softmax layer. Trained for 3 epochs on my laptop’s CPU, it scored 98.54% on the 10,000 test images. This is the finished model … Read more >>

51 Scikit Learn Interview Questions And Answers

Scikit Learn Interview Questions And Answers

Getting a handle on Scikit-Learn is pretty much a must for anyone diving into machine learning or data science. This Python library packs a bunch of tools for data preprocessing, model training, and evaluation. If you can tackle interview questions about Scikit-Learn, you’ll show off both real-world skills and the kind of theory employers want … Read more >>

How to Create Multiple Bar Charts in Matplotlib (Grouped, Side by Side)

Matplotlib multiple bar chart: grouped_bar() drawing 2026 quarterly sales for four US regions side by side

Many guides still draw a Matplotlib multiple bar chart by hand, shifting each series with x – width/2 offsets. It works, but you’ll redo the math for every new series. Matplotlib 3.11 has a cleaner answer: ax.grouped_bar(data, tick_labels=…) places the side-by-side bars for you. In this tutorial, I’ll build a matplotlib multiple bar chart four … Read more >>

QSpacerItem in PyQt6: Build a Form with Spacers

QSpacerItem in PyQt6: support ticket window with spacers pushing the buttons right and down

A support team asked me for a ticket window where the buttons stay in the bottom-right corner, however big the window gets. That’s the job of QSpacerItem in PyQt6: an invisible, stretchable gap you add to a layout with addSpacerItem(). Here’s what we’re building: In this tutorial, I’ll build that window one spacer at a … Read more >>

How to Concatenate Arrays in Python

Python concatenate arrays: Command Prompt output of np.concatenate joining two 2D sales arrays along axis 0 and axis 1

A retail client sent me first-quarter sales for two store regions as separate NumPy arrays and wanted one array for a report. That’s the everyday reason to concatenate arrays in Python. The obvious east + west doesn’t join anything; on NumPy arrays, + adds values position by position. Quick answer: pass the arrays as a … Read more >>

Fix: numpy.core.multiarray failed to import (NumPy 2)

numpy core multiarray failed to import: Command Prompt showing the NumPy 2 compatibility error

ImportError: numpy.core.multiarray failed to import means a package with compiled code was built for NumPy 1.x, but you have NumPy 2 installed. Fix it by upgrading that package, or by going back to NumPy 1.x: I reproduced this error for real in a fresh virtual environment, using matplotlib 3.7.5 with NumPy 2.1.3 on Python 3.11, … Read more >>

Monte Carlo Simulation in Python (With NumPy Examples)

Monte Carlo simulation in Python: histogram of 10,000 simulated retirement balances

A Monte Carlo simulation in Python answers “what’s likely to happen?” by running a random experiment thousands of times and looking at the results. NumPy makes it fast because you can generate millions of random numbers at once: Every number here comes from a seeded run of Python 3.12.5 with NumPy 2.5.3, so you’ll get … Read more >>