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
Programming Articles
Page 414 of 2547
Finding the number of rows and columns in a given matrix using Numpy
NumPy provides several ways to find the dimensions of a matrix. The most common method is using the shape attribute, which returns a tuple containing the number of rows and columns. Creating a Matrix First, let's create a NumPy matrix to work with ? import numpy as np # Create a 2x3 matrix with random numbers matrix = np.random.rand(2, 3) print("Matrix:") print(matrix) Matrix: [[0.37454012 0.95071431 0.73199394] [0.59865848 0.15601864 0.15599452]] Finding Rows and Columns Using shape The shape attribute returns a tuple where the first element is the number ...
Read MoreHow to create an identity matrix using Numpy?
An identity matrix is a square matrix where diagonal elements are 1 and all other elements are 0. NumPy provides the identity() function to create identity matrices efficiently. Syntax numpy.identity(n, dtype=None) Parameters n: Size of the identity matrix (n x n) dtype: Data type of the matrix elements (optional, defaults to float) Creating a Basic Identity Matrix import numpy as np # Create a 3x3 identity matrix identity_matrix = np.identity(3) print(identity_matrix) [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] Specifying Data Type ...
Read MoreHow to plot ROC curve in Python?
The ROC (Receiver Operating Characteristic) curve is a graphical plot used to evaluate binary classification models. It shows the trade-off between true positive rate (sensitivity) and false positive rate (1-specificity) at various threshold settings. Python's sklearn.metrics module provides the plot_roc_curve() method to easily visualize ROC curves for classification models. Steps to Plot ROC Curve Generate a random binary classification dataset using make_classification() method Split the data into training and testing sets using train_test_split() method Train a classifier (like SVM) on the training data using fit() method Plot the ROC curve using plot_roc_curve() method Display the plot ...
Read MoreHow to find the first date of a given year using Python?
The datetime module in Python can be used to find the first day of a given year. This datetime module is widely used for manipulating dates and times in various formats and calculations. Common approaches to find the first day of a given year using Python are as follows ? Datetime Module − Widely used library for manipulating dates and times in various ways. Calendar Module ...
Read MoreWrite a Python program to remove a certain length substring from a given string
We need to write a Python program that removes a specific substring from a given string. Python provides several methods to accomplish this task efficiently. Algorithm Step 1: Define a string. Step 2: Use the replace() function to remove the substring from the given string. Step 3: Display the modified string. Using replace() Method The most straightforward approach is using the built-in replace() method to replace the unwanted substring with an empty string ? original_string = "C++ is a object oriented programming language" modified_string = original_string.replace("object oriented", "") print("Original:", original_string) print("Modified:", modified_string) ...
Read MorePrint dates of today, yesterday and tomorrow using Numpy
NumPy provides datetime functionality through the datetime64 data type, allowing you to easily work with dates. You can calculate today's, yesterday's, and tomorrow's dates using np.datetime64() and np.timedelta64() functions. Understanding DateTime64 The datetime64 function creates date objects, while timedelta64 represents time differences. The 'D' parameter specifies the unit as days − import numpy as np # Get today's date today = np.datetime64('today', 'D') print("Today's Date:", today) Today's Date: 2024-01-15 Calculating Yesterday and Tomorrow You can add or subtract timedelta64 objects to get past or future dates ? ...
Read MoreHow to make several plots on a single page using matplotlib in Python?
Matplotlib provides several methods to create multiple plots on a single page. You can use subplots() to create a grid of subplots or subplot() to add plots one by one. This is useful for comparing different datasets or showing related visualizations together. Method 1: Using subplots() with Multiple Axes The subplots() function creates a figure with multiple subplot areas in a grid layout − import matplotlib.pyplot as plt import numpy as np # Sample data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) y3 = np.tan(x) y4 = np.log(x + 1) ...
Read MoreScatter plot and Color mapping in Python
We can create scatter plots with color mapping using Matplotlib's scatter() method. This technique allows us to visualize an additional dimension of data through color variations, making patterns and relationships more apparent. Basic Scatter Plot with Color Mapping Here's how to create a scatter plot where each point has a different color based on its position in the dataset − import matplotlib.pyplot as plt import numpy as np # Generate random data points x = np.random.rand(100) y = np.random.rand(100) # Create scatter plot with color mapping colors = range(100) plt.scatter(x, y, c=colors, cmap='viridis') plt.colorbar() ...
Read MoreHow to reset index in Pandas dataframe?
In Pandas, the index serves as row labels for a DataFrame. Sometimes you need to reset the index back to the default integer sequence (0, 1, 2...) or convert a custom index into a regular column. The reset_index() method provides this functionality. Basic reset_index() Usage Let's start with a simple example showing how to reset a DataFrame's index ? import pandas as pd # Create DataFrame with default index data = {'Name': ["Allen", "Jack", "Mark", "Vishal"], 'Marks': [85, 92, 99, 87]} df = pd.DataFrame(data) print("Original DataFrame:") ...
Read MoreHow to plot two dotted lines and set marker using Matplotlib?
In this article, we will learn how to plot two dotted lines with custom markers using Matplotlib. Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Prerequisites First, we need to import the matplotlib.pyplot module ? import matplotlib.pyplot as plt Pyplot is a collection of command-style functions that make matplotlib work like MATLAB, providing an easy interface for plotting. Basic Example with Dotted Lines Let's start with a simple example plotting two dotted lines ? import matplotlib.pyplot as plt # Define coordinates for two ...
Read More