1: Implementing Linear Regression Implement simple linear regression to predict the
price of houses based on their size. Use the dataset provided and calculate the slope and
intercept of the regression line
Dataset:
house_sizes = [1400, 1600, 1700, 1875, 1100]
house_prices = [245000, 312000, 279000, 308000, 199000]
Coding:
import numpy as np
house_sizes = np.array([1400, 1600, 1700, 1875, 1100])
house_prices = np.array([245000, 312000, 279000, 308000, 199000])
slope, intercept = np.polyfit(house_sizes, house_prices, 1)
print("Slope:", slope)
print("Intercept:", intercept)
Output:
Slope: 144.97884344146678
Intercept: 46057.47531734835
Exercise 2: Implementing K-Means Clustering Implement K-Means clustering
algorithm to cluster the given data points into two clusters.
Dataset: data_points = [[2, 3], [5, 6], [8, 7], [3, 5], [4, 6], [7, 9]]
Coding:
from sklearn.cluster import KMeans
data_points = [[2, 3], [5, 6], [8, 7], [3, 5], [4, 6], [7, 9]]
kmeans = KMeans(n_clusters=2)
kmeans.fit(data_points)
print("Cluster Centers:", kmeans.cluster_centers_)
Output:
Cluster Centers: [[3. 4.66666667]
[6.66666667 7.33333333]]
Exercise 3: Implementing Decision Trees Implement a decision tree classifier to classify
whether a given person will buy a computer based on their age and income.
Dataset:
features = [[23, 25000], [45, 56000], [35, 45000], [20, 34000], [55, 78000], [30, 67000]]
labels = ['No', 'Yes', 'No', 'No', 'Yes', 'Yes']
Coding:
from sklearn.tree import DecisionTreeClassifier
features = [[23, 25000], [45, 56000], [35, 45000], [20, 34000], [55, 78000], [30, 67000]]
labels = ['No', 'Yes', 'No', 'No', 'Yes', 'Yes']
clf = DecisionTreeClassifier()
clf.fit(features, labels)
# Example prediction
print(clf.predict([[40, 60000]]))
Output:
['Yes']
Exercise 4: FOLP (First Order Logic Programming) Implement a simple FOLP system
to infer relationships between objects based on rules encoded in first-order logic
Coding:
from pyswip import Prolog
prolog = Prolog()
prolog.assertz("father(bob, alice)")
prolog.assertz("father(bob, john)")
for solution in prolog.query("father(bob, Child)"):
print(solution["Child"])
Output:
alice
john
Exercise 5: Rule-Based Implementation Create a rule-based system to recommend
movies based on user preferences
Coding:
# Rule-based movie recommendation system
def recommend_movie(user_preferences):
if "action" in user_preferences and "comedy" in user_preferences:
return "Watch 'Deadpool' for a mix of action and comedy!"
elif "drama" in user_preferences:
return "Watch 'The Shawshank Redemption' for a gripping drama!"
elif "horror" in user_preferences:
return "Watch 'The Conjuring' for a spine-chilling horror experience!"
else:
return "Sorry, we couldn't find a suitable recommendation."
user_preferences = ["action", "comedy"]
print(recommend_movie(user_preferences))
Output:
Watch 'Deadpool' for a mix of action and comedy!