ML (Lab Programs)
ML (Lab Programs)
Lab Programs
1. Install and set-up Python and Pardas. and essential libmates Like
Numpy
● Open the .exe file, such as Python 3.12.1 and 64, then launch the
python installer.
● Choose the option to install the launcher for all users by checking
the corresponding checkbox,
● verify the python installation in windows
print("pandas library
version is: ") print(pandas.
version )
print("pandas library is successfully installed")
csv_file_path='C:\\ML_Projects\\sample_data.csv'
excel_file_path='C:\\ML_Projects\\sample_data.xlsx'
data_csv=pd.read_csv(csv_file_path)
print(data_csv)
data_excel=pd.read_excel(excel_file_path)
print(data_excel)
print(data_csv.describe())
print(data_excel.describe())
print(data_csv.dtypes)
print(data_excel.dtypes)
Output
0 Manoj 19 95
1 Dilip 20 97
2 Manjula 40 35
3 Rakesh 24 45
4 Kushal 22 80
0 Rajesh BCA 1
1 Ramesh BCA 2
2 Swati BCOM 1
3 Florina BCOM 3
4 Pooja BBA 2
5 Raghu BBA 4
Data Descriptions:
Age Score
Sem
count 6.000000
mean 2.166667
std 1.169045
min 1.000000
25% 1.250000
50% 2.000000
75% 2.750000
max 4.000000
Name object
Age int64
Score int64
dtype: object
Name object
Course object
Sem int64
dtype: object
Lab Program 5: Write a program to visualize the dataset to gain insights using
Matplotlib or Seaborn by plotting scatter plots, bar charts.
data= pd.read_csv('C:\\ML_Projects\\study_data.csv')
plt.figure(figsize=(14,7))
plt.subplot(1,2,1)
plt.xlabel('Study Hours')
plt.ylabel('Exam Scores')
plt.grid(True)
bins=[0,2,4,6,8,10,12]
plt.subplot(1,2,2)
grouped_data.plot(kind='bar', color='pink')
plt.xticks(rotation=0)
plt.tight_layout()
plt.show()
output
Lab Program 6: Write a program to Handle missing data, encode
categorical variables, and perform feature scaling.
import pandas as pd
data={
}
df= pd.DataFrame(data)
imputer = SimpleImputer(strategy='mean')
print(df)
#Encoding categorical variables
encoder = OneHotEncoder()
encoded_data = encoder.fit_transform(df[['Gender']]).toarray()
encoded_df= pd.DataFrame(encoded_data,
columns=encoder.get_feature_names_out(['Gender']))
print(encoded_df)
scaler = StandardScaler()
print(scaled_df)
Output
Lab Program 7: Write a program to implement a k-Nearest Neighbours (k-NN) classifier
using scikitlearn and Train the classifier on the dataset and evaluate its performance.
X = np.array([[88, 75], [95, 90], [60, 50], [45, 30], [30, 48], [85, 95], [70, 60], [50, 55], [40, 45], [60,
70]])
knn = KNeighborsClassifier(n_neighbors=3)
y_pred=knn.predict(X_test)
accuracy=accuracy_score(y_test, y_pred)
predicted_outcome=knn.predict(user_input)
else:
OutPut:
Lab Program 08. Write a program to implement a linear regression model for regression
tasks and Train the model on a dataset.
#Regression Algorithm
import numpy as np
import matplotlib.pyplot as plt
# Getting the Solution that is Y- value, for new data set that is X- value.
new_X = 7.5
new_Y=a* new_X + b
print()
print (f"Predict Y-value using= {a:.2f} + {b:.2f}X for new X- value= {new_X} ")
print (f"Predicted Y-value is =(new_Y:.2f) ")
Lab Program 09. Write a program to implement a decision tree classifier using
scikit-learn and visualize the decision tree and understand its splits.
from sklearn.tree import DecisionTreeClassifier, plot_tree
from matplotlib.pyplot import figure,show
import matplotlib.pyplot as plt
Kmeans.fit(data)
print("Integer labels provided to each data points are:")
labels=Kmeans.Labels_
print(labels)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("K-Means clustering (k=" + str(k) + ")")
plt.grid()
plt.show()