读书报告

发布时间 2023-12-17 22:17:48作者: 过过过过过过

《Python数据科学手册》中介绍了四个重要的数据科学库:NumPy、Pandas、Matplotlib和Scipy。

  1. NumPy

NumPy是Python中用于科学计算的基础包,提供了多维数组对象、各种派生对象(如掩码数组和矩阵)以及用于数组计算的各种例程。NumPy的核心功能是ndarray(N-dimensional array object),它是一个具有许多属性和方法的快速且灵活的容器。

常见函数:

  • np.zeros:创建指定长度或形状的全零数组;
  • np.ones:创建指定长度或形状的全一数组;
  • np.arange:创建等差数列数组;
  • np.random.rand:创建指定形状的随机数数组。
  1. Pandas

Pandas提供了两个主要的数据结构,Series和DataFrame。Series是一种一维的数组型对象,它包含了一个值序列(与NumPy中的ndarray类似)和一个与之相关的数据标签,称为索引。DataFrame是一个二维的表格型数据结构,它包含了一组有序的列,每列可以是不同的类型(数值、字符串、布尔值等)。

常见函数:

  • pd.read_csv:读取csv文件;
  • pd.concat:将两个DataFrame对象按照指定轴进行拼接;
  • df.groupby:按照指定条件进行分组。
  1. Matplotlib

Matplotlib是Python中最著名的绘图库之一,它提供了一套完整的绘图工具,可以在各种环境下生成出版物质量的图形。

常见函数:

  • plt.plot:绘制线条图;
  • plt.scatter:绘制散点图;
  • plt.hist:绘制直方图;
  • plt.bar:绘制条形图。
  1. Scipy

Scipy是Python的科学计算库,它基于NumPy扩展了许多操作。它包含了许多模块,用于优化、线性代数、积分、插值、特殊函数、快速傅里叶变换等等。

常见函数:

  • scipy.optimize.curve_fit:拟合函数;
  • scipy.linalg.solve:求解线性方程组;
  • scipy.integrate.quad:求解定积分。

例子:

使用Pandas和Matplotlib分析红酒数据集,找出哪些因素会影响红酒的品质评分。

首先读取数据集:

 
import pandas as pd
import matplotlib.pyplot as plt

# 读取数据集
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv'
df = pd.read_csv(url, sep=';')

然后绘制品质评分与酒精含量之间的关系:

 
plt.scatter(df['alcohol'], df['quality'])
plt.xlabel('Alcohol')
plt.ylabel('Quality')
plt.show()

最后,使用Pandas的corr函数计算各个因素之间的相关性:

 
corr = df.corr()
print(corr['quality'].sort_values(ascending=False))

输出结果:

 
quality                 1.000000
alcohol                 0.476166
sulphates               0.251397
citric acid             0.226373
volatile acidity       -0.390558
total sulfur dioxide   -0.470029
density                -0.494588
chlorides              -0.128907
free sulfur dioxide    -0.050656
pH                     -0.057731
fixed acidity          -0.076743
residual sugar         -0.097577
Name: quality, dtype: float64

可以看出,酒精含量、硫酸盐和柠檬酸都对红酒的品质评分有影响。

 

Matplotlib是一个强大的绘图库,可以用于图像处理中的可视化和图像操作。以下是一些常见的使用Matplotlib进行图像处理的示例:

  1. 显示图像:
 
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# 读取图像
img = mpimg.imread('image.jpg')

# 显示图像
plt.imshow(img)
plt.axis('off')  # 关闭坐标轴
plt.show()
  1. 调整亮度和对比度:
 
import numpy as np

# 调整亮度和对比度的函数
def adjust_brightness_contrast(img, brightness, contrast):
    adjusted_img = img * (contrast / 255.0) + brightness
    return np.clip(adjusted_img, 0, 1)

# 调整亮度和对比度
adjusted_img = adjust_brightness_contrast(img, 50, 1.5)

# 显示调整后的图像
plt.imshow(adjusted_img)
plt.axis('off')
plt.show()
  1. 图像旋转和翻转:
 
# 图像旋转函数
def rotate_image(img, angle):
    rotated_img = np.rot90(img, angle)
    return rotated_img

# 图像翻转函数
def flip_image(img, flip_direction):
    flipped_img = np.flip(img, flip_direction)
    return flipped_img

# 旋转图像
rotated_img = rotate_image(img, 90)

# 翻转图像
flipped_img = flip_image(img, 1)

# 显示旋转和翻转后的图像
plt.subplot(121)
plt.imshow(rotated_img)
plt.axis('off')
plt.subplot(122)
plt.imshow(flipped_img)
plt.axis('off')
plt.show()
  1. 图像缩放和裁剪:
 
import cv2

# 图像缩放函数
def resize_image(img, scale_percent):
    width = int(img.shape[1] * scale_percent / 100)
    height = int(img.shape[0] * scale_percent / 100)
    dim = (width, height)
    resized_img = cv2.resize(img, dim, interpolation=cv2.INTER_LINEAR)
    return resized_img

# 图像裁剪函数
def crop_image(img, x, y, width, height):
    cropped_img = img[y:y+height, x:x+width]
    return cropped_img

# 缩放图像
resized_img = resize_image(img, 50)

# 裁剪图像
cropped_img = crop_image(img, 100, 100, 200, 200)

# 显示缩放和裁剪后的图像
plt.subplot(121)
plt.imshow(resized_img)
plt.axis('off')
plt.subplot(122)
plt.imshow(cropped_img)
plt.axis('off')
plt.show()