Btech1007022 Lab5.1
Btech1007022 Lab5.1
Roll - BTECH/10070/22
LAB-5
Program1:
import csv
import numpy as np
data = []
plt.scatter(X, Y, color='blue')
plt.xlabel('Experience (years)')
plt.ylabel('Salary')
plt.show()
fl
fi
fl
fi
# Initialize parameters
m = 0 # Slope
b = 0 # Intercept
learning_rate = 0.01
iterations = 1000
n = len(X)
total_error = 0
for i in range(len(X)):
return total_error / n
# Gradient Descent
errors = []
for _ in range(iterations):
m_grad = 0
b_grad = 0
for i in range(len(X)):
m -= (m_grad / n) * learning_rate
b -= (b_grad / n) * learning_rate
mse = compute_mse(X, Y, m, b)
errors.append(mse)
print(f"Final Parameters: m = {m}, b = {b}")
plt.xlabel('Iteration')
plt.show()
plt.scatter(X, Y, color='blue')
plt.xlabel('Experience (years)')
plt.ylabel('Salary')
plt.show()
OUTPUT:
Program2:
import pandas as pd
data = pd.read_csv('insurance.csv')
print(data.head(10))
features = data.columns[:-1]
label = data.columns[-1]
data = data.dropna()
features = data.columns[:-1]
scaler = MinMaxScaler()
X = data[features]
y = data[label]
model = LinearRegression()
y_pred = model.predict(X_test)
OUTPUT:
fi
fi
Program3:
import pandas as pd
import numpy as np
data = pd.read_csv('iris.csv')
print(data.head(10))
target_variable = 'Species'
label_encoder = LabelEncoder()
X = data.iloc[:, 1:-1] # Exclude the 'Id' column and the target variable column
y = data.iloc[:, -1]
# Split the data into training and testing sets (80% training, 20% testing)
class LogisticRegression:
self.learning_rate = learning_rate
self.iterations = iterations
return 1 / (1 + np.exp(-z))
self.weights = np.zeros(self.n)
self.bias = 0
self.errors = []
for _ in range(self.iterations):
y_pred = self.sigmoid(linear_model)
db = (1 / self.m) * np.sum(y_pred - y)
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
self.errors.append(loss)
fi
def predict(self, X):
y_pred = self.sigmoid(linear_model)
y_pred = log_reg.predict(X_test)
print("Accuracy:", accuracy)
OUTPUT:
fi