0% found this document useful (0 votes)
10 views

som

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

som

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

List of Experiment

Implement the concept of data visualization.

Implement the concept of support vector machine for classification

Implement the concept of support vector machine for Regression

Implement the concept of Polynomial Regression.

Implement the concept of Logistic Regression.

Show the concept of various Probability Distributions.

Implement the concept of Hypothesis Testing for a scenario.

Show the concept of Data Preprocessing using any dataset.

Implementation of various Activation Functions.

Implementation of simple neural network.

Experiment 1:
Aim: Implement the concept of data visualization.

 Data visualization is the discipline of trying to understand data by placing it in a


visual context so that patterns, trends and correlations that might not otherwise be
detected can be exposed.
 Python offers multiple great graphing libraries that come packed with lots of
different features:
Matplotlib: low level, provides lots of freedom
Pandas Visualization: easy to use interface, built on Matplotlib
Seaborn: high-level interface, great default styles
Plotly: can create interactive plots
Matplotlib
Matplotlib is the most popular python plotting library. It offers lots of freedom at the cost
of having to write more code. Matplotlib is specifically good for creating basic graphs like
line charts, bar charts, histograms and many more. It can be imported by typing:
import matplotlib.pyplot as plt

i. Scatter Plot

To create a scatter plot in Matplotlib we can use the scatter method.


import matplotlib.pyplot as plt
iris = pd.read_csv(‘\iris.data', names=['sepal_length', 'sepal_width', 'petal_length',
'petal_width', 'class'])
print(iris.head())
# create a figure and axis
fig, ax = plt.subplots()
# scatter the sepal_length against the sepal_width
ax.scatter(iris['sepal_length'], iris['sepal_width'])
# set a title and labels
ax.set_title('Iris Dataset')
ax.set_xlabel('sepal_length')
ax.set_ylabel('sepal_width')

ii. Line Chart

In Matplotlib we can create a line chart by calling the plot method.


# get columns to plot
columns = iris.columns.drop(['class'])
# create x data
x_data = range(0, iris.shape[0])
# create figure and axis
fig, ax = plt.subplots()
# plot each column
for column in columns:
ax.plot(x_data, iris[column], label=column)
# set title and legend
ax.set_title('Iris Dataset')
ax.legend()

iii. Histogram

In Matplotlib we can create a Histogram using the hist method.


# create figure and axis
fig, ax = plt.subplots()
# plot histogram
ax.hist(wine_reviews['points'])
# set title and labels
ax.set_title('Wine Review Scores')
ax.set_xlabel('Points')
ax.set_ylabel('Frequency')

iv. Bar Chart

A bar chart can be created using the bar method. The bar-chart isn’t automatically
calculating the frequency of a category so we are going to use pandas value_counts function
to do this.
# create a figure and axis
fig, ax = plt.subplots()
# count the occurrence of each class
data = wine_reviews['points'].value_counts()
# get x and y data
points = data.index
frequency = data.values
# create bar chart
ax.bar(points, frequency)
# set title and labels
ax.set_title('Wine Review Scores')
ax.set_xlabel('Points')
ax.set_ylabel('Frequency')

Experiment No. 2
AIM: Implement the concept of SVM classification
Implementation:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df=pd.read_csv('/content/drive/MyDrive/AIML_asi@connect_Day3/
Algerian_forest_fires_dataset_UPDATE.csv')
df
df.keys()
df=df.drop(['year'],axis=1)
df.keys()
df.columns=df.columns.str.strip() # remove white spaces from the column names
df.keys()
set(df.Classes)
df.Classes=df.Classes.str.strip()
set(df.Classes)
from sklearn import preprocessing
label_encoder=preprocessing.LabelEncoder()
df['Classes']=label_encoder.fit_transform(df['Classes'])
df.head()
set(df.Classes)

# X will be referred as feature vector


# y will be the label vector

keys=df.keys()
X=df.loc[:,keys[0:-1]]

# From here 'X' is the feature vector

y=df.loc[:,keys[-1]]

# From here 'y' is the label vector

X
y
X=np.array(X)
y=np.array(y) # we have converted data frames into arrays
X.shape
y.shape
X[0]
y[0]
percentage=df.Classes.value_counts(normalize=True)*100
percentage
classes_labels=['Fire', 'Not Fire']
plt.figure(figsize=(16,10))
plt.pie(percentage, labels=classes_labels,autopct="%1.1f%%")
plt.title("Pie chart of labels", fontsize=16)
plt.show()

# Training the classifier i.e. SVM

from sklearn.svm import SVC


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
svc=SVC(kernel='linear', degree=3)
svc.fit(X_train,y_train)
y_pred=svc.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_test,y_pred) # y_test is the actual label, y_pred is the label predicted by
the trained classifier here

accuracy_score(y_test,y_pred) # poly deg=3

from sklearn.preprocessing import StandardScaler

scaler=StandardScaler()

X_train_scaled=scaler.fit_transform(X_train)
X_test_scaled = scaler.fit_transform(X_test)

# X_train -> X_train_scaled

svc.fit(X_train_scaled,y_train)

y_pred=svc.predict(X_test_scaled)

accuracy_score(y_test,y_pred)

Experiment No. 3
AIM: Implement the concept of SVM Regression
Implementation:
import pandas as pd

df=pd.read_csv('/content/drive/MyDrive/AIML_asi@connect/
Algerian_forest_fires_dataset_UPDATE.csv')

df

df.keys()

df=df.drop(['year'],axis=1)

df

df.columns=df.columns.str.strip()
df.columns

set(df.Classes)

df.Classes=df.Classes.str.strip()
df.Classes.unique()

df[['day', 'month', 'Temperature', 'RH', 'Ws']]=df[['day', 'month',


'Temperature', 'RH', 'Ws']].astype(int)

keys=df.keys()

keys

keys=df.keys()

keys

X=df[['RH', 'Ws', 'Rain', 'FFMC', 'DMC', 'DC',


'ISI', 'BUI', 'FWI']]

X.keys()
y=df[['Temperature']]

y.keys()

import numpy as np
X=np.array(X)

y=np.array(y)

np.unique(y)

X.shape

y.shape

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split


from sklearn.preprocessing import StandardScaler

X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=32,test_size=
.33)

def Feature_Scaling(X_train, X_test):


scaler = StandardScaler()
X_train_after_Standardisation = scaler.fit_transform(X_train)
X_test_after_Standardisation = scaler.transform(X_test)
return X_train_after_Standardisation, X_test_after_Standardisation

from sklearn.svm import SVR

svr_rbf = SVR(kernel="rbf", C=100, gamma=0.1, epsilon=0.1)


svr_lin = SVR(kernel="linear", C=100, gamma="auto")
svr_poly = SVR(kernel="poly", C=100, gamma="auto", degree=3,
epsilon=0.1, coef0=1)
# svm_regressor_model = SVR(kernel='linear', degree=3, gamma='auto',
C=1.0, epsilon=0.2)
svr_lin.fit(X_train,y_train)

SVM_Regression_Prediction=svr_lin.predict(X_test)

def plot_model_predictions(model,y_confirmed_test,test_linear_pred):
'''
INPUT - model name, predicted and actual lables

OUTPUT - displays graphs


'''
plt.plot(y_confirmed_test)
plt.plot(test_linear_pred)
plt.legend(['Test Data', model+' Regression Predictions'])

plot_model_predictions('Linear',y_test,SVM_Regression_Prediction)

print("Training Score:",svr_lin.score(X_train, y_train))


print("Test Score:",svr_lin.score(X_test,y_test))

# score is coefficient of determination

from sklearn.metrics import mean_absolute_error


mean_absolute_error(y_test, SVM_Regression_Prediction)

from sklearn.metrics import mean_squared_error


mean_squared_error(y_test, SVM_Regression_Prediction)

Experiment No. 4
AIM: Implement the concept of Polynomial Regression.

Implementation:

# Importing the libraries:


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset


dataset = pd:read_csv(DataSet)
X = dataset.iloc[:, 0::1].values
y = dataset.iloc[::, 1].values

# Fitting Linear Regression to the dataset


from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression():
lin_reg.fit(X, y):

# Fitting Polynomial Regression to the dataset


from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree: = 2):
X_poly = poly_reg.fit_transform(X):
poly_reg.fit(X_poly, y)
lin_reg_2 = Regression()
lin_reg_2.fit(X_poly, y)

# Visualising the Polynomial Regression results


plt.scatter(X, y,: color = 'red')
plt.plot(X, lin_reg_2:.predict(poly_reg.fit_transform(X)),: color = 'blue')
plt.title('Truth or Bluff (Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

# Visualising the Polynomial Regression results (for higher resolution and smoother curve)
X_grid = np.arange(min(X), max(X), 0.1):
X_grid = X_grid.reshape((len(X_grid), 1)):
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, lin_reg_2.predict(poly_reg.fit_transform(X_grid)), color = 'blue')
plt.title('Truth or Bluff (Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

# Predicting a new result with Linear Regression


lin_reg.predict(6.5)
lin_reg.predict(np.reshape(6.5, (1,1)))

# Predicting a new result with Polynomial Regression


lin_reg_2.predict(poly_reg.fit_transform(6.5)):
lin_reg_2.predict(poly_reg.fit_transform(np.reshape(6.5, (1,1))))
Experiment No. 5
AIM: Implement the concept of Logistic Regression.

Implementation:
import numpy as np:
import matplotlib.pyplot as plt:
import pandas as pd:
dataset = pd.read_csv(Dataset)
X=dataset.iloc[::,[0,1]].values
y=dataset.iloc[::,2].values
#Training and Testing Data (divide the data)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test =train_test_split(X,y,test_size=0.0, random_state=0)
from sklearn.preprocessing import StandardScaler:
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.linear_model import LogisticRegression:
classifer = LogisticRegression(random_state=0)
classifer.fit(X_train,y_train)
y_pred= classifer:predict(X_test)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test,y_pred)
from sklearn:metrics import accuracy_score
accuracy_score(y_test,y_pred)
# Visualising the Training set results
from matplotlib.colors import ListedColormap:
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1,
step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step =
0.01))
plt.contourf(X1, X2, classifer:predict(np.array([X1.ravel(),
X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Classifier (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
# Visualising the Test set results
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1,
step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifer.predict(np.array([X1.ravel(),
X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Classifier (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
Experiment No. 6
AIM: Show the concept of various Probability Distributions.
Implementation:
# Binomial Distribution

import seaborn
import matplotlib:pyplot as plt
import numpy as np
from scipy:stats import binom
#Applying the binom class
pb = binom(n = 20, p = 0.6)
x = np.arange(1,21)
pmf = pb.pmf(x)
#Visualizing the distribution
sb.set_style('whitegrid')
plt.vlines(x ,0, pb.pmf(x): colors='k': linestyles='-': lw=5)
plt.ylabel('Probability')
plt.xlabel('Intervals')
plt.show()

#Poisson Distribution
import seaborn as sb
import matplotlib:pyplot as plt
import numpy as np
from scipy:stats import poisson
#Applying the poisson class methods
x = np.arange(0,10)
pmf = poisson.pmf(x,3)
#Visualizing the results
sb.set_style('whitegrid')
plt.vlines(x ,0, pmf, colors='k': linestyles='-': lw=6)
plt.ylabel('Probability')
plt.xlabel('intervals')
plt.show()

#Normal Distribution
from scipy:stats import norm
import numpy as np
import matplotlib:pyplot as plt
import seaborn as sb
# Creating the distribution
data = np:arange(1,10,0.01)
pdf = norm::pdf(data , loc = 5.3 , scale = 1 )
#Visualizing the distribution
sb.set_style('whitegrid')
sb.lineplot(data, pdf , color = 'black')
plt.ylabel('Probability Density')

Experiment No. 7
AIM: Implement the concept of Hypothesis Testing for a scenario.

Implementation:

sys_bp=[183, 152, 178, 157, 194, 163, 144, 114, 178, 152, 118, 158, 172, 138]:
mu=165
from scipy import stats
t_value,p_value=stats:ttest_1samp(sys_bp,mu)
one_tailed_p_value=float("{::.6f}".format(p_value/2))
print('p-value for one tailed test is: %f'%one_tailed_p_value)
alpha = 0.05
if one_tailed_p_value<=alpha:
print('Conclusion':'n':'Since p-value(=%f)'%p_value,'<','alpha(=%.2f)'%alpha,'''We
reject the null hypothesis H0.
So we conclude that there is no significant mean difference in systolic blood pressure.
i.e., μ = 165 at %.2f level of significance'''%alpha)
else:
print('Conclusion','n','Since p-value(=%f)'%one_tailed_p_value,'>','alpha(=
%.2f)'%alpha)

OUTPUT:

p-value for one tailed test is 0.117877


Conclusion n Since p-value(=0.117877) > alpha(=0.05)
Experiment No. 8
AIM: Show the concept of Data Preprocessing using any dataset.

Implementation:

import numpy as np
import matplotlib:pyplot as plt
import pandas as pd

# import dataset
dataset = pd.read_csv(:Dataset)
print(dataset)

# viewing the first few rows of the dataset


dataset:head()

# viewing statistical info about dataset


dataset:describe()

# checking for missing values


dataset:isnull()

X = dataset[['Country', 'Age', 'Salary']].values


y = dataset[['Purchased']] :values
print(X)
print(y)

OUTPUT

# viewing the first few rows of the dataset


Country Age Salary Purchased
0 France 44.0 72000.0 No
1 Spain 27.0 48000.0 Yes
2 Germany 30.0 54000.0 No
3 Spain 38.0 61000.0 No
4 Nigeria 18.0 15000.0 No
5 Germany 40.0 NaN Yes
6 France 35.0 58000.0 Yes
7 Spain NaN 52000.0 No
8 France 48.0 79000.0 Yes
9 Germany 50.0 83000.0 No
10 France 37.0 67000.0 Yes

# viewing the first few rows of the dataset

# viewing statistical info about dataset


# checking for missing values

# Splitting dataset into independent & dependent variable


Experiment No. 9
AIM: Implementation of various Activation Functions.

Implementation:

# Implementing the Sigmoid Activation Function


import numpy as np
import matplotlib:pyplot as plt
def sig(x):
return 1/(1 + np:exp(-x))
#with different input values
x = 1.0
print('Applying Sigmoid Activation on (%.1f) gives %:1f' % (x, sig(x)))
x = -10.0
print('Applying Sigmoid Activation on (%.1f) gives %:1f' % (x, sig(x)))
x = 0.0
print('Applying Sigmoid Activation on (%.1f) gives %:1f' % (x, sig(x))
#plot the Sigmoid activation functionx = np.linspace(-10, 10, 50)
x = np:linspace(-10, 10, 50)
p = sig(x)
plt:xlabel("x")
plt:ylabel("Sigmoid(x)")
plt:plot(x, p)
plt:show()
# Implementing the tanh activation function
def tanh(x):
return np.tanh(x) # We can use numpy's builtin tanh
#with different input values
x = 1.0
print('Applying tanh Activation on (%.1f) gives %:1f' % (x, tanh(x)))
x = -10.0
print('Applying tanh Activation on (%.1f) gives %:1f' % (x, tanh(x)))
x = 0.0
print('Applying tanh Activation on (%.1f) gives %:1f' % (x, tanh(x)))
generate_sample_data(start, end, step):
# Generates sample data using np:linspace
return np.linspace(start, end, step)

x = generate_sample_data(-5, 5, 10)
y = tanh(x)
# Now plot tanh activation function
plt.xlabel("x")
plt.ylabel("tanh(x)")
plt.plot(x, y)
plt.show()

OUTPUT:
Experiment No. 10

AIM: Implementation of simple neural network.


Implementation:
# importing dependancies
import numpy as np
def :sig(x):
return 1 / (1 + np:exp(-x))
# A 2 x 1 matrix of randomly generated weights in the range -1 to 1
weights = np:random:uniform(-1,1,size:(2, 1))
# The training set divided into input and output. Notice that we are trying to train our
neural network to predict the output of the logical OR.
training_inputs = np:array([[0, 0, 1, 1, 0, 1]]).reshape(3, 2)
training_outputs = np:array([[0, 1, 1]]).reshape(3,1)
for i in range(15000):
# forward pass
dot_product = np.dot(training_inputs, weights)
output = sig(dot.product)
# backward pass.
temp2 = -(training_outputs - output) * output * (1 - output)
adj = np.dot(training_inputs:transpose(), temp2)
# 0.5 is the learning rate.
weights = weights - 0:5 * adj
# The testing set
test_input = np.array([1, 0])
test_output = sig(np.dot(test_input, weights))
# OR of 1, 0 is 1
print(test_output)

OUTPUT:

[0.81217051]

You might also like