som
som
Experiment 1:
Aim: Implement the concept of data visualization.
i. Scatter Plot
iii. Histogram
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)
keys=df.keys()
X=df.loc[:,keys[0:-1]]
y=df.loc[:,keys[-1]]
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()
scaler=StandardScaler()
X_train_scaled=scaler.fit_transform(X_train)
X_test_scaled = scaler.fit_transform(X_test)
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()
keys=df.keys()
keys
keys=df.keys()
keys
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
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=32,test_size=
.33)
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
plot_model_predictions('Linear',y_test,SVM_Regression_Prediction)
Experiment No. 4
AIM: Implement the concept of Polynomial Regression.
Implementation:
# 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()
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:
Implementation:
import numpy as np
import matplotlib:pyplot as plt
import pandas as pd
# import dataset
dataset = pd.read_csv(:Dataset)
print(dataset)
OUTPUT
Implementation:
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
OUTPUT:
[0.81217051]