DEV Lab Record - Merged
DEV Lab Record - Merged
VISUALISATION
Branch : ……………………………….
BONAFIDE CERTIFICATE
1
1. BASIC CALCULATOR
1. Problem Statement: Install the python software and write a python program
to create a simple calculator which can perform basic arithmetic operations
like addition, subtraction, multiplication or division depending upon the user
input.
4. A. Input: Get the option for performing operation, get the numbers for doing
basic operations. For example it takes the input as follows
6. Coding:
# Python program for simple calculator
# Function to add two numbers
def add(num1, num2):
return num1 + num2
# Function to subtract two numbers
def subtract(num1, num2):
return num1 - num2
# Function to multiply two numbers
def multiply(num1, num2):
return num1 * num2
2
# Function to divide two numbers
def divide(num1, num2):
return num1 / num2
print("Please select operation -\n" \
"1. Add\n" \
"2. Subtract\n" \
"3. Multiply\n" \
"4. Divide\n")
# Take input from the user
select = int(input("Select operations form 1, 2, 3, 4 :"))
number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))
if select == 1:
print(number_1, "+", number_2, "=",
add(number_1, number_2))
elif select == 2:
print(number_1, "-", number_2, "=",
subtract(number_1, number_2))
elif select == 3:
print(number_1, "*", number_2, "=",
multiply(number_1, number_2))
elif select == 4:
print(number_1, "/", number_2, "=",
divide(number_1, number_2))
else:
print("Invalid input")
7. Test Case:
20 * 13 = 260
3
2. DATA VISUALIZATIONS
3. Problem Analysis: Take two different datasets and plotting different charts
and graphs.
A. Input: IRIS Dataset and Wine Review Data set taken as the Input.
B. Output: Different graph plotting.
4. Algorithm:
5. Coding:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
iris = pd.read_csv('iris.csv', names=['sepal_length', 'sepal_width',
'petal_length', 'petal_width', 'class'])
print(iris.head())
wine_reviews = pd.read_csv('winemag-data-130k-v2.csv', index_col=0)
wine_reviews.head()
4
# Create Line Chart Plotting
columns = iris.columns.drop(['class'])
# create x data
x_data = range(0, iris.shape[0])
# create figure and axis
fig, ax = plt.subplots()
# plot each column
for column in columns:
ax.plot(x_data, iris[column], label=column)
# set title and legend
ax.set_title('Iris Dataset')
ax.legend()
plt.show()
wine_reviews['points'].value_counts().sort_index().plot.bar()
plt.show()
wine_reviews['points'].value_counts().sort_index().plot.barh()
plt.show()
wine_reviews.groupby("country").price.mean().sort_values(ascending=False)[:
5].plot.bar()
5
plt.show()
# Correlation Matrix
corr = iris.corr()
fig, ax = plt.subplots()
# create heatmap
im = ax.imshow(corr.values)
# set labels
ax.set_xticks(np.arange(len(corr.columns)))
ax.set_yticks(np.arange(len(corr.columns)))
ax.set_xticklabels(corr.columns)
ax.set_yticklabels(corr.columns)
6. Test case :
Actual
a.
Result:
6
Histogram for Wine Review Scores.
7
Bar Chart for Wine Review Scores
8
Bar chart for Wine Review with highest cost five different
Counties.
Correlation Matrix
9
3. TIME SERIES VISUALIZATIONS
3. Problem Analysis: Take time series stock datasets and plotting different charts
and graphs.
A time-series analysis consists of methods for analyzing time series data in order
to extract meaningful insights and other useful characteristics of data.
4. Algorithm:
Step1: Load the stock price Dataset into the Data frame.
Step 2: Removing the unwanted columns from Data frame.
Step 3: Create a line plot for time series data using ‘Volume’.
Step 4: Create line sub-plots for showing seasonality.
Step 5: Create the Bar Chart for showing the month wise average ‘Volume’.
Step 6: Create the low difference in values of a specified interval.
Step 7: Create the low difference in values of a specified interval.
Step 8: Create plot the changes that occurred in data over time.
5. Coding:
# Time Series Visualisation
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
# reading the dataset using read_csv
df = pd.read_csv("stock_data.csv",parse_dates=True,index_col="Date")
# displaying the first five rows of dataset
display(df.head())
# deleting column
df.drop(columns='Unnamed: 0')
# set the title
plt.title('Volume Plot')
df['Volume'].plot()
plt.show()
# using subplot
fig, ax = plt.subplots(figsize=(10, 6))
10
# plotting bar graph
ax.bar(df_month['2016':].index,
df_month.loc['2016':, "Volume"],
width=25, align='center')
plt.show()
df.Low.diff(2).plot(figsize=(10, 6))
plt.show()
df.High.diff(2).plot(figsize=(10, 6))
plt.show()
df['Change'] = df.Close.div(df.Close.shift())
df['Change'].plot(figsize=(10, 8), fontsize=16)
plt.show()
7. Test case:
Actual
a
Result
.
:
11
Line sub-plots for showing seasonality
12
Low difference in values of a specified interval
13
Plot the changes that occurred in data over time.
14
4. WINE QUALITY ANALYSIS
3. Problem Analysis: Take wine datasets and plotting different charts and graphs.
4. Algorithm:
5. Coding:
# Red Wine Quality Analysis
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from statsmodels.multivariate.factor import Factor
from scipy.stats import skew
df_red = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-
databases/wine-quality/winequality-red.csv", delimiter=";")
print(df_red.columns)
print(df_red.describe())
print(df_red.shape)
print(df_red.info())
sns.set(rc={'figure.figsize': (14, 8)})
sns.countplot(df_red['quality'])
plt.show()
sns.pairplot(df_red)
plt.show()
sns.heatmap(df_red.corr(), annot=True, fmt='.2f', linewidths=2)
plt.show()
sns.distplot(df_red['alcohol'])
plt.show()
15
print(skew(df_red['alcohol']))
sns.boxplot(x='quality', y='alcohol', data = df_red, showfliers=False)
plt.show()
sns.jointplot(x='alcohol',y='pH',data=df_red, kind='reg')
plt.show()
6. Test Case:
Actual
a.
Result:
16
Alcohol distribution level
17
Boxplot quality of wine with respect to alcohol
concentration.
18
5. COMPNAY SALES ANALYSIS
3. Problem Analysis: Take company sales data set and plotting using lollipop
chart. It is a method for analyzing data in order to extract meaningful insights
and other useful characteristics of data.
4. Algorithm:
5. Coding:
19
6. Test Case:
Actual
a.
Result:
20
6. DATA TRANSFORAMTION
3. Problem Analysis: Create a data frame and perform the data transformation
operations like deduplication and handling missing values.
4. Algorithm:
5. Coding:
# Data frame creation and Transformation
import pandas as pd
import numpy as np
data = pd.DataFrame({"a": ["one","two"]*3,
"b": [1,1,2,3,2,3]})
print(data)
print(data.duplicated())
print(data.drop_duplicates())
data["c"]=np.nan
data.replace([np.nan],data.b.mean(),inplace=True)
print(data)
6. Test Case:
Data frame creation and transformation
a b
0 one 1
Actual 1 two 1
a. 2 one 2
Result:
3 two 3
4 one 2
5 two 3
21
0 False
1 False
2 False
3 False
4 True
5 True
dtype: bool
a b
0 one 1
1 two 1
2 one 2
3 two 3
a b c
0 one 1 2.0
1 two 1 2.0
2 one 2 2.0
3 two 3 2.0
4 one 2 2.0
5 two 3 2.0
22
7. NUMERICAL SUMMARIES AND PERCENTAGE TABLE CREATION
7. Algorithm:
8. Coding:
# Numerical Summaries and Spread Level
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
H = [155.4,160.2,162.5,150.5,152.3]
W = [53.2 ,62.5 ,70.3 ,53.2 ,57.4]
H.sort(reverse=True)
W.sort(reverse=True)
data = pd.DataFrame({"Height": H,
"Weight": W})
print("Creating dataframe:")
print(data)
# create figure and axis
fig, ax = plt.subplots()
# plot histogram
ax.plot(data['Height'], data['Weight'])
# set title and labels
ax.set_title('Height Weight Summary')
23
ax.set_xlabel('Height')
ax.set_ylabel('Weight')
plt.show()
data[['Height','Weight']] = data[['Height','Weight']].apply(lambda x:
x/x.sum(), axis=1)
print("Percentages:")
print(data)
9. Test Case:
Data frame creation and present the
Numerical summaries.
Actual
a.
Result:
Percentages Table:
Height Weight
0 0.698024 0.301976
1 0.719353 0.280647
2 0.730263 0.269737
3 0.741119 0.258881
4 0.738832 0.261168
24
8. CREATE, INDEXING AND SLICING OF 1D, 2D AND 3D ARRAYS USING
NUMPY
10. Algorithm:
11. Coding:
# Numpy 1D and 2D Array manipulation
# Python program for
# Creation of Arrays
import numpy as np
25
print(arr)
# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3],
[3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
"(1, 2), (0, 1), (3, 0):\n", Index_arr)
Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
[0. 6. 2. 3.]
26