0% found this document useful (0 votes)
9 views8 pages

Class 1 - 2024 Business Analytics

Business analytics class material

Uploaded by

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

Class 1 - 2024 Business Analytics

Business analytics class material

Uploaded by

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

8/31/24, 10:56 AM Class 1_2024

In [1]:
import numpy as np

1. Creating Arrays
In [2]:
# Creating an array from a list
arr1 = np.array([1, 2, 3, 4, 5])
print("Array from list:", arr1)
arr1[0]

Array from list: [1 2 3 4 5]


1
Out[2]:

In [3]:
# Creating an array with zeros
arr2 = np.zeros((3, 4))
print("Array of zeros:\n", arr2)
arr2[2,3]

Array of zeros:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
0.0
Out[3]:

In [4]:
# Creating an array with ones
arr3 = np.ones((2, 3), dtype = int)
print("Array of ones:\n", arr3)

Array of ones:
[[1 1 1]
[1 1 1]]

In [5]:
# Creating an array with a range of numbers
arr4 = np.arange(10)
print("Array with range of numbers:", arr4)

Array with range of numbers: [0 1 2 3 4 5 6 7 8 9]

In [6]:
# Creating an array with evenly spaced values
arr5 = np.linspace(0, 1, 5)
print("Array with evenly spaced values:", arr5)

Array with evenly spaced values: [0. 0.25 0.5 0.75 1. ]

2. Indexing and Slicing Arrays


In [7]:
# Creating a sample array
arr = np.array([10, 20, 30, 40, 50])

# Indexing
print("Element at index 0:", arr[0])
print("Element at index 3:", arr[3])

file:///C:/Users/Jasashwi Mandal/OneDrive - NATIONAL INSTITUTE OF INDUSTRIAL ENGINEERING/COURSES/Executive MBA_Maritime_Stati… 1/8


8/31/24, 10:56 AM Class 1_2024
Element at index 0: 10
Element at index 3: 40

In [8]:
# Slicing
print("Elements from index 1 to 3:", arr[1:4])
print("Elements from the start to index 3:", arr[:4])
print("Elements from index 2 to the end:", arr[2:])

Elements from index 1 to 3: [20 30 40]


Elements from the start to index 3: [10 20 30 40]
Elements from index 2 to the end: [30 40 50]

3. Multi-dimensional Arrays
In [9]:
# Creating a 2D array
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

In [10]:
print("Element at row 1, column 2:", arr2d[0, 1])

Element at row 1, column 2: 2

In [11]:
# Indexing
print("Element at row 1, column 2:", arr2d[0, 1])

Element at row 1, column 2: 2

In [12]:
# Slicing
print("First two rows and columns 1 and 2:\n", arr2d[:2, 0:2])

First two rows and columns 1 and 2:


[[1 2]
[4 5]]

4. Basic Mathematical Operations


In [13]:
# Creating sample arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

In [14]:
# Element-wise addition
arr1 + arr2

array([5, 7, 9])
Out[14]:

In [15]:
# Element-wise subtraction
print("Element-wise subtraction:", arr1 - arr2)

Element-wise subtraction: [-3 -3 -3]

In [16]:
# Element-wise multiplication
print("Element-wise multiplication:", arr1 * arr2)

Element-wise multiplication: [ 4 10 18]


file:///C:/Users/Jasashwi Mandal/OneDrive - NATIONAL INSTITUTE OF INDUSTRIAL ENGINEERING/COURSES/Executive MBA_Maritime_Stati… 2/8
8/31/24, 10:56 AM Class 1_2024

In [17]:
# Element-wise division
print("Element-wise division:", arr1 / arr2)

Element-wise division: [0.25 0.4 0.5 ]

5. Universal Functions (ufuncs)


In [18]:
# Creating a sample array
arr = np.array([0, np.pi/2, np.pi])

In [19]:
# Using universal functions
print("Sine of the array elements:", np.sin(arr))
print("Exponential of the array elements:", np.exp(arr))
print("Square root of the array elements:", np.sqrt(arr))

Sine of the array elements: [0.0000000e+00 1.0000000e+00 1.2246468e-16]


Exponential of the array elements: [ 1. 4.81047738 23.14069263]
Square root of the array elements: [0. 1.25331414 1.77245385]

Exercise 1: Create a 3x3 Identity Matrix


In [20]:
# Creating a 3x3 identity matrix
identity_matrix = np.eye(3)
print("3x3 Identity Matrix:\n", identity_matrix)

3x3 Identity Matrix:


[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

Exercise 2: Reshape a 1D Array to 2D


In [21]:
# Creating a 1D array
arr = np.arange(1, 10)

# Reshaping to 2D array (3x3)


arr_reshaped = arr.reshape(3, 3)
print("Reshaped 2D array:\n", arr_reshaped)

Reshaped 2D array:
[[1 2 3]
[4 5 6]
[7 8 9]]

Exercise 3: Calculate the Dot Product of Two


Matrices
In [22]:
# Creating two matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Calculating the dot product


file:///C:/Users/Jasashwi Mandal/OneDrive - NATIONAL INSTITUTE OF INDUSTRIAL ENGINEERING/COURSES/Executive MBA_Maritime_Stati… 3/8
8/31/24, 10:56 AM Class 1_2024
dot_product = np.dot(A, B)
print("Dot product of A and B:\n", dot_product)

Dot product of A and B:


[[19 22]
[43 50]]

Exercise 4: Find the Mean and Standard


Deviation of an Array
In [23]:
# Creating a sample array
arr = np.array([1, 2, 3, 4, 5])

# Calculating mean and standard deviation


mean = np.mean(arr)
std_dev = np.std(arr)
print("Mean:", mean)
print("Standard Deviation:", std_dev)

Mean: 3.0
Standard Deviation: 1.4142135623730951

Introduction to Pandas
In [24]:
import pandas as pd

1. Creating DataFrames
In [25]:
# Creating a DataFrame from a dictionary
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}

df = pd.DataFrame(data)
print("DataFrame from dictionary:\n", df)

DataFrame from dictionary:


Name Age City
0 Alice 25 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago

Creating a DataFrame from a list of lists


In [26]:
data = [
['Alice', 25, 'New York'],
['Bob', 30, 'Los Angeles'],
['Charlie', 35, 'Chicago']
]
df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])
print("DataFrame from list of lists:\n", df)

file:///C:/Users/Jasashwi Mandal/OneDrive - NATIONAL INSTITUTE OF INDUSTRIAL ENGINEERING/COURSES/Executive MBA_Maritime_Stati… 4/8


8/31/24, 10:56 AM Class 1_2024
DataFrame from list of lists:
Name Age City
0 Alice 25 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago

Creating a DataFrame from a CSV file


In [27]:
df = pd.read_csv('Data.csv')

In [28]:
print("DataFrame from CSV file:\n", df)

DataFrame from CSV file:


Name Age City
0 Alice 25 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago

2. Indexing and Selecting Data


In [29]:
# Selecting a single column
print("Selecting 'Name' column:\n", df['Name'])

Selecting 'Name' column:


0 Alice
1 Bob
2 Charlie
Name: Name, dtype: object

In [30]:
# Selecting multiple columns
print("Selecting 'Name' and 'Age' columns:\n", df[['Name', 'Age']])

Selecting 'Name' and 'Age' columns:


Name Age
0 Alice 25
1 Bob 30
2 Charlie 35

In [31]:
# Selecting rows by index
print("Selecting first row:\n", df.iloc[0])

Selecting first row:


Name Alice
Age 25
City New York
Name: 0, dtype: object

In [32]:
# Selecting rows by condition
print("Selecting rows where Age > 25:\n", df[df['Age'] > 25])

Selecting rows where Age > 25:


Name Age City
1 Bob 30 Los Angeles
2 Charlie 35 Chicago

file:///C:/Users/Jasashwi Mandal/OneDrive - NATIONAL INSTITUTE OF INDUSTRIAL ENGINEERING/COURSES/Executive MBA_Maritime_Stati… 5/8


