MlLabManualdocx 2024 09 04 22 02 58
MlLabManualdocx 2024 09 04 22 02 58
LAB MANUAL
Machine Learning
BTCS619-18
Course Code: BTCS619-18
List of Experiment:
import cv2
import numpy as np
import tensorflow as tf
model = tf.keras.models.load_model('keras_model.h5')
video = cv2.VideoCapture(0)
while True:
check,frame = video.read()
img = cv2.resize(frame,(224,224))
# Predict Result
prediction = model.predict(normalised_image)
cv2.imshow("Result",frame)
key = cv2.waitKey(1)
if key == 32:
print("Closing")
break
video.release()
df = pd.read_csv("data.csv")
height = df["Height"].tolist()
weight = df["Weight"].tolist()
x = 250
y = m * x + c
print(f"Weight of someone with height {x} is {y}")
import pandas as pd
np.random.seed(0)
model = LinearRegression()
model.fit(X_train, y_train)rget'] = y
y_pred = model.predict(X_test)
r2 = r2_score(y_test, y_pred)
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
print(predictions_df.head())
#Column Name
col_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi',
'pedigree', 'age', 'label']
df = pd.read_csv("diabetes.csv", names=col_names).iloc[1:]
print(df.head())
features = ['pregnant', 'insulin', 'bmi', 'age','glucose','bp','pedigree']
X = df[features]
y = df.label
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
dot_data = StringIO() #Where we will store the data from our decision tree
classifier as text.
print(dot_data.getvalue())
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('diabetes.png')
Image(graph.create_png())
clf = DecisionTreeClassifier(max_depth=3)
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
dot_data = StringIO() #Where we will store the data from our decision tree
classifier as text.
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('diabetes.png')
Image(graph.create_png())
import numpy as np
import pandas as pd
datasets = pd.read_csv('Social_Network_Ads.csv')
X = datasets.iloc[:, [2,3]].values
Y = datasets.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set
# Feature Scaling
sc_X = StandardScaler()
X_Train = sc_X.fit_transform(X_Train)
X_Test = sc_X.transform(X_Test)
classifier.fit(X_Train,Y_Train)
Y_Pred = classifier.predict(X_Test)
cm = confusion_matrix(Y_Test, Y_Pred)
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(Y_Set)):
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(Y_Set)):
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
df = pd.read_csv('diabetes.csv')
print(df.head())
from sklearn.model_selection import train_test_split
X = df[["glucose", "bloodpressure"]]
y = df["diabetes"]
sc = StandardScaler()
x_train_1 = sc.fit_transform(x_train_1)
x_test_1 = sc.fit_transform(x_test_1)
model_1 = GaussianNB()
model_1.fit(x_train_1, y_train_1)
y_pred_1 = model_1.predict(x_test_1)
X = df[["glucose", "bloodpressure"]]
y = df["diabetes"]
sc = StandardScaler()
x_train_2 = sc.fit_transform(x_train_2)
x_test_2 = sc.fit_transform(x_test_2)
model_2 = LogisticRegression(random_state = 0)
model_2.fit(x_train_2, y_train_2)
y_pred_2 = model_2.predict(x_test_2)
accuracy = accuracy_score(y_test_2, y_pred_2)
print(accuracy)
#Uploading the csv
from google.colab import files
data_to_load = files.upload()
import pandas as pd
df = pd.read_csv('income.csv')
print(df.head())
print(df.describe())
from sklearn.model_selection import train_test_split
sc = StandardScaler()
x_train_1 = sc.fit_transform(x_train_1)
x_test_1 = sc.fit_transform(x_test_1)
model_1 = GaussianNB()
model_1.fit(x_train_1, y_train_1)
y_pred_1 = model_1.predict(x_test_1)
x_train_2 = sc.fit_transform(x_train_2)
x_test_2 = sc.fit_transform(x_test_2)
model_2 = LogisticRegression(random_state = 0)
model_2.fit(x_train_2, y_train_2)
y_pred_2 = model_2.predict(x_test_2)
df = pd.read_csv("petals_sepals.csv")
print(df.head())
print(X)
wcss = []
#Here the range is taken till 11 because we just need 10 cluster points.
for i in range(1, 11):
kmeans = KMeans(n_clusters=i, init='k-means++', random_state = 42)
kmeans.fit(X)
plt.figure(figsize=(10,5))
sns.lineplot(wcss, marker='o', color='red')
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
kmeans = KMeans(n_clusters = 3, init = 'k-means++', random_state = 42)
y_kmeans = kmeans.fit_predict(X)
plt.figure(figsize=(15,7))
sns.scatterplot(x = X[y_kmeans == 0, 0], y = X[y_kmeans == 0, 1], color =
'yellow', label = 'Cluster 1')
sns.scatterplot(x = X[y_kmeans == 1, 0], y = X[y_kmeans == 1, 1], color =
'blue', label = 'Cluster 2')
sns.scatterplot(x = X[y_kmeans == 2, 0], y = X[y_kmeans == 2, 1], color =
'green', label = 'Cluster 3')
sns.scatterplot(x = kmeans.cluster_centers_[:, 0], y =
kmeans.cluster_centers_[:, 1], color = 'red', label =
'Centroids',s=100,marker=',')
plt.grid(False)
plt.title('Clusters of Flowers')
plt.xlabel('Petal Size')
plt.ylabel('Sepal Size')
plt.legend()
plt.show()
https://github.com/earlyann/grocery_market_basket_analysis/blob/main/groc_v2.ipynb
import numpy as np
model = Sequential()
model.add(Dense(2, input_shape=(2,)))
model.add(Activation('sigmoid'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.summary()
import numpy as np
import yaml
import datetime
import pickle
import codecs
try:
p = yaml.safe_load(input)
print( error )
exit(1)
g = Github(p["user"], p["password"])
number_of_reps = p["items"]
github_server_link = "https://github.com/"
last_tables_file_name = 'last_table_data.pickle'
md_file_name = 'readme.md'
# Main query
results = []
# print(rep.url) # Everything are here as json file (You can use it instead of the API)
rep_prop = [index+1]
rep_prop.append("[{}]({})".format(rep.name, link))
rep_prop.append(rep.description)
rep_prop.append(rep.language)
rep_prop.append(rep.stargazers_count)
rep_prop.append(rep.forks)
results.append(rep_prop)
break
for i in range(len(names_of_props)):
table_data[0][i] = names_of_props[i]
for i in range(number_of_reps):
for j in range(len(names_of_props)):
table_data[i+1][j] = results[i][j]
table = GithubFlavoredMarkdownTable(table_data)
table_str = table.table
now = datetime.datetime.now()
f.write("# Top %s Github repositories\n" % p["search"])
f.write("Here is a list of the top-%s %s Github repositories sorted by the number of stars.\n" %
(p["items"], p["search"]))
f.write("The query that has been used for the GitHub search API is \"%s" % p["search"] + "\".\n")
f.write(table_str)