Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Numpy_and_matplotlib_practical

The document provides examples of using NumPy and Matplotlib in Python for data manipulation and visualization. It includes creating arrays, performing statistical calculations, and generating various types of plots such as line plots, bar charts, histograms, and scatter plots. Additionally, it highlights the use of functions like mean, median, and mode from NumPy and SciPy.

Uploaded by

uwugauri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Numpy_and_matplotlib_practical

The document provides examples of using NumPy and Matplotlib in Python for data manipulation and visualization. It includes creating arrays, performing statistical calculations, and generating various types of plots such as line plots, bar charts, histograms, and scatter plots. Additionally, it highlights the use of functions like mean, median, and mode from NumPy and SciPy.

Uploaded by

uwugauri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

11/27/24, 12:15 PM X_E_NUMPY_MATPLOTLIB

In [5]: #alias

import numpy as np

print (np.__version__)

1.23.5

In [21]: #CREATING AN ARRAY USING NUMPY


stu = np.array([2,4,6,7])

print(stu)

print(stu + 6)

print(stu * 2)

print(stu ** 3)

[2 4 6 7]
[ 8 10 12 13]
[ 4 8 12 14]
[ 8 64 216 343]

In [4]: a = np.array([1,9,8,3]) #1d dimension

print(a)

print(a+5)

print(a ** 2)

[1 9 8 3]
[ 6 14 13 8]
[ 1 81 64 9]

In [3]: import numpy as npy

# creating a 2-d array


arr1 = npy.array([[1, 3, 5,7 ], [2, 4, 6, 8]])
print(arr1.shape)
print(arr1.dtype)

(2, 4)
int32

In [5]: print(a.shape)

(4,)

In [26]: arr1 = npy.array([1.2, 4.5, 7.8])

print(arr1.dtype)

float64

In [8]: a = np.array([1,9,8,3,100])

print(a.max())

file:///C:/Users/deep2/OneDrive/Desktop/X_E_NUMPY_MATPLOTLIB.html 1/8
11/27/24, 12:15 PM X_E_NUMPY_MATPLOTLIB
100

In [9]: print(a.min())

In [10]: print(a.sum())

121

In [34]: #STATISTICAL - MEAN, MEDIAN, MODE

speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]

avg_speed = np.mean(speed)

print(avg_speed)

89.76923076923077

In [11]: med_speed = np.median(speed)

print(med_speed)

87.0

In [15]: from scipy import stats

speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]

mode = stats.mode(speed)

C:\Users\deep2\AppData\Local\Temp\ipykernel_8704\126877046.py:5: FutureWarning: Unlik


e other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode`
typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will chang
e: the default value of `keepdims` will become False, the `axis` over which the stati
stic is taken will be eliminated, and the value None will no longer be accepted. Set
`keepdims` to True or False to avoid this warning.
mode = stats.mode(speed)

In [11]: lst = [1,2,3,4,5]

arr = np.array(lst)

print(arr)

print(arr + 4)
print(lst +5)

[1 2 3 4 5]
[5 6 7 8 9]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[11], line 8
5 print(arr)
7 print(arr + 4)
----> 8 print(lst +5)

TypeError: can only concatenate list (not "int") to list

file:///C:/Users/deep2/OneDrive/Desktop/X_E_NUMPY_MATPLOTLIB.html 2/8
11/27/24, 12:15 PM X_E_NUMPY_MATPLOTLIB

MATPLOTLIB - VISUALIZATION LIBRAY OF


PYTHON
In [38]: from matplotlib import pyplot as plt

# A line plot is a way to display data along a number line.

flavors = ['orange', 'vanilla', 'chocolate', 'mint', 'mango']


numbers = [5, 2, 9, 4, 7]
num = [15, 12, 10, 9, 7]

plt.plot(num,numbers)
plt.show()

In [39]: from matplotlib import pyplot as plt

# A bar chart or bar graph is a chart or graph that presents categorical data
#with rectangular bars with heights or lengths proportional to the values that
#they represent.

flavors = ['orange', 'vanilla', 'chocolate', 'mint', 'mango']


numbers = [5, 2, 9, 4, 7]
#num = [15, 12, 10, 9, 7]

plt.bar(num,numbers)
plt.show()

file:///C:/Users/deep2/OneDrive/Desktop/X_E_NUMPY_MATPLOTLIB.html 3/8
11/27/24, 12:15 PM X_E_NUMPY_MATPLOTLIB

In [40]: #Plotting a Histogram


#A histogram is a graphical representation of a grouped frequency
#distribution with continuous classes

flavors = ['orange', 'vanilla', 'chocolate', 'mint', 'mango']


numbers = [5, 2, 9, 4, 7]
#num = [15, 12, 10, 9, 7]

plt.hist(numbers)
plt.show()

file:///C:/Users/deep2/OneDrive/Desktop/X_E_NUMPY_MATPLOTLIB.html 4/8
11/27/24, 12:15 PM X_E_NUMPY_MATPLOTLIB

In [41]: import matplotlib.pyplot as plt

# create data
data = [32, 96, 45, 67, 76, 28, 79, 62, 43, 81, 70,
61, 95, 44, 60, 69, 71, 23, 69, 54, 76, 67,
82, 97, 26, 34, 18, 16, 59, 88, 29, 30, 66,
23, 65, 72, 20, 78, 49, 73, 62, 87, 37, 68,
81, 80, 77, 92, 81, 52, 43, 68, 71, 86]

# create histogram
plt.hist(data)

# display histogram
plt.show()

file:///C:/Users/deep2/OneDrive/Desktop/X_E_NUMPY_MATPLOTLIB.html 5/8
11/27/24, 12:15 PM X_E_NUMPY_MATPLOTLIB

In [42]: # A scatter plot is a type of plot or mathematical diagram using Cartesian


#coordinates to display values for typically two variables for a set of data.

In [43]: import matplotlib.pyplot as plt

x =[5, 7, 8, 7, 2, 17, 2, 9,
4, 11, 12, 9, 6]

y =[99, 86, 87, 88, 100, 86,


103, 87, 94, 78, 77, 85, 86]

plt.scatter(x, y, c ="blue")

# To show the plot


plt.show()

file:///C:/Users/deep2/OneDrive/Desktop/X_E_NUMPY_MATPLOTLIB.html 6/8
11/27/24, 12:15 PM X_E_NUMPY_MATPLOTLIB

In [46]: import matplotlib.pyplot as plt

# dataset-1
x1 = [89, 43, 36, 36, 95, 10,
66, 34, 38, 20]

y1 = [21, 46, 3, 35, 67, 95,


53, 72, 58, 10]

# dataset2
x2 = [26, 29, 48, 64, 6, 5,
36, 66, 72, 40]

y2 = [26, 34, 90, 33, 38,


20, 56, 2, 47, 15]

plt.scatter(x1, y1, c ="pink",


linewidths = 2,
marker ="s",
edgecolor ="green",
s = 50)

plt.scatter(x2, y2, c ="yellow",


linewidths = 2,
marker ="^",
edgecolor ="pink",
s = 200)

plt.xlabel("deepshikha")
plt.ylabel("Y-axis")
plt.show()

file:///C:/Users/deep2/OneDrive/Desktop/X_E_NUMPY_MATPLOTLIB.html 7/8
11/27/24, 12:15 PM X_E_NUMPY_MATPLOTLIB

In [ ]:

file:///C:/Users/deep2/OneDrive/Desktop/X_E_NUMPY_MATPLOTLIB.html 8/8

You might also like