Sorry anyone can help me to classify this data? Can i use Least Squares Classifier in python?
Here’s screenshot :
Warm Regards and thanksfull,
Hilmi
Sorry anyone can help me to classify this data? Can i use Least Squares Classifier in python?
Here’s screenshot :
Warm Regards and thanksfull,
Hilmi
maybe something like this,
import torch
import torch.nn as nn
model = nn.Sequential(nn.Linear(in_features=5, out_features=1))
input_ = torch.randn(5, 5) # would use the Ciri matrix here
target = torch.tensor([1., 0., 1., 0., 1.]) # would use the Kelas series as target here
criterion = nn.MSELoss()
optim = torch.optim.SGD(model.parameters(), lr=0.01)
for _ in range(100):
optim.zero_grad()
prediction = model(input_)
loss = criterion(prediction, target.unsqueeze(dim=1))
print(loss, abs(torch.round(prediction)))
loss.backward()
optim.step()
model.eval()
input_test = torch.randn(4, 5)
target_test = torch.tensor([1., 0., 1., 0.])
with torch.no_grad():
prediction = model(input_test)
loss = criterion(prediction, target_test.unsqueeze(dim=1))
print(loss, abs(torch.round(prediction)))
we could see the mathematical equations using sympy,
import torch
import torch.nn as nn
from sympy import *
model = nn.Sequential(nn.Linear(2, 2, bias=False))
input_tensor = torch.randn(2, 2)
target_tensor = torch.tensor([[1., 0.], [0., 1.]])
weights = MatrixSymbol('weights', 2, 2)
input_ = MatrixSymbol('input', 2, 2)
target = MatrixSymbol('target', 2, 2)
loss = sum(Matrix(weights*input_.transpose() - target)**2) # with bias=False, and we sum the terms of our matrix, could also mean
to take the derivative, and substitute the tensors,
loss.diff(Matrix(weights)).as_immutable().subs({input_: Matrix(input_tensor), weights: Matrix(model[0].weight.data), target: Matrix(target_tensor)}).doit()
this would be the gradient, which would be used by the optimizer, for the gradient descent step
The nice thing about least squares is that you can solve the equation directly instead of using an iterative method:
import numpy as np
input_ = np.random.randn(5, 5) # would use the Ciri matrix here
target = np.array([1., 0., 1., 0., 1.]) # would use the Kelas series as target here
beta = np.linalg.inv(X.T @ X) @ X.T @ y
input_test = np.random.randn(4, 5)
prediction = input_test @ beta
If you prefer to use a framework for this, you can use scikit-learn:
import numpy as np
from sklearn.linear_model import LinearRegression
n_samples = 10
n_dim = 5
X = np.random.randn(n_samples, n_dim)
y = np.random.choice([-1, 1], size=n_samples)
model = LinearRegression().fit(X, y)
params = model.coef_
input_test = np.random.randn(4, 5)
prediction = model.predict(input_test)
here maybe the cross entropy loss would be a better option,
criterion = nn.CrossEntropyLoss()
criterion(model(input_), target)
the target here needs to have class indices