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 551 of 2547
Python - Ways to convert array of strings to array of floats
When working with numerical data, you often need to convert arrays of string numbers to floating-point arrays for mathematical operations. NumPy provides several efficient methods to perform this conversion. Using astype() Method The astype() method is the most common way to convert data types in NumPy arrays − import numpy as np # Create array of string numbers string_array = np.array(["1.1", "1.5", "2.7", "8.9"]) print("Initial array:", string_array) # Convert to array of floats using astype float_array = string_array.astype(np.float64) print("Final array:", float_array) print("Data type:", float_array.dtype) Initial array: ['1.1' '1.5' '2.7' '8.9'] Final ...
Read MorePython - Using variable outside and inside the class and method
Python is an object-oriented programming language where variables can be defined at different scopes. Understanding variable scope is crucial for writing clean, maintainable code. Variables can be defined outside classes (global scope), inside classes (class scope), or inside methods (local scope). Variables Defined Outside the Class (Global Variables) Variables defined outside any class or function have global scope and can be accessed from anywhere in the program ? # Variable defined outside the class (global scope) outVar = 'outside_class' print("Global access:", outVar) # Class one class Ctest: print("Inside class:", outVar) ...
Read MorePython - Prefix sum list
A prefix sum list (also called cumulative sum) is a new list where each element represents the sum of all elements from the start up to that position in the original list. This is useful for range sum queries and sliding window problems. Using List Comprehension with sum() The most straightforward approach uses list comprehension with the sum() function and list slicing ? # using list comprehension + sum() + list slicing # initializing list test_list = [3, 4, 1, 7, 9, 1] # printing original list print("The original list : " + str(test_list)) ...
Read MorePython - Plotting Radar charts in excel sheet using XlsxWriter module
A radar chart is a graphical method of displaying multivariate data in the form of a two-dimensional chart where three or more quantitative variables are represented on axes starting from the same point. Python's XlsxWriter module allows you to create radar charts directly in Excel files. Installing XlsxWriter First, install the XlsxWriter module if you haven't already ? pip install XlsxWriter Creating a Radar Chart Here's how to create a radar chart with sample data comparing two batches ? import xlsxwriter # Create a workbook and add a worksheet workbook ...
Read MorePython - Plotting an Excel chart with pattern fills in column using XlsxWriter module
The XlsxWriter module allows you to create Excel charts with pattern fills in columns, making data visualization more distinctive and professional. Pattern fills help differentiate data series visually, especially when working with monochrome displays or printing. Understanding Pattern Fills Pattern fills apply textures or patterns to chart columns instead of solid colors. Common patterns include 'shingle', 'horizontal_brick', 'vertical_brick', and 'dots'. Each pattern can have foreground and background colors for customization. Example Let's create a column chart comparing different building materials with pattern fills − import xlsxwriter # Create workbook and worksheet workbook = ...
Read MorePython - Plotting an Excel chart with Gradient fills using XlsxWriter module
XlsxWriter is a Python library for creating Excel files with advanced formatting, including charts with gradient fills. This tutorial shows how to create a column chart with gradient-filled data series. Installing XlsxWriter First, install the required module ? pip install xlsxwriter Creating Excel Chart with Gradient Fills The following example creates a column chart with two data series, each having different gradient colors ? import xlsxwriter # Create a workbook and add a worksheet workbook = xlsxwriter.Workbook('chart_gradient1.xlsx') worksheet = workbook.add_worksheet() # Create a bold format for headers bold = ...
Read MorePython - Number of values greater than K in list
Counting the number of values greater than a specific threshold K in a list is a common programming task. Python provides several efficient approaches to solve this problem using different techniques. Using a For Loop The traditional approach uses a counter variable and iterates through each element − # Find number of elements > k using for loop numbers = [1, 7, 5, 6, 3, 8] k = 4 print("The list:", numbers) count = 0 for num in numbers: if num > k: ...
Read MorePython - Implementation of Polynomial Regression
Polynomial Regression is a form of linear regression in which the relationship between the independent variable x and dependent variable y is modeled as an nth degree polynomial. Polynomial regression fits a nonlinear relationship between the value of x and the corresponding conditional mean of y, denoted E(y |x). Unlike simple linear regression that creates a straight line, polynomial regression can capture curved relationships in data by using polynomial terms like x², x³, etc. Creating Sample Data First, let's create some sample data to demonstrate polynomial regression ? import numpy as np import matplotlib.pyplot as ...
Read MorePython - Image Classification using keras
Image classification is a fundamental computer vision task that categorizes images into predefined classes. Keras provides powerful tools to build convolutional neural networks (CNNs) for this purpose. There are two main approaches ? Training a small network from scratch Fine tuning the top layers of the model using VGG16 Setting Up the Environment First, we import the necessary libraries and define basic parameters ? # Importing all necessary libraries from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense from keras import backend ...
Read MorePython - How and where to apply Feature Scaling?
Feature scaling is a crucial data preprocessing step applied to independent variables or features. It normalizes data within a particular range, ensuring all features contribute equally to machine learning algorithms. Why Feature Scaling is Important Most datasets contain features with vastly different magnitudes, units, and ranges. For example, age (20-80) versus income (20, 000-100, 000). Machine learning algorithms that use Euclidean distance treat these differences literally ? import numpy as np from sklearn.preprocessing import StandardScaler # Example: Age vs Income (unscaled) data = np.array([[25, 50000], [30, 75000], [35, 100000]]) print("Original data:") print("Age | Income") for ...
Read More