0% found this document useful (0 votes)
12 views16 pages

ML Lab File Vijay Kumar

The document provides an overview of fundamental functionalities of the Python programming language, including object-oriented programming concepts, functions, and libraries such as NumPy and Pandas for data manipulation and analysis. It covers various operations with arrays, statistical functions, and data handling techniques, as well as visualization using Matplotlib and scientific computing with SciPy. The content is structured as an experiment aimed at demonstrating practical applications of Python in programming and data analysis.

Uploaded by

samv0294
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

ML Lab File Vijay Kumar

The document provides an overview of fundamental functionalities of the Python programming language, including object-oriented programming concepts, functions, and libraries such as NumPy and Pandas for data manipulation and analysis. It covers various operations with arrays, statistical functions, and data handling techniques, as well as visualization using Matplotlib and scientific computing with SciPy. The content is structured as an experiment aimed at demonstrating practical applications of Python in programming and data analysis.

Uploaded by

samv0294
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

EXPERIMENT - 1

AIM - To understand and demonstrate the fundamental functionalities of the Python


programming language.

THEORY - Python is a high-level, interpreted programming language known for its simplicity
and readability. It is widely used in various fields such as web development, data analysis,
machine learning, automation, and more. Python's syntax is designed to be easy to read and
write, making it an excellent choice for beginners and experienced programmers alike.

1. Classes and Objects: Python is an object-oriented programming (OOP) language,


which means it supports the concepts of classes and objects. A class is a blueprint for
creating objects, which are instances of the class. Classes encapsulate data and
functions that operate on that data. Objects are instances that hold the actual data and
can use the class's methods.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def greet(self):
return f'Hello, my name is {self.name} and I am {self.age} years
old.'

# Create an instance of the class


person = Person('Vijay', 19)
print(person.greet())

2. Functions: Functions are blocks of reusable code that perform a specific task. They
allow for modular and organized code, making it easier to manage and debug. Python
functions are defined using the def keyword followed by the function name and
parameters.

def add(a, b):


return a + b

def subtract(a, b):


return a - b

# Using the functions


print(add(5, 3)) # Output: 8
print(subtract(5, 3)) # Output: 2

Libraries:-

1. NumPy stands for "Numerical Python." It's a powerful library in Python for
numerical and scientific computing. At its core, NumPy provides support for arrays
(grids of values) and a collection of functions to operate on these arrays.

Creating Arrays - Arrays are the basic data structure in NumPy. They can be one-
dimensional (like a list) or multi-dimensional (like a matrix).

1. import numpy as np
2.
3. # Creating a 1-dimensional array
4. array_1d = np.array([1, 2, 3, 4, 5])
5. print("1D array:", array_1d)
6.
7. # Creating a 2-dimensional array
8. array_2d = np.array([[1, 2, 3], [4, 5, 6]])
9. print("2D array:\n", array_2d)
10.

OUTPUT –

Array Operations - NumPy provides functions to create arrays filled with zeros, ones,
or a range of numbers. These operations are useful for initializing arrays.

1. # Array of zeros
2. zeros_array = np.zeros((3, 3))
3. print("Zeros array:\n", zeros_array)
4.
5. # Array of ones
6. ones_array = np.ones((2, 4))
7. print("Ones array:\n", ones_array)
8.
9. # Array with a range of values
10. range_array = np.arange(0, 10, 2)
11. print("Range array:", range_array)
12.
13. # Array with evenly spaced values
14. linspace_array = np.linspace(0, 1, 5)
15. print("Linspace array:", linspace_array)
16.

OUTPUT –

Array Reshaping - Reshaping allows changing the shape of an array without


changing its data. This is useful when you need a different dimensional structure for
your data.

1. original_array = np.arange(12)
2. reshaped_array = original_array.reshape((3, 4))
3. print("Original array:", original_array)
4. print("Reshaped array:\n", reshaped_array)
5.

OUTPUT –
Basic Arithmetic Operations - NumPy allows for element-wise arithmetic operations
on arrays, which means you can perform operations like addition, subtraction,
multiplication, and division on corresponding elements of arrays.

1. a = np.array([1, 2, 3])
2. b = np.array([4, 5, 6])
3.
4. # Element-wise addition
5. print("Addition:", a + b)
6.
7. # Element-wise subtraction
8. print("Subtraction:", a - b)
9.
10. # Element-wise multiplication
11. print("Multiplication:", a * b)
12.
13. # Element-wise division
14. print("Division:", a / b)
15.

OUTPUT –
Statistical Operations - NumPy provides functions to calculate statistical measures
such as mean, sum, standard deviation, minimum, and maximum on arrays.

1. stats_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])


