# Import necessary libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Load the dataset
# Replace ‘your_dataset.csv’ with the path to your file
# Ensure that your dataset has continuous numeric features and target variable
data = pd.read_csv(‘your_dataset.csv’)
# efine the features (X) and target (y) variables
# Replace ‘target_column’ with the name of your target variable
X = data.drop(columns=[‘target_column’])
y = data[‘target_column’]
# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize the Linear Regression model
model = LinearRegression()
# Fill the model to the training data
model.fit(X_train, y_train)
# Predict on the test data
y_pred = model.predict(X_test)
# Calculate Mean Squared Error (MSE) and Root Mean Squared Error (RMSE)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
# Print the evaluation metrics
print(“Mean Squared Error (MSE):”, mse)
print(“Root Mean Squared Error (RMSE):”, rmse)
3.
# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report
# Load the dataset
# Replace ‘your_dataset.csv’ with the path to your dataset
data = pd.read_csv(‘your_dataset.csv’)
# Define the features (X) and target (y) variables
# Replace ‘target_column’ with the name of your target column
X = data.drop(columns=[‘target_column’])
y = data[‘target_column’]
# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize the Naive Bayes model
model = GaussianNB()
# Fit the model to the training data
model.fit(X_train, y_train)
# Predict on the test data
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average=’weighted’)
recall = recall_score(y_test, y_pred, average=’weighted’)
f1 = f1_score(y_test, y_pred, average=’weighted’)
# Print the evaluation metrics
print(“Accuracy:”, accuracy)
print(“Precision:”, precision)
print(“Recall:”, recall)
print(“F1-score:”, f1)
# Optional: Print a detailed classification report
print(“\nClassification Report:\n”, classification_report(y_test, y_pred))
5.
# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report
# Load the dataset
# Replace ‘your_dataset.csv’ with the path to your dataset
data = pd.read_csv(‘your_dataset.csv’)
# Define the features (X) and target (y) variables
# Replace ‘target_column’ with the name of your target column
X = data.drop(columns=[‘target_column’])
y = data[‘target_column’]
# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize the KNN model
# You can adjust the number of neighbors (k) as needed
k = 5
model = KNeighborsClassifier(n_neighbors=k)
# Fit the model to the training data
model.fit(X_train, y_train)
# Predict on the test data
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average=’weighted’)
recall = recall_score(y_test, y_pred, average=’weighted’)
f1 = f1_score(y_test, y_pred, average=’weighted’)
# Print the evaluation metrics
print(“Accuracy:”, accuracy)
print(“Precision:”, precision)
print(“Recall:”, recall)
print(“F1-score:”, f1)
# Optional: Print a detailed classification report
print(“\nClassification Report:\n”, classification_report(y_test, y_pred))
5.
# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report
# Load the dataset
# Replace ‘your_dataset.csv’ with the path to your dataset
data = pd.read_csv(‘your_dataset.csv’)
# Define the features (X) and target (y) variables
# Replace ‘target_column’ with the name of your target column
X = data.drop(columns=[‘target_column’])
y = data[‘target_column’]
# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize the SVM model
# You can specify different kernels (e.g., ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’)
model = SVC(kernel=’rbf’) # ‘rbf’ kernel is commonly used, but can be adjusted
# Fit the model to the training data
model.fit(X_train, y_train)
# Predict on the test data
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average=’weighted’)
recall = recall_score(y_test, y_pred, average=’weighted’)
f1 = f1_score(y_test, y_pred, average=’weighted’)
# Print the evaluation metrics
print(“Accuracy:”, accuracy)
print(“Precision:”, precision)
print(“Recall:”, recall)
print(“F1-score:”, f1)
# Optional: Print a detailed classification report
print(“\nClassification Report:\n”, classification_report(y_test, y_pred))
6.
# Import necessary libraries
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler
# Load the dataset
# Replace ‘your_dataset.csv’ with the path to your dataset
data = pd.read_csv(‘your_dataset.csv’)
# Optional: Preprocess the data (e.g., scaling features)
scaler = StandardScaler()
X = scaler.fit_transform(data)
# Initialize and fit the K-Means model
# Specify the number of clusters (k); this can be optimized.
k = 3
kmeans = KMeans(n_clusters=k, random_state=42)
# Fit the model to the data
kmeans.fit(X)
# Predict the clusters
cluster_labels = kmeans.labels_
# Evaluate the model using the silhouette score
silhouette_avg = silhouette_score(X, cluster_labels)
print(“Silhouette Score:”, silhouette_avg)
Please follow and like us: