Practical for IML

 

Aim 1: Explore any one Machine Learning tool (Scikit-learn)

Program

# Exploring Scikit-learn library import sklearn print("Scikit-learn version:", sklearn.__version__) print("Scikit-learn is used for machine learning tasks such as:") print("Classification, Regression, Clustering, and Model Evaluation")

Aim 2: NumPy basic operations

Program

import numpy as np # Convert list to 1D NumPy array list1 = [1, 2, 3, 4, 5] arr1 = np.array(list1) print("1D Array:", arr1) # Create 3x3 matrix from 2 to 10 matrix = np.arange(2, 11).reshape(3, 3) print("3x3 Matrix:\n", matrix) # Append values to array arr2 = np.append(arr1, [6, 7]) print("Appended Array:", arr2) # Reshape array from 3x2 to 2x3 arr3 = np.array([[1, 2], [3, 4], [5, 6]]) reshaped = arr3.reshape(2, 3) print("Reshaped Array:\n", reshaped)

Aim 3: NumPy mathematical operations

Program

import numpy as np a = np.array([10, 20, 30]) b = np.array([2, 4, 6]) # Element-wise operations print("Addition:", a + b) print("Subtraction:", a - b) print("Multiplication:", a * b) print("Division:", a / b) # Round elements c = np.array([1.2, 2.5, 3.7]) print("Rounded Array:", np.round(c)) # Mean across dimension d = np.array([[1, 2, 3], [4, 5, 6]]) print("Mean across rows:", np.mean(d, axis=1)) # Difference between neighboring elements e = np.array([10, 15, 25, 40]) print("Difference:", np.diff(e))

Aim 4: Pandas – missing values & duplicates

Program

import pandas as pd data = { "Name": ["A", "B", "C", "C"], "Marks": [85, None, 90, 90] } df = pd.DataFrame(data) print("Original DataFrame:\n", df) # Drop missing values df_no_nan = df.dropna() print("\nAfter dropping missing values:\n", df_no_nan) # Remove duplicates df_no_dup = df_no_nan.drop_duplicates() print("\nAfter removing duplicates:\n", df_no_dup)

Aim 5: Pandas – NaN checking and filtering

Program

import pandas as pd data = { "A": [1, 2, None], "B": [4, 5, 6], "C": [None, 8, 9] } df = pd.DataFrame(data) print("Original DataFrame:\n", df) # Columns with all values present print("\nColumns without NaN:\n", df.dropna(axis=1)) # Check NaN positions print("\nNaN positions:\n", df.isna()) # Drop rows with any NaN df_clean = df.dropna() print("\nAfter dropping rows with NaN:\n", df_clean)

Aim 6: Scikit-learn dataset information

Program

from sklearn.datasets import load_iris data = load_iris() print("Keys:", data.keys()) print("Number of rows and columns:", data.data.shape) print("Feature names:", data.feature_names) print("Description:\n", data.DESCR[:500])

Aim 7: K-Nearest Neighbour algorithm

Program

from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import accuracy_score # Load dataset data = load_iris() X = data.data y = data.target # Split dataset X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # KNN model knn = KNeighborsClassifier(n_neighbors=3) knn.fit(X_train, y_train) # Prediction y_pred = knn.predict(X_test) print("Accuracy:", accuracy_score(y_test, y_pred))

Aim 8: Machine Learning algorithm (Linear Regression)

Program

import numpy as np from sklearn.linear_model import LinearRegression # Dataset X = np.array([[1], [2], [3], [4], [5]]) y = np.array([2, 4, 6, 8, 10]) # Model model = LinearRegression() model.fit(X, y) # Prediction prediction = model.predict([[6]]) print("Predicted value for input 6:", prediction)

Comments

Popular posts from this blog

Unit - 1 Introduction to machine learning

Unit - II ML Python Libraries