Create prediction function #28

Open
opened 2025-03-15 02:27:13 +00:00 by nuluh · 0 comments
nuluh commented 2025-03-15 02:27:13 +00:00 (Migrated from github.com)

Example code

import numpy as np
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

# Train separate models for different sensors
svm_model1 = SVC()
svm_model1.fit(x_train1, y_train)

svm_model2 = SVC()
svm_model2.fit(x_train2, y_train)

def predict_new_data(model, new_data):
    """
    Predict class for new input data.
    
    Args:
        model: Trained SVM model
        new_data: A NumPy array or list representing the new input data
    
    Returns:
        Predicted class label
    """
    new_data = np.array(new_data).reshape(1, -1)  # Reshape for single sample
    prediction = model.predict(new_data)
    return prediction[0]

# Example usage
new_sample1 = [0.5, 0.8, 1.2, -0.7]  # Replace with actual feature values
new_sample2 = [0.3, 0.6, -1.0, 0.5]

predicted_class1 = predict_new_data(svm_model1, new_sample1)
predicted_class2 = predict_new_data(svm_model2, new_sample2)

print(f"Prediction for new sample (Sensor 1): {predicted_class1}")
print(f"Prediction for new sample (Sensor 2): {predicted_class2}")
Example code ```py import numpy as np from sklearn.svm import SVC from sklearn.metrics import accuracy_score # Train separate models for different sensors svm_model1 = SVC() svm_model1.fit(x_train1, y_train) svm_model2 = SVC() svm_model2.fit(x_train2, y_train) def predict_new_data(model, new_data): """ Predict class for new input data. Args: model: Trained SVM model new_data: A NumPy array or list representing the new input data Returns: Predicted class label """ new_data = np.array(new_data).reshape(1, -1) # Reshape for single sample prediction = model.predict(new_data) return prediction[0] # Example usage new_sample1 = [0.5, 0.8, 1.2, -0.7] # Replace with actual feature values new_sample2 = [0.3, 0.6, -1.0, 0.5] predicted_class1 = predict_new_data(svm_model1, new_sample1) predicted_class2 = predict_new_data(svm_model2, new_sample2) print(f"Prediction for new sample (Sensor 1): {predicted_class1}") print(f"Prediction for new sample (Sensor 2): {predicted_class2}") ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nuluh/thesis#28