0% found this document useful (0 votes)
11 views

Modelling and Simulation Sample Model 5

The document loads the Iris dataset, splits it into training and test sets, trains a support vector machine (SVM) classifier on the training set using a linear kernel, predicts the classes of the test set, and calculates the accuracy of the predictions compared to the true test labels.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Modelling and Simulation Sample Model 5

The document loads the Iris dataset, splits it into training and test sets, trains a support vector machine (SVM) classifier on the training set using a linear kernel, predicts the classes of the test set, and calculates the accuracy of the predictions compared to the true test labels.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

import numpy as np

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score

# Load a sample dataset (Iris dataset)

iris = datasets.load_iris()

X = iris.data

y = iris.target

# Split the dataset 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)

# Initialize the SVM model

svm_model = SVC(kernel='linear') # You can use other kernels like 'rbf', 'poly', etc.

# Train the model on the training data

svm_model.fit(X_train, y_train)

# Make predictions on the test data

y_pred = svm_model.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")import numpy as np

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC


from sklearn.metrics import accuracy_score

# Load a sample dataset (Iris dataset)

iris = datasets.load_iris()

X = iris.data

y = iris.target

# Split the dataset 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)

# Initialize the SVM model

svm_model = SVC(kernel='linear') # You can use other kernels like 'rbf', 'poly', etc.

# Train the model on the training data

svm_model.fit(X_train, y_train)

# Make predictions on the test data

y_pred = svm_model.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")import numpy as np

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score

# Load a sample dataset (Iris dataset)

iris = datasets.load_iris()
X = iris.data

y = iris.target

# Split the dataset 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)

# Initialize the SVM model

svm_model = SVC(kernel='linear') # You can use other kernels like 'rbf', 'poly', etc.

# Train the model on the training data

svm_model.fit(X_train, y_train)

# Make predictions on the test data

y_pred = svm_model.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")import numpy as np

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score

# Load a sample dataset (Iris dataset)

iris = datasets.load_iris()

X = iris.data

y = iris.target

# Split the dataset 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)

# Initialize the SVM model

svm_model = SVC(kernel='linear') # You can use other kernels like 'rbf', 'poly', etc.

# Train the model on the training data

svm_model.fit(X_train, y_train)

# Make predictions on the test data

y_pred = svm_model.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")import numpy as np

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score

# Load a sample dataset (Iris dataset)

iris = datasets.load_iris()

X = iris.data

y = iris.target

# Split the dataset 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)

# Initialize the SVM model

svm_model = SVC(kernel='linear') # You can use other kernels like 'rbf', 'poly', etc.
# Train the model on the training data

svm_model.fit(X_train, y_train)

# Make predictions on the test data

y_pred = svm_model.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")import numpy as np

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score

# Load a sample dataset (Iris dataset)

iris = datasets.load_iris()

X = iris.data

y = iris.target

# Split the dataset 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)

# Initialize the SVM model

svm_model = SVC(kernel='linear') # You can use other kernels like 'rbf', 'poly', etc.

# Train the model on the training data

svm_model.fit(X_train, y_train)
# Make predictions on the test data

y_pred = svm_model.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")import numpy as np

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score

# Load a sample dataset (Iris dataset)

iris = datasets.load_iris()

X = iris.data

y = iris.target

# Split the dataset 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)

# Initialize the SVM model

svm_model = SVC(kernel='linear') # You can use other kernels like 'rbf', 'poly', etc.

# Train the model on the training data

svm_model.fit(X_train, y_train)

# Make predictions on the test data

y_pred = svm_model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")

You might also like