The Correct Way to Generate Random Numbers in Python with NumPy
|

The Correct Way to Generate Random Numbers in Python with NumPy

Random number generation is a common programming task that is required for many different programs and applications. In Python, the most common way to generate random numbers is arguably the NumPy module. NumPy is fast, reliable, easy to install, and relied on by many programs. As of 2022 (numpy version 1.22) the proper way to…

10 Ways to Initialize a Numpy Array (How to create numpy arrays)
|

10 Ways to Initialize a Numpy Array (How to create numpy arrays)

In Python, matrix-like data structures are most commonly represented with numpy arrays. The numpy Python package has been well-developed for efficient computation of matrices. The first step to using numpy arrays is to initialize, or create, an array. In Python, there are many ways to create a numpy array. In this article, I’ll demonstrate how to create numpy arrays in ten different ways.

|

How to Use numpy.where() in Python with Examples

npwhere ¶ The where function from numpy is a powerful way to vectorize if/else statements across entire arrays. There are two primary ways to use numpy.where. First, numpy.where can be used to idenefity array indices where a condition is true (or false). Second, it can be used to index and change values where a condition…

How to Check if NumPy is Installed and Find Your NumPy Version
|

How to Check if NumPy is Installed and Find Your NumPy Version

The numpy Python module is widely used for many different analyses and as a dependency for many other Python packages. In many instances, it is necessary to determine if numpy is installed and which numpy version is installed. This article will show you multiple ways to determine your numpy version and installation status. Check if…

Sort NumPy Arrays By Columns or Rows
|

Sort NumPy Arrays By Columns or Rows

NumPy is a fundamental module used in many Python programs and analyses because it conducts numerical computations very efficiently. However, for those new to NumPy, it can be difficult to grasp at first. Specifically, understanding array indexing and sorting quickly becomes complex. Fortunately, NumPy has some built-in functions that make performing basic sorting operations quite…

Vectorize Moving Window Grid Operations on NumPy Arrays
| |

Vectorize Moving Window Grid Operations on NumPy Arrays

There’s a good chance you’ve done something today that used a sliding window (also known as a moving window) and you didn’t even know it. Have you done any photo editing? Many editing algorithms are based on moving windows. Do you do terrain analysis in GIS? Most topographic raster metrics (slope, aspect, hillshade, etc.) are…

Python: Geographic Object-Based Image Analysis (GeOBIA) – Part 2: Image Classification
| | | |

Python: Geographic Object-Based Image Analysis (GeOBIA) – Part 2: Image Classification

Use the random forests algorithm to classify image segments into land cover categories. This post is a continuation of Geographic Object-Based Image Analysis (GeOBIA). Herein, we use data describing land cover types to train and test the accuracy of a random forests classifier. Land cover data were created in the previous post. Step-be-step video instructions…

Python: Geographic Object-Based Image Analysis (GeOBIA) – Part 1: Image Segmentation
| | | |

Python: Geographic Object-Based Image Analysis (GeOBIA) – Part 1: Image Segmentation

This tutorial will walk you through segmenting and classifying high resolution imagery using Python. Part 1 of this tutorial teaches how to segment images with Python. After you have completed Part 1, Part 2 will teach how to use machine learning methods to classify segments into land cover types with Python. YouTube videos give step-by-step…

|

numpy: Methods For Creating Arrays

It is tedious, and not practical, to manually type in values for array creation. In this tutorial we will go through methods for automating array creation and importing tabular data into numpy arrays. Creating empty arrays With numpy you don’t actually create an ‘empty’ array. But you can create an array without intializing specific values. This can be useful…

|

numpy: Array shapes and reshaping arrays

We’ve gone through the basics to manually create arrays of different dimensions and shapes. There will be times that you will want to query array shapes, or automatically reshape arrays. This tutorial will show you how to use numpy.shape and numpy.reshape to query and alter array shapes for 1D, 2D, and 3D arrays. Different methods are required to find…

|

numpy: Creating Arrays

Creating a simple array After numpy is installed, we can begin to create arrays. First, we’ll need to import numpy into our python project. Here I use the statement, import numpy as np, to limit my typing later. This code will allow me to use np in my script to represent instead of typing the full numpy everytime. Then, we can create a simpy…

|

numpy: Install numpy

Check Installation First, check to see if you already have numpy installed. From the terminal, you can use pip to do this. If numpy is installed you will get output similar to this. If numpy is not installed no output will be shown. Install numpy numpy can be installed simply using pip. Check version You can check the numpy version using pip show as demonstrated above. Video tutorial This…

|

numpy: Introduction to numpy

Requirements: Python version 2.7 or greater Beginner skills with python Desire to learn numpy Objectives: Teach the basics of using numpy in python Provide data science applications of numpy Introduce advanced numpy concepts What is numpy? numpy is a powerful python package for handling arrays. It uses C libraries to optimize procedures that would otherwise be slow/computationally expensive in python. What can I do…