8/31/24, 10:56 AM Class 1_2024

3. Modifying DataFrames
In [33]:
# Adding a new column
df['Salary'] = [70000, 80000, 90000]
print("DataFrame after adding 'Salary' column:\n", df)

DataFrame after adding 'Salary' column:


Name Age City Salary
0 Alice 25 New York 70000
1 Bob 30 Los Angeles 80000
2 Charlie 35 Chicago 90000

In [34]:
# Modifying an existing column
df['Age'] = df['Age'] + 1
print("DataFrame after modifying 'Age' column:\n", df)

DataFrame after modifying 'Age' column:


Name Age City Salary
0 Alice 26 New York 70000
1 Bob 31 Los Angeles 80000
2 Charlie 36 Chicago 90000

In [35]:
# Deleting a column
df = df.drop('City', axis=1)
print("DataFrame after deleting 'City' column:\n", df)

DataFrame after deleting 'City' column:


Name Age Salary
0 Alice 26 70000
1 Bob 31 80000
2 Charlie 36 90000

Data Cleaning: Handling Missing Values,


Filtering, and Sorting
In [36]:
# Creating a sample DataFrame with missing values
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, None, 35],
'City': ['New York', 'Los Angeles', None]
}
df = pd.DataFrame(data)

In [37]:
# Identifying missing values
print("Missing values in DataFrame:\n", df.isnull())

Missing values in DataFrame:


Name Age City
0 False False False
1 False True False
2 False False True

In [38]:
# Dropping rows with missing values
df_dropped = df.dropna()
print("DataFrame after dropping rows with missing values:\n", df_dropped)

file:///C:/Users/Jasashwi Mandal/OneDrive - NATIONAL INSTITUTE OF INDUSTRIAL ENGINEERING/COURSES/Executive MBA_Maritime_Stati… 6/8


8/31/24, 10:56 AM Class 1_2024

DataFrame after dropping rows with missing values:


Name Age City
0 Alice 25.0 New York

In [39]:
# Filling missing values
df_filled = df.fillna({'Age': df['Age'].mean(), 'City': 'Unknown'})
print("DataFrame after filling missing values:\n", df_filled)

DataFrame after filling missing values:


Name Age City
0 Alice 25.0 New York
1 Bob 30.0 Los Angeles
2 Charlie 35.0 Unknown

5. Filtering and Sorting Data


In [40]:
# Creating a sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)

# Filtering rows based on a condition


filtered_df = df[df['Age'] > 25]
print("Filtered DataFrame (Age > 25):\n", filtered_df)

Filtered DataFrame (Age > 25):


Name Age City
1 Bob 30 Los Angeles
2 Charlie 35 Chicago

In [41]:
# Sorting the DataFrame by a column
sorted_df = df.sort_values(by='Age', ascending = False)
print("DataFrame sorted by 'Age' (descending):\n", sorted_df)

DataFrame sorted by 'Age' (descending):


Name Age City
2 Charlie 35 Chicago
1 Bob 30 Los Angeles
0 Alice 25 New York

In [42]:
import matplotlib.pyplot as plt
ratings = ['Poor', 'Below Average', 'Average', 'Above Average', 'Excellent']
frequency = [2, 3, 5, 9, 1]
colors = ['blue', 'yellow', 'green', 'red', 'orange']
plt.bar(ratings, frequency, color=colors)
plt.xlabel('Rating')
plt.ylabel('Frequency')
plt.title('Rating Frequency Bar Graph')
plt.show()

file:///C:/Users/Jasashwi Mandal/OneDrive - NATIONAL INSTITUTE OF INDUSTRIAL ENGINEERING/COURSES/Executive MBA_Maritime_Stati… 7/8


8/31/24, 10:56 AM Class 1_2024

In [ ]:

file:///C:/Users/Jasashwi Mandal/OneDrive - NATIONAL INSTITUTE OF INDUSTRIAL ENGINEERING/COURSES/Executive MBA_Maritime_Stati… 8/8

You might also like