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 matplotlib.pyplot 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", data.head())
# 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
model.fit(X_train, y_train)
# Predict on test data
y_pred = 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 regression line
plt.scatter(X_test, y_test, color='blue')
plt.plot(X_test, y_pred, color='red')
plt.title('Linear Regression: Test Data')
plt.xlabel('Feature')
plt.ylabel('Label')
plt.show()
# Print model score (accuracy)
score = model.score(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 matplotlib.pyplot 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", data.head())
# 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
plt.scatter(X_test, y_test, color='blue')
plt.plot(X_test, y_pred, color='red', linestyle='None', marker='x')
plt.title('Logistic Regression: Test Data')
plt.xlabel('Feature')
plt.ylabel('Label')
plt.show()
# 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 sklearn.metrics import accuracy_score
import matplotlib.pyplot 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", data.head())
# 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
plt.scatter(X_test, y_test, color='blue')
plt.scatter(X_test, y_pred, color='red', marker='x')
plt.title('Naive Bayes: Test Data')
plt.xlabel('Feature')
plt.ylabel('Label')
plt.show()
# 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 sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot 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", data.head())
# 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)
plt.scatter(X_test, y_test, color='blue')
plt.scatter(X_test, y_pred, color='red', marker='x')
plt.title('Decision Tree: Test Data')
plt.xlabel('Feature')
plt.ylabel('Label')
plt.show()
# 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 sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot 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", data.head())
# 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
plt.scatter(X_test, y_test, color='blue')
plt.scatter(X_test, y_pred, color='red', marker='x')
plt.title('KNN: Test Data')
plt.xlabel('Feature')
plt.ylabel('Label')
plt.show()
# Print model accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy}")
Output: