Introduction to Python Programming for AI & ML¶
Python is a powerful programming language widely used in the fields of Artificial Intelligence (AI) and Machine Learning (ML). This notebook serves as an introduction to Python, focusing on aspects that are particularly relevant for AI and ML applications.
Basics of Python¶
Printing and Variables¶
Python allows us to print text and store values in variables. Here's an example:
message = "Welcome to AI & ML with Python!"
print(message)
Data Types¶
Understanding basic data types such as integers, floats, strings, and booleans is essential.
integer_number = 10
floating_number = 5.6
text = "Python for AI & ML"
flag = True
print(type(integer_number), type(floating_number), type(text), type(flag))
my_list = [1, 2, 3, 4]
my_list.append(5)
print(my_list)
Dictionaries¶
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict['b'])
x = 10
if x > 10:
print("x is greater than 10")
elif x == 10:
print("x is equal to 10")
else:
print("x is less than 10")
for i in range(5):
print(i)
While Loops¶
count = 0
while count < 5:
print(count)
count += 1
List Comprehensions¶
List comprehensions provide a concise way to create lists.
squares = [x**2 for x in range(10)]
print(squares)
Lambda Functions¶
Lambda functions are small anonymous functions defined with the lambda
keyword.
multiply = lambda x, y: x * y
print(multiply(3, 4))
They are particularly useful when used with functions like map
, filter
, and reduce
.
numbers = [1, 2, 3, 4]
squares = map(lambda x: x**2, numbers)
print(list(squares))
import re
pattern = re.compile(r'\d+') # Matches one or more digits
result = pattern.match("12345 is a number")
print(result.group())
Searching for a Pattern¶
result = pattern.search("The number 12345 is here")
print(result.group())
Replacing a Pattern¶
new_string = re.sub(r'\d+', 'NUMBER', "12345 is a number")
print(new_string)
Splitting a String Based on a Pattern¶
split_string = re.split(r'\s+', "Split on spaces")
print(split_string)
Regular expressions are powerful but can be complex. It's good to practice with various examples to get comfortable with them.
Conclusion¶
With the understanding of control structures, lambda functions, and regular expressions, you now have more tools to write effective and efficient Python code for AI and ML applications. These concepts form the building blocks of many algorithms and data processing tasks in AI and ML.
Conditional Statements¶
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
Loops¶
for i in range(5):
print(i)
Functions¶
Creating functions is essential for writing modular and maintainable code.
def add_numbers(a, b):
return a + b
print(add_numbers(3, 4))
import numpy as np
array = np.array([1, 2, 3])
print(array)
Pandas¶
Pandas is used for data manipulation and analysis.
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 22]}
df = pd.DataFrame(data)
print(df)
Scikit-learn¶
Scikit-learn provides tools for data mining and data analysis.
from sklearn.datasets import load_iris
iris = load_iris()
print(iris.data[0:5])
TensorFlow and Keras¶
TensorFlow and Keras are popular for deep learning.
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)