1.
Implement Linear Regression
Aim: To implement linear regression using a CSV file containing one feature and
one label.
Code:
python
CopyEdit
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import [Link] as plt
# Load the data from CSV file
# Assuming the CSV contains 'Feature' and 'Label' columns
data = pd.read_csv('linear_data.csv')
# Display the first few rows of the data
print("Data Head:\n", [Link]())
# Defining X and y
X = data[['Feature']] # Feature column
y = data['Label'] # Label column
# Split 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)
# Create Linear Regression model
model = LinearRegression()
# Train the model on training data
[Link](X_train, y_train)
# Predict on test data
y_pred = [Link](X_test)
# Show predictions vs actual values
for actual, predicted in zip(y_test, y_pred):
print(f"Actual: {actual}, Predicted: {predicted}")
# Visualize the regression line
[Link](X_test, y_test, color='blue')
[Link](X_test, y_pred, color='red')
[Link]('Linear Regression: Test Data')
[Link]('Feature')
[Link]('Label')
[Link]()
# Print model score (accuracy)
score = [Link](X_test, y_test)
print(f"Model Accuracy: {score}")
Output:
2. Implement Logistic Regression
Aim: To implement logistic regression using a CSV file containing one feature and
one label.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import [Link] as plt
# Load the data from CSV file
# Assuming the CSV contains 'Feature' and 'Label' columns
data = pd.read_csv('logistic_data.csv')
# Display the first few rows of the data
print("Data Head:\n", [Link]())
# Defining X and y
X = data[['Feature']] # Feature column
y = data['Label'] # Label column (binary classification)
# Split 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)
# Create Logistic Regression model
log_model = LogisticRegression()
# Train the model on training data
log_model.fit(X_train, y_train)
# Predict on test data
y_pred = log_model.predict(X_test)
# Show predictions vs actual values
for actual, predicted in zip(y_test, y_pred):
print(f"Actual: {actual}, Predicted: {predicted}")
# Visualize the data points and decision boundary
[Link](X_test, y_test, color='blue')
[Link](X_test, y_pred, color='red', linestyle='None', marker='x')
[Link]('Logistic Regression: Test Data')
[Link]('Feature')
[Link]('Label')
[Link]()
# Print model score (accuracy)
score = log_model.score(X_test, y_test)
print(f"Model Accuracy: {score}")
Output:
3. Implement Naive Bayes Classifier
Aim: To implement the Naive Bayes classifier using a CSV file containing one
feature and one label.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score
import [Link] as plt
# Load the data from CSV file
data = pd.read_csv('naive_bayes_data.csv')
# Display the first few rows of the data
print("Data Head:\n", [Link]())
# Defining X and y
X = data[['Feature']] # Feature column
y = data['Label'] # Label column (binary or multiclass
classification)
# Split 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)
# Create Naive Bayes model
nb_model = GaussianNB()
# Train the model on training data
nb_model.fit(X_train, y_train)
# Predict on test data
y_pred = nb_model.predict(X_test)
# Show predictions vs actual values
for actual, predicted in zip(y_test, y_pred):
print(f"Actual: {actual}, Predicted: {predicted}")
# Visualize the data points and predictions
[Link](X_test, y_test, color='blue')
[Link](X_test, y_pred, color='red', marker='x')
[Link]('Naive Bayes: Test Data')
[Link]('Feature')
[Link]('Label')
[Link]()
# Print model accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy}")
Output:
4. Implement Decision Tree (ID3)
Aim: To implement a decision tree using the ID3 algorithm with a CSV file
containing one feature and one label.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeClassifier
from [Link] import accuracy_score
import [Link] as plt
# Load the data from CSV file
data = pd.read_csv('decision_tree_data.csv')
# Display the first few rows of the data
print("Data Head:\n", [Link]())
# Defining X and y
X = data[['Feature']] # Feature column
y = data['Label'] # Label column (binary or multiclass
classification)
# Split 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)
# Create Decision Tree model
dt_model = DecisionTreeClassifier()
# Train the model on training data
dt_model.fit(X_train, y_train)
# Predict on test data
y_pred = dt_model.predict(X_test)
# Show predictions vs actual values
for actual, predicted in zip(y_test, y_pred):
print(f"Actual: {actual}, Predicted: {predicted}")
# Visualize the decision tree (Optional)
[Link](X_test, y_test, color='blue')
[Link](X_test, y_pred, color='red', marker='x')
[Link]('Decision Tree: Test Data')
[Link]('Feature')
[Link]('Label')
[Link]()
# Print model accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy}")
Output:
5. Implement K-Nearest Neighbour (KNN)
Aim: To implement the K-Nearest Neighbour (KNN) algorithm using a CSV file
containing one feature and one label.
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score
import [Link] as plt
# Load the data from CSV file
data = pd.read_csv('knn_data.csv')
# Display the first few rows of the data
print("Data Head:\n", [Link]())
# Defining X and y
X = data[['Feature']] # Feature column
y = data['Label'] # Label column (binary or multiclass
classification)
# Split 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)
# Create KNN model
knn_model = KNeighborsClassifier(n_neighbors=3)
# Train the model on training data
knn_model.fit(X_train, y_train)
# Predict on test data
y_pred = knn_model.predict(X_test)
# Show predictions vs actual values
for actual, predicted in zip(y_test, y_pred):
print(f"Actual: {actual}, Predicted: {predicted}")
# Visualize the KNN results
[Link](X_test, y_test, color='blue')
[Link](X_test, y_pred, color='red', marker='x')
[Link]('KNN: Test Data')
[Link]('Feature')
[Link]('Label')
[Link]()
# Print model accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy}")
Output: