Machine Learnin
Machine Learnin
Machine Learnin
LAB PRACTICAL
Code:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error,
r2_score
import matplotlib.pyplot as plt
X_multiple = data.drop(columns=['MEDV'])
y_multiple = data['MEDV']
# Evaluation metrics
mae_simple = mean_absolute_error(y_test_simple, y_pred_simple)
rmse_simple = np.sqrt(mean_squared_error(y_test_simple,
y_pred_simple))
r2_simple = r2_score(y_test_simple, y_pred_simple)
# Print the metrics for both models
print("Simple Linear Regression Metrics:")
print("MAE:", mae_simple)
print("RMSE:", rmse_simple)
print("R-squared:", r2_simple)
OUTPUT:
Graph:
Lab Assignment 2 : Multiple Regression:
Code:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error,
r2_score
import matplotlib.pyplot as plt
OUTPUT:
Graph:
Lab Assignment 3: Logistics Regression
Code:
#Data Pre-procesing Step
# importing libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
#importing datasets
data_set= pd.read_csv('user_data.csv')
#feature Scaling
from sklearn.preprocessing import StandardScaler
st_x= StandardScaler()
x_train= st_x.fit_transform(x_train)
x_test= st_x.transform(x_test)
for i, j in enumerate(nm.unique(y_set)):
mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],
c = ListedColormap(('purple', 'green'))(i), label = j)
Output:
Lab Assignment 4: Support Vector Machine
Code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
Output:
ASSIGNMENT: - 5
PROBLEM STATEMENT: -
SOURCE CODE: -
import numpy as np
import matplotlib.pyplot as mtp
import pandas as pd
#load the dataset
dataset = pd.read_csv('User_Data.csv')
#print(dataset)
#print(dataset.head())
x = dataset.iloc[:, [2,3]].values
y = dataset.iloc[:, 4].values
#print(x)
#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
Output:
ASSIGNMENT: - 6
PROBLEM STATEMENT: -
SOURCE CODE: -
import numpy as np
#import matplotlib.pyplot as mtp
import pandas as pd
from sklearn import datasets
from sklearn.model_selecƟon import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import plotly.express as px
import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv('iris.csv')
#print(df)
x = df.drop(['variety'],axis='columns')
y=df['variety']
x_train,x_test,y_train,y_test =
train_test_split(x,y,test_size=0.2,random_state=5)
kmn=KNeighborsClassifier(n_neighbors=7)
kmn.fit(x_train,y_train)
res = kmn.score(x_test,y_test)
print(res)
fig =
px.scaƩer_3d(df,x='sepal.length',y='sepal.width',z='petal.length',co
lor='variety')
fig.show()
Output :
ASSIGNMENT: - 7
PROBLEM STATEMENT:
Perform Decision Tree and K-means on the given dataset.
Decision Tree:
# importng libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
import plotly.express as px
import warnings
warnings.filterwarnings(‘ignore’)
#imporƟng datasets
data_set= pd.read_csv(‘User_Data.csv’)
#ExtracƟng Independent and dependent Variable
x= data_set.iloc[:, [2,3]].values
y= data_set.iloc[:, 4].values
#feature Scaling
from sklearn.preprocessing import StandardScaler
st_x= StandardScaler()
x_train= st_x.fit_transform(x_train)
x_test= st_x.transform(x_test)
#Fiƫng Decision Tree classifier to the training set
from sklearn.tree import DecisionTreeClassifier
classifier= DecisionTreeClassifier(criterion=’entropy’,
random_state=0)
classifier.fit(x_train, y_train)
Code:
# importing libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
# ImporƟng the dataset
dataset = pd.read_csv('Mall_Customers.csv')
x = dataset.iloc[:, [3, 4]].values
#print(x)
Output:
Lab Assignment 9 : MLP
Code:
import pandas as pd
import numpy as np
df=pd.read_csv("bank_note_data.csv")
#print(df.head())
#df.shape
#df.describe()
x=df.drop('Class', axis=1)
y=df['Class']
#print(x.head())
#print(y.head())
#print(x.shape)
Code:
# importing libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
#importing datasets
data_set= pd.read_csv("Position_Salaries.csv")
Output: