Lesson 2: Python for Machine Learning

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Entries:-

  • Data Engineering Job Interview Questions :- Datawarehouse Terms
  • Oracle Query Execution phases- How query flows?
  • Pyspark -Introduction, Components, Compared With Hadoop
  • PySpark Architecture- (Driver- Executor) , Web Interface
  • Memory Management through Hadoop Traditional map reduce vs Pyspark- explained with example of Complex data pipeline used for Both used
  • Example Spark submit command used in very complex etl Jobs
  • Deploying a PySpark job- Explain Various Methods and Processes Involved
  • What is Hive?
  • In How many ways pyspark script can be executed? Detailed explanation
  • DAG Scheduler in Spark: Detailed Explanation, How it is involved at architecture Level
  • CPU Cores, executors, executor memory in pyspark- Expalin Memory Management in Pyspark
  • Pyspark- Jobs , Stages and Tasks explained
  • A DAG Stage in Pyspark is divided into tasks based on the partitions of the data. How these partitions are decided?
  • Apache Spark- Partitioning and Shuffling
  • Discuss Spark Data Types, Spark Schemas- How Sparks infers Schema?
  • String Data Manipulation and Data Cleaning in Pyspark

Discover more from AI HintsToday

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

Continue reading