Python Encyclopedia for Academics
  • Course Outline
  • Artificial Intelligence
    • Data Science Foundation
      • Python Programming
        • Introduction and Basics
          • Variables
          • Print Function
          • Input From User
          • Data Types
          • Type Conversion
        • Operators
          • Arithmetic Operators
          • Relational Operators
          • Bitwise Operators
          • Logical Operators
          • Assignment Operators
          • Compound Operators
          • Membership Operators
          • Identity Operators
      • Numpy
        • Vectors, Matrix
        • Operations on Matrix
        • Mean, Variance, and Standard Deviation
        • Reshaping Arrays
        • Transpose and Determinant of Matrix
      • Pandas
        • Series and DataFrames
        • Slicing, Rows, and Columns
        • Operations on DataFrames
        • Different wayes to creat DataFrame
        • Read, Write Operations with CSV files
      • Matplotlib
        • Graph Basics
        • Format Strings in Plots
        • Label Parameters, Legend
        • Bar Chart, Pie Chart, Histogram, and Scatter Plot
  • Machine Learning Algorithms
    • Regression Analysis In ML
      • Regression Analysis in Machine Learning
      • Proof of Linear Regression Formulas
      • Simple Linear Regression Implementation
      • Multiple Linear Regression
      • Advertising Dataset Example
      • Bike Sharing Dataset
      • Wine Quality Dataset
      • Auto MPG Dataset
    • Classification Algorithms in ML
      • Proof of Logistic Regression
      • Simplified Mathematical Proof of SVM
      • Iris Dataset
  • Machine Learning Laboratory
    • Lab 1: Titanic Dataset
      • Predicting Survival on the Titanic with Machine Learning
    • Lab 2: Dow Jones Index Dataset
      • Dow Jones Index Predictions Using Machine Learning
    • Lab 3: Diabetes Dataset
      • Numpy
      • Pandas
      • Matplotlib
      • Simple Linear Regression
      • Simple Non-linear Regression
      • Performance Matrix
      • Preprocessing
      • Naive Bayes Classification
      • K-Nearest Neighbors (KNN) Classification
      • Decision Tree & Random Forest
      • SVM Classifier
      • Logistic Regression
      • Artificial Neural Network
      • K means Clustering
    • Lab 4: MAGIC Gamma Telescope Dataset
      • Classification in ML-MAGIC Gamma Telescope Dataset
    • Lab 5: Seoul Bike Sharing Demand Dataset
      • Regression in ML-Seoul Bike Sharing Demand Dataset
    • Lab 6: Medical Cost Personal Datasets
      • Predict Insurance Costs with Linear Regression in Python
    • Lab 6: Predict The S&P 500 Index With Machine Learning And Python
      • Predict The S&P 500 Index With Machine Learning And Python
  • Artificial Neural Networks
    • Biological Inspiration vs. Artificial Neurons
    • Review linear algebra and calculus essentials for ANNs
    • Activation Function
  • Mathematics
    • Pre-Calculus
      • Factorials
      • Roots of Polynomials
      • Complex Numbers
      • Polar Coordinates
      • Graph of a Function
    • Calculus 1
      • Limit of a Function
      • Derivative of Function
      • Critical Points
      • Indefinite Integrals
  • Calculus 2
    • 3D Coordinates and Vectors
    • Vectors and Vector Operations
    • Lines and Planes in Space (3D)
    • Partial Derivatives
    • Optimization Problems (Maxima/Minima) in Multivariable Functions
    • Gradient Vectors
  • Engineering Mathematics
    • Laplace Transform
  • Electrical & electronics Eng
    • Resistor
      • Series Resistors
      • Parallel Resistors
    • Nodal Analysis
      • Example 1
      • Example 2
    • Transient State
      • RC Circuit Equations in the s-Domain
      • RL Circuit Equations in the s-Domain
      • LC Circuit Equations in the s-Domain
      • Series RLC Circuit with DC Source
  • Computer Networking
    • Fundamental
      • IPv4 Addressing
      • Network Diagnostics
  • Cybersecurity
    • Classical Ciphers
      • Caesar Cipher
      • Affine Cipher
      • Atbash Cipher
      • Vigenère Cipher
      • Gronsfeld Cipher
      • Alberti Cipher
      • Hill Cipher
