[Python] Unable to get the raw decimal of my weights, auto rounding somewhere?

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm doing a single layer perceptron where I need to get my model to predict whether the user makes a T or an L using some buttons on a website. rn i'm trying to get the weights and bias so I can implement them into my websites pyscript code since I can't have the website fitting and predicting. However, whenever I try to print the weights they come back rounded to sci notation. I've tried doing repr, but that doesn't work for some reason. Could anyone show me where I'm going wrong?

    import numpy as np
    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn import datasets
    import matplotlib.pyplot as plt

    def activation_function(z):
    return np.where(z >= 0, 1, 0)

    #importing the dataset as a pandas dataframe
    df = pd.read_csv("TrainingData.csv")
    df.head()

    #split training data into X and Y
    X = df.iloc[:, 1:]
    print(X)

    y = df.iloc[:, 0]
    print(y)


    #Convert Y labels into integers
    y = y.map({'L': 0, 'T': 1})

    #Convert X and Y to numpy arrays for later processing
    X = X.to_numpy(dtype = np.float64)
    y = y.to_numpy(dtype = np.int32)

    print(X)
    print(y)

    print(X.dtype)
    print(y.dtype)

    #split training data and check shapes. Doing a 70/30 split due to the dataset not being very large (58 samples, 16 features)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42)

    print(X_train.shape)
    print(X_test.shape)
    print(y_train.shape)
    print(y_test.shape)

    #Fitting the model to retreive the weights and biases for the pyscript portion of the code
    weights = np.zeros(16)*1
    bias = 0

    learning_rate = 0.01

    for _ in range(1000):
    for idx, x_i in enumerate(X_train):

    linear_product = np.dot(x_i, weights) + bias

    y_pred = activation_function(linear_product)

    loss = y_pred - y_train[idx]

    weights -= learning_rate * loss * x_i

    bias -= learning_rate * loss

    repr((weights))
    repr((bias))


    I'm expecting to get very large decimals assuming my conversions and equations are accurate. I've tried asking friends, prof, and chatGPT and using the repr among other methods to get it to print the correct way.

    Continue reading...

Compartilhe esta Página