Introduction to Essential Python Data Science Libraries with Example Code

This article introduces key Python libraries for data analysis, visualization, statistical modeling, and machine learning—including NumPy, Pandas, Matplotlib, Seaborn, SciPy, Statsmodels, Scikit-learn, BeautifulSoup, TensorFlow, and Plotly—each accompanied by concise code examples demonstrating their core functionality.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Introduction to Essential Python Data Science Libraries with Example Code

NumPy : A fundamental library for handling large multi‑dimensional arrays and matrices.

import numpy as np
# 创建一个2x2的数组
arr = np.array([[1, 2], [3, 4]])
print(arr)
# 计算数组的平均值
mean = np.mean(arr)
print(mean)

Pandas : Provides the DataFrame data structure for flexible data analysis.

import pandas as pd
# 创建一个简单的DataFrame
data = {'Name': ['Tom', 'Nick', 'John'], 'Age': [20, 21, 19]}
df = pd.DataFrame(data)
print(df)
# 对Age列进行排序
sorted_df = df.sort_values('Age')
print(sorted_df)

Matplotlib : A plotting library for creating static, animated, and interactive visualizations.

import matplotlib.pyplot as plt
# 创建一些数据点
x = [1, 2, 3, 4]
y = [10, 20, 30, 40]
# 绘制线图
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')
plt.show()

Seaborn : Built on Matplotlib, it offers higher‑level statistical graphics.

import seaborn as sns
import pandas as pd
# 加载内置数据集
tips = sns.load_dataset("tips")
# 绘制箱线图
sns.boxplot(x="day", y="total_bill", data=tips)
plt.show()

SciPy : Supplies a collection of scientific computing tools.

from scipy import stats
# 创建一些随机数据
data = np.random.randn(100)
# 计算数据的正态性检验
normality_test_result = stats.shapiro(data)
print(normality_test_result)

Statsmodels : Used for estimating and testing statistical models.

import statsmodels.api as sm
import pandas as pd
# 加载内置数据集
data = sm.datasets.get_rdataset('mtcars').data
# 构建线性回归模型
X = data[['wt']]
y = data['mpg']
model = sm.OLS(y, X).fit()
print(model.summary())

Scikit-learn : A versatile library for machine learning and data mining.

from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
# 加载波士顿房价数据集
boston = load_boston()
X = boston.data
y = boston.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练线性回归模型
model = LinearRegression()
model.fit(X_train, y_train)
# 预测测试集结果
predictions = model.predict(X_test)

BeautifulSoup : A library for web scraping and parsing HTML documents.

from bs4 import BeautifulSoup
import requests
# 获取网页内容
url = "https://www.example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 提取所有标题元素
headings = soup.find_all(['h1', 'h2', 'h3'])
for heading in headings:
    print(heading.text)

TensorFlow : An open‑source platform for deep learning and neural networks.

import tensorflow as tf
import numpy as np
# 创建一个简单的线性模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(units=1, input_shape=[1])
])
# 编译模型
model.compile(optimizer='sgd', loss='mean_squared_error')
# 准备训练数据
x_train = np.array([1, 2, 3, 4]).reshape(-1, 1)
y_train = np.array([2, 4, 6, 8])
# 训练模型
model.fit(x_train, y_train, epochs=500)
# 预测新数据
x_test = np.array([5]).reshape(-1, 1)
prediction = model.predict(x_test)
print(prediction)

Plotly : A library for creating interactive data visualizations.

import plotly.express as px
import pandas as pd
# 加载内置数据集
df = px.data.iris()
# 创建交互式散点图
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
fig.show()
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonlibrariesData Science
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.