2.
3. print("Mean:", np.mean(stats_array))
4. print("Sum:", np.sum(stats_array))
5. print("Standard Deviation:", np.std(stats_array))
6. print("Minimum:", np.min(stats_array))
7. print("Maximum:", np.max(stats_array))
8.

OUTPUT –

Indexing and Slicing - Indexing and slicing in NumPy allows accessing and
modifying specific elements or subsets of an array. This is similar to list indexing and
slicing in Python but more powerful for multi-dimensional arrays.

1. array = np.array([10, 20, 30, 40, 50])


2.
3. # Indexing
4. print("Element at index 2:", array[2])
5.
6. # Slicing
7. print("Elements from index 1 to 3:", array[1:4])
8.

OUTPUT –

Broadcasting - Broadcasting allows NumPy to perform element-wise operations on


arrays of different shapes. This feature eliminates the need to explicitly reshape
arrays for compatible operations.

1. array1 = np.array([1, 2, 3])


2. array2 = np.array([[4], [5], [6]])
3.
4. # Broadcasting addition
5. result = array1 + array2
6. print("Broadcasting result:\n", result)
7.

OUTPUT –

Linear Algebra - NumPy supports various linear algebra operations, such as matrix
multiplication, transpose, inverse, and determinant. These operations are essential for
scientific computing and machine learning.

1. # Creating matrices
2. matrix1 = np.array([[1, 2], [3, 4]])
3. matrix2 = np.array([[5, 6], [7, 8]])
4.
5. # Matrix multiplication
6. matrix_product = np.dot(matrix1, matrix2)
7. print("Matrix product:\n", matrix_product)
8.
9. # Transpose of a matrix
10. transpose_matrix = np.transpose(matrix1)
11. print("Transpose of matrix1:\n", transpose_matrix)
12.

OUTPUT –

2. Pandas provides two primary data structures: Series (1-dimensional) and


DataFrame (2-dimensional). These structures are optimized for handling and
analyzing data, making it easier to perform operations like filtering, grouping, and
statistical analysis.

1. Series: A one-dimensional array-like object that can hold any data type (e.g.,
integers, strings, floats). It’s similar to a column in a spreadsheet.

1. import pandas as pd
2.
3. # Creating a Series
4. series = pd.Series([10, 20, 30, 40, 50])
5. print("Series:\n", series)
6.
7. # Creating a DataFrame
8. data = {
9. 'Name': ['Alice', 'Bob', 'Charlie', 'David'],
10. 'Age': [25, 30, 35, 40],
11. 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']
12. }
13. df = pd.DataFrame(data)
14. print("DataFrame:\n", df)
15.

2. DataFrame: A two-dimensional, size-mutable, and potentially heterogeneous


tabular data structure with labeled axes (rows and columns). It's like an Excel
spreadsheet or a SQL table.

1. # Selecting a column
2. print("Selecting 'Age' column:\n", df['Age'])
3.
4. # Adding a new column
5. df['Salary'] = [50000, 60000, 70000, 80000]
6. print("DataFrame with new 'Salary' column:\n", df)
7.
8. # Filtering rows based on a condition
9. filtered_df = df[df['Age'] > 30]
10. print("Filtered DataFrame (Age > 30):\n", filtered_df)
11.

Selecting Columns and Rows

1. # Selecting a column
2. print("Selecting 'Age' column:\n", df['Age'])
3.
4. # Selecting multiple columns
5. print("Selecting 'Name' and 'City' columns:\n", df[['Name', 'City']])
6.
7. # Selecting a row by index
8. print("Selecting the first row:\n", df.iloc[0])
9.
10. # Selecting a row by condition
11. print("Selecting rows where Age > 30:\n", df[df['Age'] > 30])
12.

OUTPUT –
This part demonstrates how to select specific columns and rows. You can select
single or multiple columns and filter rows based on conditions using boolean indexing.

Adding, Removing, and Handling Missing Data

1. # Adding a new column


2. df['Salary'] = [50000, 60000, 70000, 80000]
3. print("DataFrame with new 'Salary' column:\n", df)
4.
5. # Removing a column
6. df = df.drop(columns=['Salary'])
7. print("DataFrame after removing 'Salary' column:\n", df)
8.
9. # Adding NaN values
10. df_with_nan = df.copy()
11. df_with_nan.loc[1, 'Age'] = None
12. print("DataFrame with NaN:\n", df_with_nan)
13.
14. # Handling missing values
15. # Filling NaN with a specific value
16. df_filled = df_with_nan.fillna(0)
17. print("DataFrame after filling NaN with 0:\n", df_filled)
18.
19. # Dropping rows with NaN values
20. df_dropped = df_with_nan.dropna()
21. print("DataFrame after dropping rows with NaN:\n", df_dropped)
22.
23.

