In this lesson, we’ll cover essential Python libraries for machine learning: NumPy, Pandas, Matplotlib, and Scikit-Learn.

NumPy

NumPy is a library for numerical computations in Python. It provides support for arrays, matrices, and many mathematical functions.

Installation:

pip install numpy

Basic Operations:

import numpy as np

# Create an array
arr = np.array([1, 2, 3, 4, 5])
print(arr)

# Basic operations
print(arr + 5)  # Add 5 to each element
print(arr * 2)  # Multiply each element by 2

# Array slicing
print(arr[1:4])

# Multidimensional arrays
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)
print(matrix[1, 2])  # Access element at row 1, column 2

Pandas

Pandas is a powerful library for data manipulation and analysis.

Installation:

pip install pandas

Basic Operations:

import pandas as pd

# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df)

# Access columns
print(df['Name'])

# Access rows
print(df.iloc[0])  # First row

# Filtering
print(df[df['Age'] > 25])

Matplotlib

Matplotlib is a plotting library for creating static, animated, and interactive visualizations.

Installation:

pip install matplotlib

Basic Operations:

import matplotlib.pyplot as plt

# Basic plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title('Basic Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

# Bar plot
plt.bar(['A', 'B', 'C'], [10, 20, 15])
plt.title('Bar Plot')
plt.show()

Scikit-Learn

Scikit-Learn is a machine learning library in Python.

Installation:

pip install scikit-learn

Basic Operations:

from sklearn.linear_model import LinearRegression
import numpy as np

# Create dataset
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([1, 4, 9, 16, 25])

# Create and train model
model = LinearRegression()
model.fit(X, y)

# Make predictions
predictions = model.predict(np.array([[6], [7]]))
print(predictions)


Discover more from AI HintsToday

Subscribe to get the latest posts sent to your email.

Table of Contents

    Trending

    Discover more from AI HintsToday

    Subscribe now to keep reading and get access to the full archive.

    Continue reading