Powered by GitBook
On this page
  • What is Type Conversion in Python?
  • Why It’s Important in Machine Learning?
  • 1. Implicit Type Conversion (Automatic by Python)
  • 2. Explicit Type Conversion (Manual by You)
  • Real-world Example in Machine Learning:
  • 3. Practical Examples of int(), float(), str(), bool()
  • Use Case in Machine Learning: pandas
  • Type Conversion in NumPy
  • Example Project: Cleaning Data for ML
  • Tip: Check Before Converting
  • Summary Cheat Sheet
  • ML Case You’ll Use Often
  • Keywords
  1. Artificial Intelligence
  2. Data Science Foundation
  3. Python Programming
  4. Introduction and Basics

Type Conversion

Nerd Cafe

What is Type Conversion in Python?

Type Conversion is the process of converting the data from one data type to another.

There are two types of type conversion:

  1. Implicit Type Conversion – Python automatically converts.

  2. Explicit Type Conversion – You manually convert.

Why It’s Important in Machine Learning?

In machine learning, you:

  • Read data from files (like CSVs) – everything might be a string.

  • Need to feed numbers (not strings) into models.

  • Often convert data types to fit into numpy arrays, pandas DataFrames, or scikit-learn models.

1. Implicit Type Conversion (Automatic by Python)

Python automatically converts smaller data types to larger data types during operations.

Example:

x = 5      # int
y = 2.0    # float

result = x + y
print(result)       # Output: 7.0
print(type(result)) # Output: <class 'float'>

Notes:

  • int + float = float

  • Useful in calculations – Python promotes to the higher precision type.

2. Explicit Type Conversion (Manual by You)

You manually convert using functions like:

Function
Converts To

int()

Integer

float()

Float

str()

String

bool()

Boolean

list()

List

Real-world Example in Machine Learning:

You read CSV data, and you get this:

age = "25"          # str from CSV
height = "1.75"     # str from CSV

But ML models need numeric types:

age = int(age)
height = float(height)

3. Practical Examples of int(), float(), str(), bool()

Convert String to Integer

s = "123"
num = int(s)
print(num, type(num))  # 123 <class 'int'>

But this will fail:

s = "123.45"
num = int(s)  # ValueError!

Solution:

s = "123.45"
num = int(float(s))  # First to float, then int
print(num)  # 123

Convert String to Float

s = "3.14"
f = float(s)
print(f, type(f))  # 3.14 <class 'float'>

Convert Number to String

n = 100
s = str(n)
print(s, type(s))  # '100' <class 'str'>

Convert Boolean

print(bool(1))     # True
print(bool(0))     # False
print(bool("hi"))  # True
print(bool(""))    # False

Note: Empty string or zero = False. Everything else = True.

Use Case in Machine Learning: pandas

import pandas as pd

df = pd.read_csv("data.csv")

# Suppose 'salary' is stored as string
df['salary'] = df['salary'].astype(float)

Important: Use .astype() to convert entire pandas column.

Type Conversion in NumPy

import numpy as np

arr = np.array(['1.2', '3.4', '5.6'])  # dtype = <U3 (Unicode string)
arr_float = arr.astype(float)
print(arr_float)   # [1.2 3.4 5.6]

Example Project: Cleaning Data for ML

data = [
    {'age': '25', 'height': '1.80', 'gender': 'male'},
    {'age': '30', 'height': '1.75', 'gender': 'female'}
]

# Cleaned data
clean_data = []

for row in data:
    clean_data.append({
        'age': int(row['age']),
        'height': float(row['height']),
        'gender': 1 if row['gender'] == 'male' else 0  # boolean encoding
    })

print(clean_data)
# [{'age': 25, 'height': 1.8, 'gender': 1}, {'age': 30, 'height': 1.75, 'gender': 0}]

Tip: Check Before Converting

Always check the type before and after conversion:

print(type(value))  # Before
value = float(value)
print(type(value))  # After

Summary Cheat Sheet

int("123")           # 123
float("3.14")        # 3.14
str(123)             # '123'
bool(0)              # False
bool("text")         # True

list("hello")        # ['h', 'e', 'l', 'l', 'o']
tuple([1, 2, 3])     # (1, 2, 3)

ML Case You’ll Use Often

Scenario
Conversion Required

Data from CSV as strings

int(), float()

Encode categorical features

str to int (LabelEncoder)

Convert labels to booleans

str → bool()

Pandas columns conversion

.astype(dtype)

NumPy array conversion

astype(dtype)

Keywords

type conversion, python casting, implicit conversion, explicit conversion, int(), float(), str(), bool(), list(), tuple(), data preprocessing, machine learning, pandas astype, numpy astype, data cleaning, csv parsing, type checking, value error, string to number, boolean conversion, nerd cafe

PreviousData TypesNextOperators

Last updated 2 months ago