Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.8 kB
3
Indexable
Never
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split # Library with algos and functions needed for data and ai. 

# files = 1
# for n in range(files): # Iterate over each file
files = 15

for n in range(files):
    file = np.loadtxt(f'./Dane/dane{n+1}.txt')

    # Getting the X column
    X = file[:, [1]]


    # Getting the Y column
    y = file[:, [0]]

    # Getting the train and test data splits.
    # Test is to verify the level of training done to the module with X
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)

    c = np.hstack([X_train*X_train*X_train, X_train*X_train, X_train, np.ones(X_train.shape)])
    print(c)
    v = np.linalg.pinv(c) @ y_train
    
    print("Funkcja x^3")
    print("Train error")
    e_train = y_train - (v[0]*X_train*X_train*X_train + v[1]*X_train*X_train + v[2]*X_train + v[3])
    e_avg = e_train.T @ e_train / len(e_train)
    print(e_avg)

    print("Test error")
    e_test = y_test - (v[0]*X_test*X_test*X_test + v[1]*X_test*X_test + v[2]*X_test + v[3])
    e_avg = e_test.T @ e_test / len(e_test)
    print(e_avg)

    
    print('------------------------------------------')

    print("Prosta funkcja liniowa")
    # Prosta funkcja liniowa
    c2 = np.hstack([X_train, np.ones(X_train.shape)])
    v2 = np.linalg.pinv(c2) @ y_train
    
    print("Train error")
    e_train = y_train - (v2[0] * X_train + v2[1])
    e_avg = e_train.T @ e_train / len(e_train)
    print(e_avg)

    print("Test error")
    e_test = y_test - (v2[0] * X_test + v2[1])
    e_avg = e_test.T @ e_test / len(e_test)
    print(e_avg)

   

    plt.plot(X,v2[0]*X + v2[1])
    plt.plot(X,v[0]*X*X*X + v[1]*X*X + v[2]*X + v[3])
    # plt.plt()
    plt.plot(X, y, 'ro')
    plt.show()