Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Niharika Aitam
Page 6 of 14
How to check whether the element of a given NumPy array is non-zero?
NumPy provides several methods to check whether elements in an array are non-zero. Each approach serves different purposes: checking if all elements are non-zero, finding indices of non-zero elements, or counting non-zero elements. Using np.all() for Boolean Check The np.all() function checks if all elements in an array are non-zero (truthy). It returns True if all elements are non-zero, False otherwise ? import numpy as np arr = np.array([2, 5, 8, 11, 14, 17]) print("Original array:", arr) if np.all(arr): print("All elements are non-zero") else: print("Array contains ...
Read MoreHow to check whether the day is a weekday or not using Pandas in Python?
Python pandas library provides several functions to check whether a day is a weekday or weekend, including weekday(), day_name(), and isoweekday(). These functions work with pandas datetime objects to identify the day of the week. Using the weekday() Function The weekday() function returns the day of the week as an integer, where 0 represents Monday, 1 represents Tuesday, and so on up to 6 for Sunday ? import pandas as pd date = pd.to_datetime('2023-03-25') day_number = date.weekday() print(f"Day number: {day_number}") if day_number < 5: print(f"{date.date()} is a Weekday") else: ...
Read MoreHow to check whether specified values are present in NumPy array?
NumPy provides several methods to check whether specified values are present in an array. The most common approaches are using the in keyword, np.isin() function, and np.where() function. Using the "in" Keyword The in keyword checks if a single element exists in the array ? import numpy as np arr = np.array([10, 30, 2, 40.3, 56, 456, 32, 4]) print("The Original array:", arr) if 4 in arr: print("The element 4 is present in the array.") else: print("The element 4 is not present in the array.") ...
Read MoreHow to check the execution time of Python script?
Measuring execution time is crucial for optimizing Python scripts and identifying performance bottlenecks. Python provides several built-in modules like time, timeit, and cProfile to measure how long your code takes to run. Using the time Module The time module provides a simple way to measure execution time by recording start and end timestamps. The time() function returns the current time in seconds since the Unix epoch ? Example import time # Record start time start_time = time.time() # Code to measure numbers = [] for i in range(100000): numbers.append(i ...
Read MoreHow to check multiple variables against a value in Python?
When working with multiple variables in Python, you often need to check if they match certain values or conditions. Python provides several approaches to efficiently compare multiple variables against values using logical operators, built-in functions, and data structures. Using Logical 'and' and 'or' Operators The most straightforward approach uses logical operators. The and operator requires all conditions to be true, while or requires at least one condition to be true. Using 'and' Operator Check if all variables equal specific values ? x = 10 y = 20 z = 30 if x == ...
Read MoreHow to Check Loading Time of Website using Python
Website loading time is a crucial performance metric. In Python, we can measure how long a website takes to respond by recording timestamps before and after making an HTTP request. This involves using the requests module for web requests and the time module for timing measurements. Basic Approach The concept is simple: record the time before making a request, send the request, then calculate the difference. Here's the basic implementation ? import requests import time url = "https://www.tutorialspoint.com" start_time = time.time() response = requests.get(url) end_time = time.time() loading_time = end_time - start_time print(f"Loading time ...
Read MoreHow to check if Time Series Data is Stationary with Python?
Time series data is a collection of data points recorded at regular intervals. To make accurate forecasts, it's essential to check if the data is stationary – meaning its statistical properties don't change over time. Python provides several methods to test stationarity. What is Stationarity? A time series is stationary if its mean, variance, and autocorrelation remain constant over time. Non-stationary data shows trends, seasonality, or changing variance that can mislead forecasting models. Augmented Dickey-Fuller (ADF) Test The ADF test checks for unit roots in time series data. It tests the null hypothesis that the data ...
Read MoreHow to Check if Tensorflow is Using GPU?
GPU is abbreviated as Graphics Processing Unit. It is a specialized processor designed to handle the complex and repetitive calculations required for video encoding or decoding, graphics rendering and other computational intensive tasks. It is mainly suited to perform large-scale parallel computations, which makes it ideal for machine learning and other data-based applications. GPUs in machine learning have become more popular as they reduce the time required to train complex neural networks. TensorFlow, PyTorch, and Keras are built-in frameworks of machine learning which support GPU acceleration. The following are the steps to check if TensorFlow is using ...
Read MoreHow to Check If Python Package Is Installed?
In Python, we often need to check if a package is installed before using it in our programs. Python provides several built-in methods to verify package installation in your environment. A package is a directory containing one or more Python modules with an __init__.py file. Packages allow developers to create reusable code that can be imported into other programs, avoiding code duplication. Using try-except The most straightforward approach is to use a try-except block. When Python attempts to import an uninstalled package, it raises an ImportError exception. Example Here's how to check if a package ...
Read MoreHow to check if Pandas column has value from list of string?
In pandas, we often need to check if values in a DataFrame column match any items from a given list of strings. This is useful for filtering data, data validation, or finding specific patterns in your dataset. Pandas is a powerful Python library for data analysis and manipulation. It provides several methods to check if column values exist in a list of strings, with isin() being the most commonly used approach. Creating Sample Data Let's first create a sample DataFrame to demonstrate different methods ? import pandas as pd data = { ...
Read More