test.py 1.29 KB
Newer Older
Zheng Gaoshan's avatar
Zheng Gaoshan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression  # 确保正确导入
import torch

def test_numpy():
    print("Testing NumPy...")
    a = np.array([1, 2, 3])
    b = np.array([4, 5, 6])
    c = a + b
    print("NumPy array sum:", c)

def test_pandas():
    print("\nTesting Pandas...")
    data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
    df = pd.DataFrame(data)
    print("Pandas DataFrame:")
    print(df)

def test_matplotlib():
    print("\nTesting Matplotlib...")
    x = np.linspace(0, 10, 100)
    y = np.sin(x)
    plt.plot(x, y)
    plt.title("Sine Wave")
    plt.xlabel("x")
    plt.ylabel("sin(x)")
    plt.show()

def test_sklearn():
    print("\nTesting Scikit-learn (Linear Regression)...")
    X = np.array([[1], [2], [3]])  # 特征变量
    y = np.array([2, 4, 6])        # 目标变量
    model = LinearRegression()
    model.fit(X, y)
    prediction = model.predict()  # 提供输入特征 
    print("Prediction for x=4:", prediction[0])

def test_torch():
    print("\nTesting PyTorch...")
    x = torch.tensor([1.0, 2.0, 3.0])
    y = x * 2
    print("Torch tensor result:", y)

if __name__ == "__main__":
    test_numpy()
    test_pandas()
    test_matplotlib()
    # test_sklearn()
    test_torch()