Ke - Lab
Ke - Lab
LAB MANUVAL
STUDENT NAME :
REGISTER NUMBER :
PROGRAMME : UG
1
CCS350 - KNOWLEDGE ENGINEERING
PRACTICAL EXERCISES:
2
TABLE OF CONTENT
3
EX.NO:01
AIM :
ALGORITHM :
PROGRAM :
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# Create the dataset
data = {
'Project': ['Project_A', 'Project_B', 'Project_C', 'Project_D',
'Project_E'],
'Number_of_Workers': [10, 15, 20, 25, 30],
'Years_of_Experience': [5, 7, 9, 6, 8],
'Success_rate': [80, 85, 75, 90, 88]
}
df = pd.DataFrame(data)
4
# Define features and target variable
X = df[['Number_of_Workers', 'Years_of_Experience']]
y = df['Success_rate']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict the success rate for the new project
new_project = pd.DataFrame({
'Number_of_Workers': [15],
'Years_of_Experience': [6]
})
new_success_pred = model.predict(new_project)
print(f'Predicted success for the new project: {new_success_pred[0]}')
OUTPUT :
RESULT :
5
EX.NO:02
AIM :
ALGORITHM :
PROGRAM :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# Sample data
data = {
'Project': ['Project_A', 'Project_B', 'Project_C', 'Project_D',
'Project_E'],
'Number_of_Workers': [10, 15, 20, 25, 30],
'Years_of_Experience': [5, 7, 9, 6, 8],
'Success_rate': [80, 85, 75, 90, 88]
}
# Create a DataFrame
6
df = pd.DataFrame(data)
# Define features and target variable
X = df[['Number_of_Workers', 'Years_of_Experience']]
y = df['Success_rate']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict the success rate for the new project
new_project = pd.DataFrame({
'Number_of_Workers': [15],
'Years_of_Experience': [6]
})
new_success_pred = model.predict(new_project)
print(f'Predicted success rate for the new project:
{new_success_pred[0]}')
# Create line plots
plt.figure(figsize=(14, 6))
# Line plot for Number of Workers vs. Success Rate
plt.subplot(1, 2, 1)
plt.plot(df['Number_of_Workers'], df['Success_rate'], marker='o')
plt.xlabel('Number of Workers')
plt.ylabel('Success Rate')
plt.title('Number of Workers vs Success Rate')
# Line plot for Years of Experience vs. Success Rate
plt.subplot(1, 2, 2)
plt.plot(df['Years_of_Experience'], df['Success_rate'], marker='o')
plt.xlabel('Years of Experience')
plt.ylabel('Success Rate')
plt.title('Years of Experience vs Success Rate')
plt.tight_layout()
plt.show()
7
OUTPUT :
RESULT :
8
EX.NO:03
Perform operations on Probability Based Reasoning
DATE:
AIM :
ALGORITHM :
PROGRAM :
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# Create the dataset
data = {
'Project': ['Project_A', 'Project_B', 'Project_C', 'Project_D',
'Project_E'],
'Number_of_Workers': [10, 15, 20, 25, 30],
'Years_of_Experience': [5, 7, 9, 6, 8],
'Success_rate': [80, 85, 75, 90, 88]
9
}
# Create a DataFrame
df = pd.DataFrame(data)
# Define features and target variable
X = df[['Number_of_Workers', 'Years_of_Experience']]
y = df['Success_rate']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict the success rate for the new project
new_project = pd.DataFrame({
'Number_of_Workers': [15],
'Years_of_Experience': [6]
})
new_success_pred = model.predict(new_project)
print(f'Predicted success for the new project: {new_success_pred[0]}')
# Bayesian Inference
# Prior probability of success (based on past data)
prior_success_prob = 0.8 # Initial belief (e.g., 80% success rate)
# Likelihood of evidence given success (P(E|S))
likelihood_given_success = 0.95 # Probability of having this
evidence if the project is successful
# Likelihood of evidence given failure (P(E|~S))
likelihood_given_failure = 0.4 # Probability of having this evidence
if the project is not successful
# Posterior probability using Bayes' Theorem
posterior_success_prob = (likelihood_given_success *
prior_success_prob) / (
(likelihood_given_success * prior_success_prob) +
(likelihood_given_failure * (1 - prior_success_prob)))
print(f'Updated success probability for the new project:
{posterior_success_prob * 100:.2f}%')
10
OUTPUT :
RESULT :
11
EX.NO:04
AIM :
ALGORITHM :
PROGRAM :
12
from bs4 import BeautifulSoup
nltk.download('vader_lexicon')
sid = SentimentIntensityAnalyzer()
def analyze_believability(text):
sentiment_scores = sid.polarity_scores(text)
believability = 1.0 - sentiment_scores['neg']
return believability
def analyze_source_credibility(url):
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
source_credibility = 0.7
return source_credibility
except Exception as e:
print(f"Error fetching or analyzing the source: {e}")
return None
if __name__ == "__main__":
text = "This is a sample text that you want to analyze for
believability."
source_url = "https://www.example.com/sample-article"
believability_score = analyze_believability(text)
source_credibility_score = analyze_source_credibility(source_url)
if believability_score is not None and source_credibility_score is
not None:
final_believability_score = (believability_score +
source_credibility_score) / 2
print(f"Believability Score: {final_believability_score}")
else:
print("Unable to calculate believability due to errors.")
13
OUTPUT :
RESULT :
14
EX.NO:05
AIM :
ALGORITHM :
PROGRAM :
15
print(f"Initial Model Accuracy: {initial_accuracy:.2f}")
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
refined_accuracy = accuracy_score(y_test, y_pred)
print(f"Refined Model Accuracy: {refined_accuracy:.2f}")
OUTPUT :
RESULT :
16
EX.NO:06
AIM :
ALGORITHM :
PROGRAM :
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
study_hours = np.array([2, 4, 6, 8, 10, 12]).reshape(-1, 1)
exam_scores = np.array([30, 40, 55, 60, 75, 85])
model = LinearRegression()
model.fit(study_hours, exam_scores)
predicted_scores = model.predict(study_hours)
plt.scatter(study_hours, exam_scores, label='Actual scores')
17
plt.plot(study_hours, predicted_scores, color='red', label='Predicted
scores')
plt.xlabel('Study Hours')
plt.ylabel('Exam Scores')
plt.legend()
plt.show()
OUTPUT :
RESULT :
18
EX.NO:07
AIM :
ALGORITHM :
PROGRAM :
19
g.add((EX.enrolledIn, RDFS.domain, EX.Student))
g.add((EX.enrolledIn, RDFS.range, EX.Course))
g.add((EX.offers, RDFS.domain, EX.University))
g.add((EX.offers, RDFS.range, EX.Course))
g.serialize("university_ontology.ttl", format="turtle")
print(g.serialize(format="turtle"))
OUTPUT :
<http://example.org/university#Course> a rdfs:Class .
<http://example.org/university#Professor> a rdfs:Class .
<http://example.org/university#Student> a rdfs:Class .
<http://example.org/university#University> a rdfs:Class .
<http://example.org/university#enrolledIn> a rdf:Property ;
rdfs:domain <http://example.org/university#Student> ;
rdfs:range <http://example.org/university#Course> .
<http://example.org/university#offers> a rdf:Property ;
rdfs:domain <http://example.org/university#University> ;
rdfs:range <http://example.org/university#Course> .
<http://example.org/university#teaches> a rdf:Property ;
rdfs:domain <http://example.org/university#Professor> ;
rdfs:range <http://example.org/university#Course> .
RESULT :
20