What are the different evaluation metrics used in machine learning?
In the first example, we are going to use accuracy as an evaluation metric in a classification problem in Python.
Here is the code:
In this code, we first split our data into training and testing sets using train_test_split . Then we train a Logistic Regression model on the training set. Next, we make predictions on the test set and calculate the accuracy of the model using the accuracy_score function. Finally, we print out the accuracy of the model. In the second example, we are going to use mean squared error (MSE) as an evaluation metric in a regression problem in Python. Here is the code:
In this code, we follow a similar process as before by splitting the data, training a Linear Regression model, making predictions, and then calculating the mean squared error using the mean_squared_error function. Finally, we print out the mean squared error of the model.
from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train the model model = LogisticRegression() model.fit(X_train, y_train) # Make predictions on the test set y_pred = model.predict(X_test) # Calculate the accuracy of the model accuracy = accuracy_score(y_test, y_pred) print(f"Accuracy: {accuracy}")
In this code, we first split our data into training and testing sets using train_test_split . Then we train a Logistic Regression model on the training set. Next, we make predictions on the test set and calculate the accuracy of the model using the accuracy_score function. Finally, we print out the accuracy of the model. In the second example, we are going to use mean squared error (MSE) as an evaluation metric in a regression problem in Python. Here is the code:
from sklearn.metrics import mean_squared_error from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train the model model = LinearRegression() model.fit(X_train, y_train) # Make predictions on the test set y_pred = model.predict(X_test) # Calculate the mean squared error of the model mse = mean_squared_error(y_test, y_pred) print(f"Mean Squared Error: {mse}")
In this code, we follow a similar process as before by splitting the data, training a Linear Regression model, making predictions, and then calculating the mean squared error using the mean_squared_error function. Finally, we print out the mean squared error of the model.
Comments
Post a Comment