Python: Overview of Essential Libraries in the Data Science Field
Data science is a versatile field that combines statistics, machine learning, and data visualization. Python has established itself as one of the leading programming languages in this area because it offers a variety of powerful libraries for data processing, modeling, and visualization.
Bernd Fischer
04. Nov 2024 · 3 min read
1. Pandas
Pandas is a central library for data manipulation and analysis. It allows you to organize data in tabular form (DataFrames) and to process it using various commands.
import pandas as pd
# DataFrame erstellen und anzeigen
data = {"Name": ["Anna", "Ben", "Carla"], "Alter": [25, 30, 28]}
df = pd.DataFrame(data)
print(df)
# Daten filtern und statistische Berechnungen durchführen
print(df[df["Alter"] > 26])
print(df.describe())
Pandas provides numerous functions for reading, filtering, grouping, and visualizing data, making it ideal for exploratory data analysis and data preparation.
2. NumPy
NumPy is the foundation for numerical computations in Python and offers a fast implementation of arrays and matrices. It is frequently used for mathematical operations and linear algebra necessary for data analysis.
import numpy as np
# Array erstellen und Grundoperationen durchführen
arr = np.array([1, 2, 3, 4])
print(arr * 2)
print(np.mean(arr))
NumPy is especially powerful for vector and matrix calculations that play a central role in machine learning algorithms or statistical models.
3. Matplotlib and Seaborn
Matplotlib is the standard library for creating charts and graphics in Python, while Seaborn builds on Matplotlib and offers user-friendly options for aesthetically pleasing visualizations.
import matplotlib.pyplot as plt
import seaborn as sns
# Lineares Diagramm mit Matplotlib
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title("Matplotlib Plot")
plt.show()
# Verteilung mit Seaborn
sns.histplot(y)
plt.title("Seaborn Histogramm")
plt.show()
Seaborn offers simplified methods for visualizing data distributions, correlations, and statistical relationships, which significantly facilitates data exploration.
4. Scikit-Learn
Scikit-Learn is the standard library for machine learning in Python. It provides a wide range of algorithms for classification, regression, clustering, and preprocessing. Scikit-Learn is particularly user-friendly and suitable for both beginners and experienced data scientists.
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Beispieldaten
X = np.array([[1], [2], [3], [4]])
y = np.array([2, 3, 4, 5])
# Modell erstellen und trainieren
model = LinearRegression()
model.fit(X, y)
predictions = model.predict(X)
# Modellbewertung
print("Mean Squared Error:", mean_squared_error(y, predictions))
Scikit-Learn simplifies the workflow from data splitting through model fitting to evaluation and is especially useful for experimental and production-ready models.
5. TensorFlow and PyTorch
TensorFlow and PyTorch are the two leading libraries for deep learning and neural networks. They offer functionalities for creating, customizing, and deploying complex neural networks and are often used for applications such as image processing, speech processing, and time series analyses.
TensorFlow
TensorFlow, developed by Google, provides a comprehensive API for neural network development.
import tensorflow as tf
# Einfaches neuronales Netzwerk in TensorFlow
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(1)
])
model.compile(optimizer="adam", loss="mean_squared_error")
PyTorch
PyTorch, developed by Facebook, offers a dynamic environment that is more flexible and intuitive, making it popular for research and prototyping.
import torch
import torch.nn as nn
# Einfaches neuronales Netzwerk in PyTorch
model = nn.Sequential(
nn.Linear(10, 64),
nn.ReLU(),
nn.Linear(64, 1)
)
Both libraries provide extensive tools for developing and training neural networks and are capable of running on GPUs to increase computational power.
6. Statsmodels
Statsmodels is a specialized library for statistical modeling and analysis, offering advanced functions for regression, time series analysis, and statistical tests. Statsmodels is particularly helpful for projects requiring statistical hypothesis testing.
import statsmodels.api as sm
# Lineare Regression mit Statsmodels
X = sm.add_constant(X) # Hinzufügen des Interzepts
model = sm.OLS(y, X)
results = model.fit()
print(results.summary())
Statsmodels provides detailed statistics and information about model fitting that are helpful when interpreting and analyzing models.
7. Plotly
Plotly is an interactive visualization library that enables the creation of charts and dashboards, well-suited for web applications and Jupyter notebooks.
import plotly.express as px
# Interaktives Balkendiagramm
fig = px.bar(x=["A", "B", "C"], y=[1, 3, 2], title="Interaktives Balkendiagramm")
fig.show()
Plotly is excellent for interactive charts that are intended to be embedded in dashboards and data visualizations.