Python Packages

basic
Published

July 24, 2024

NumPy: The Foundation of Numerical Computing

NumPy (Numerical Python) is used for scientific computing in Python. It provides powerful N-dimensional array objects and tools for working with these arrays. This makes it faster and more efficient than using standard Python lists for numerical operations.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr + 2)  # Add 2 to each element
print(arr * 2)  # Multiply each element by 2
print(np.mean(arr))  # Calculate the mean
print(np.std(arr)) # Calculate the standard deviation

arr2d = np.array([[1, 2], [3, 4]])
print(arr2d.shape) #Get the shape of the array
print(arr2d.transpose()) #transpose the array

Pandas: Data Wrangling Made Easy

Pandas is a library for data manipulation and analysis. It introduces the DataFrame object, a powerful structure for representing tabular data, similar to a spreadsheet or SQL table.

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 28],
        'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)

print(df)

print(df['Name'])

print(df[df['Age'] > 28])

print(df.groupby('City')['Age'].mean())

Matplotlib: Visualizing Your Data

Matplotlib is the go-to library for creating static, interactive, and animated visualizations in Python. It offers a wide range of plot types to effectively represent your data.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 3, 5]
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Line Plot")
plt.show()

plt.scatter(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Scatter Plot")
plt.show()

Scikit-learn: Machine Learning for Everyone

Scikit-learn provides a set of tools for various machine learning tasks, including classification, regression, clustering, dimensionality reduction, and model selection.

from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1], [2], [3]])
y = np.array([2, 4, 5])

model = LinearRegression()
model.fit(X, y)

print(model.predict([[4]]))

Requests: Simplifying HTTP Requests

The requests library makes interacting with web APIs incredibly easy. It handles the complexities of making HTTP requests, allowing you to focus on retrieving and processing data.

import requests

response = requests.get("https://www.example.com")

print(response.status_code)

print(response.text)

These are just a few of the many powerful Python packages available. Exploring and mastering these tools will improve your Python programming capabilities and open up a world of possibilities.