Commit 0a63b133 authored by Zhangkai Wu's avatar Zhangkai Wu
Browse files

上传新文件

parent fb570cce
Loading
Loading
Loading
Loading
+120 −0
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
# You are encouraged to follow this template as a guide, but feel free to make reasonable modifications as needed.
# The key requirement is that your code runs successfully and produces the expected results.
```

%% Cell type:code id: tags:

``` python
# NOTE:
# You may choose to use ChatGPT (or any AI-based tool) to assist with your assignment,
# but you must ensure that you fully understand the entire code.
# You are solely responsible for the work you submit.
# Please keep in mind: ChatGPT will not be available during the exam.
```

%% Cell type:code id: tags:

``` python
import numpy as np
import pandas


def init_layers():
    # TODO: Implement this function
    return 0


def activation_function():
    # TODO: Implement this function
    return 0


def activation_derivative():
    # TODO: Implement this function
    return 0


def single_layer_forward_propagation():
    # TODO: Implement this function
    return 0


def full_forward_propagation():
    # TODO: Implement this function
    return 0


def get_loss_value():
    # TODO: Implement this function
    return 0

def single_layer_backward_propagation():
    # TODO: Implement this function
    return 0


def full_backward_propagation():
    # TODO: Implement this function
    return 0


def update():
    # TODO: Implement this function
    return 0


def train():
    # TODO: Implement this function
    return 0


def preprocess_dataset(dataset):
    filtered_dataset = dataset.loc[
        (dataset.iloc[:, 0] == "phytophthora-rot")
        | (dataset.iloc[:, 0] == "alternarialeaf-spot")
    ]

    filtered_dataset = filtered_dataset.replace("?", -1)

    filtered_dataset.iloc[:, 0] = filtered_dataset.iloc[:, 0].astype("category")
    cat_columns = filtered_dataset.select_dtypes(["category"]).columns
    filtered_dataset[cat_columns] = filtered_dataset[cat_columns].apply(
        lambda x: x.cat.codes
    )

    filtered_dataset = filtered_dataset.astype("float64")

    X = filtered_dataset.iloc[:, 1:]
    y = filtered_dataset.iloc[:, 0]

    return X, y


# an auxiliary function that converts probability into class
def convert_prob_into_class(probs):
    probs_ = np.copy(probs)
    probs_[probs_ > 0.5] = 1
    probs_[probs_ <= 0.5] = 0
    return probs_


def get_accuracy_value(Y_hat, Y):
    Y_hat_ = convert_prob_into_class(Y_hat)
    return (Y_hat_ == Y).all(axis=0).mean()


if __name__ == "__main__":
    # Main function of script
    train_data = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.data.csv", header=None)

    test_data = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None)

    X_train, y_train = preprocess_dataset(train_data)

    X_test, y_test = preprocess_dataset(test_data)

    # TODO: Implement learning and prediction
```