assignment-3
assignment-3
assignment-3
import numpy as np
import pandas as pd
#data = pd.read_csv('titanic.csv')
df=pd.read_csv("Z:\ML LAB\Titanic.csv");
print(df);
m=len(df['Sex']);
for i in range(0,m,1):
if(df['Sex'][i]=='male'):
df['Sex'][i]=1;
else:
df['Sex'][i]=0;
for i in range(0,m,1):
if(df['Embarked'][i]=='C'):
df['Embarked'][i]=1;
if(df['Embarked'][i]=='Q'):
df['Embarked'][i]=2;
if(df['Embarked'][i]=='S'):
df['Embarked'][i]=3;
df = df.drop('Name', axis=1)
df = df.drop('Cabin', axis=1)
df = df.drop('Ticket', axis=1)
df['Age'].fillna(df['Age'].mean(), inplace=True)
df['Embarked'].fillna(df['Embarked'].mode()[0], inplace=True)
df['Fare'].fillna(df['Fare'].mean(), inplace=True)
'''q1=df['Age'].quantile(0.25);
q2=df['Age'].quantile(0.75);
IQR=q2-q1;
lb=q1-1.5*IQR;
ub=q1+1.5*IQR;
df=df[(df['Age']>=lb) &(df['Age']<=ub)];
'''
#data['Sex'] = data['Sex'].map({'male': 0, 'female': 1})
y = df['Survived']
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
def sigmoid(z):
return 1 / (1 + np.exp(-z))
m = len(y)
h = sigmoid(X.dot(theta))
return cost
m = len(y)
cost_history = []
h = sigmoid(X.dot(theta))
print(epoch,cost);
cost_history.append(cost)
theta_initial = np.zeros(X_train_bias.shape[1])
alpha = 0.01
iteration = 1000
#print('final theta:',theta_final);
plt.figure(figsize=(5, 5))
plt.xlabel('Epochs')
plt.ylabel('Cost')
plt.grid(True)
plt.show()
predict= sigmoid(XTEST.dot(theta_final))
import numpy as np
import pandas as pd
file_path = 'C:\\Users\\abhis\\Downloads\\Iris.csv'
df = pd.read_csv(file_path)
y = df['Species'].values
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
class LogisticRegressionWithGD:
def __init__(self, learning_rate=0.01, epochs=1000):
self.learning_rate = learning_rate
self.epochs = epochs
self.cost_history = []
m = len(y)
one_hot_y = np.eye(theta.shape[1])[y]
return cost
m, n = X.shape
num_classes = len(np.unique(y))
self.cost_history.append(cost)
return theta
plt.xlabel('Epochs')
plt.ylabel('Cost')
plt.legend()
plt.show()
y_pred = model.predict(X_test)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
plt.figure(figsize=(8, 6))
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.show()
Z = model.predict(grid_points)
Z = Z.reshape(xx.shape)
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.show()