Here, we cover adding and removing columns, and handling missing data. You can
introduce NaN (missing) values, fill them with a specific value, or drop rows containing
NaN values.

OUTPUT –

Reading and Writing Data


1. # Reading data from a CSV file
2. df_from_csv = pd.read_csv('path_to_file.csv')
3. print("DataFrame from CSV:\n", df_from_csv)
4.
5. # Writing data to a CSV file
6. df.to_csv('output.csv', index=False)
7. print("DataFrame written to 'output.csv'")
8.

This part demonstrates how to read data from and write data to CSV files using
read_csv and to_csv methods. These methods are useful for importing and exporting
data.

3. Matplotlib is a comprehensive library for creating static, animated, and interactive


visualizations in Python. It is particularly useful for generating plots, charts, and
figures to help visualize data. Here are a few key components of Matplotlib:

1. Figure: The overall window or page that everything is drawn on. It can contain
multiple plots.

2. Axes: The area on which data is plotted. A single figure can have multiple axes
(plots) arranged in a grid.

3. Plot: The actual visual representation of data, such as a line plot, scatter plot,
bar chart, etc.

Creating a Simple Line Plot –

1. import matplotlib.pyplot as plt


2.
3. # Creating data
4. x = [1, 2, 3, 4, 5]
5. y = [2, 3, 5, 7, 11]
6.
7. # Creating a line plot
8. plt.plot(x, y)
9. plt.xlabel('X-axis')
10. plt.ylabel('Y-axis')
11. plt.title('Simple Line Plot')
12. plt.show()
13.

This code creates a basic line plot with x and y data points. The plt.plot() function is
used to create the plot, and plt.show() displays it.
OUTPUT –

Creating a Scatter Plot

1. # Creating data
2. x = [1, 2, 3, 4, 5]
3. y = [2, 3, 5, 7, 11]
4.
5. # Creating a scatter plot
6. plt.scatter(x, y)
7. plt.xlabel('X-axis')
8. plt.ylabel('Y-axis')
9. plt.title('Simple Scatter Plot')
10. plt.show()
11.

A scatter plot is created using the plt.scatter() function. It is useful for visualizing the
relationship between two variables.

OUTPUT –
Creating a Bar Chart
1. # Creating data
2. categories = ['A', 'B', 'C', 'D']
3. values = [4, 7, 1, 8]
4.
5. # Creating a bar chart
6. plt.bar(categories, values)
7. plt.xlabel('Categories')
8. plt.ylabel('Values')
9. plt.title('Simple Bar Chart')
10. plt.show()
11.
12.

A bar chart is created using the plt.bar() function. It is useful for comparing different
categories of data.

OUTPUT –
Creating a Histogram
1. # Creating data
2. data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
3.
4. # Creating a histogram
5. plt.hist(data, bins=5)
6. plt.xlabel('Data')
7. plt.ylabel('Frequency')
8. plt.title('Simple Histogram')
9. plt.show()
10.
11.

A histogram is created using the plt.hist() function. It is useful for visualizing the distribution of
a dataset.

OUTPUT –
4. SciPy stands for "Scientific Python" and is an open-source Python library used for scientific
and technical computing. It builds on NumPy and provides a large collection of mathematical
algorithms and convenience functions, making it easier to perform scientific and engineering
tasks. Here are a few key components of SciPy:
1. Linear Algebra: Provides functions for matrix operations, solving linear systems,
eigenvalue problems, and more.
2. Optimization: Contains functions for finding the minimum or maximum of functions
(optimization), including linear programming and curve fitting.
3. Integration: Offers methods for calculating integrals, including numerical integration and
ordinary differential equations (ODE) solvers.
4. Statistics: Includes functions for statistical distributions, hypothesis testing, and
descriptive statistics.
5. Signal Processing: Provides tools for filtering, signal analysis, and Fourier transforms.

Linear Algebra
1. import numpy as np
2. from scipy import linalg
3.
4. # Creating a matrix
5. A = np.array([[1, 2], [3, 4]])
6.
7. # Computing the determinant
8. det = linalg.det(A)
9. print("Determinant:", det)
10.
11. # Solving a linear system of equations
12. b = np.array([5, 6])
13. x = linalg.solve(A, b)
14. print("Solution:", x)
15.

This code demonstrates how to compute the determinant of a matrix and solve a linear system
of equations using SciPy's linear algebra module

OUTPUT –

Optimization
1. from scipy.optimize import minimize
2.
3. # Defining the objective function
4. def objective(x):
5. return x**2 + 5*np.sin(x)
6.
7. # Finding the minimum
8. result = minimize(objective, x0=0)
9. print("Minimum:", result.x)
10.

This code demonstrates how to find the minimum of a function using SciPy's optimization
module.

OUTPUT –

You might also like