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

pythonlibraries[1]

NumPy is a Python library designed for efficient array manipulation, offering speed improvements over traditional lists and providing functions for linear algebra and matrix operations. It includes the ndarray object for array creation and supports various dimensions, while SciPy builds on NumPy for scientific computations. Additionally, Matplotlib and Pandas are introduced as tools for data visualization and data manipulation, respectively.

Uploaded by

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

pythonlibraries[1]

NumPy is a Python library designed for efficient array manipulation, offering speed improvements over traditional lists and providing functions for linear algebra and matrix operations. It includes the ndarray object for array creation and supports various dimensions, while SciPy builds on NumPy for scientific computations. Additionally, Matplotlib and Pandas are introduced as tools for data visualization and data manipulation, respectively.

Uploaded by

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

NumPy is a Python library used for working with arrays.

It also has functions for working in domain of


linear algebra, fourier transform, and matrices.

In Python we have lists that serve the purpose


of arrays, but they are slow to process.

NumPy aims to provide an array object that is up


to 50x faster than traditional Python lists.

The array object in NumPy is called ndarray,


it provides a lot of supporting functions
that make working with ndarray very easy.

Arrays are very frequently used in data science,


where speed and resources are very important.

Installation of NumPy
If you have Python and PIP already installed on a system,
then installation of NumPy is very easy.

Install it using this command:

C:\Users\Your Name>pip install numpy

import numpy

arr = numpy.array([1, 2, 3, 4, 5])

print(arr)

Create a NumPy ndarray Object


NumPy is used to work with arrays. The array object in NumPy
is called ndarray.

We can create a NumPy ndarray object by using the array()


function.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])

print(arr)

print(type(arr))

*2-D Arrays
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr)

import numpy as np

a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)

* NumPy arrays have an attribute called shape that returns a


tuple
with each index having the number of corresponding elements.
import numpy as np

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

print(arr.shape)
SciPy stands for Scientific Python.
SciPy is a scientific computation library that uses NumPy
underneath.

It provides more utility functions for optimization, stats and


signal processing.

Like NumPy, SciPy is open source so we can use it freely.


A. Constants in SciPy
As SciPy is more focused on scientific implementations, it
provides many built-in scientific constants.

These constants can be helpful when you are working with Data
Science.

PI is an example of a scientific constant.


Print the constant value of PI:

from scipy import constants


print(constants.pi)
from scipy import constants

print(constants.kibi) #1024
print(constants.mebi) #1048576
print(constants.gibi) #1073741824
print(constants.tebi) #1099511627776
print(constants.pebi) #1125899906842624
print(constants.exbi) #1152921504606846976
print(constants.zebi) #1180591620717411303424
print(constants.yobi) #1208925819614629174706176
*Matplotlib is a low level graph plotting library in python that
serves as a visualization utility.

Matplotlib is open source and we can use it freely.


Pyplot
Most of the Matplotlib utilities lies under the pyplot
submodule,
and are usually imported under the plt alias:

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()

plotting x and y points


The plot() function is used to draw points (markers) in a
diagram.
By default, the plot() function draws a line from point to point.
The function takes parameters for specifying points in the
diagram.
Parameter 1 is an array containing the points on the x-axis.
Parameter 2 is an array containing the points on the y-axis.

If we need to plot a line from (1, 3) to (8, 10), we have to pass


two arrays [1, 8] and [3, 10] to the plot function.
Draw a line in a diagram from position (1, 3) to position (8, 10):

import matplotlib.pyplot as plt


import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()

Draw a line in a diagram from position (1, 3) to (2, 8) then to (6,


1) and finally to position (8, 10):
import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 2, 6, 8])


ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

*import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle = 'dotted')


plt.show()
*import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid()

plt.show()

*Creating Bars
With Pyplot, you can use the bar() function to draw bar graphs:

ExampleGet your own Python Server


Draw 4 bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x,y)
plt.show()

*Creating Pie Charts


With Pyplot, you can use the pie() function to draw pie charts:

ExampleGet your own Python Server


A simple pie chart:

import matplotlib.pyplot as plt


import numpy as np
y = np.array([35, 25, 25, 15])

plt.pie(y)
plt.show()

* Pandas is a Python library used for working with data sets.

It has functions for analyzing, cleaning, exploring, and


manipulating data.

The name "Pandas" has a reference to both "Panel Data", and


"Python Data Analysis" and was created by Wes McKinney in
2008.
import pandas

mydataset = {
'cars': ["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}
myvar = pandas.DataFrame(mydataset)
print(myvar)

A Pandas Series is like a column in a table.

It is a one-dimensional array holding data of any type.


Create a simple Pandas Series from a list:

import pandas as pd

a = [1, 7, 2]

myvar = pd.Series(a)
print(myvar)

Create a simple Pandas Series from a dictionary:

import pandas as pd

calories = {"day1": 420, "day2": 380, "day3": 390}

myvar = pd.Series(calories)

print(myvar)

DataFrames
Data sets in Pandas are usually multi-dimensional tables, called
DataFrames.

Series is like a column, a DataFrame is the whole table.

Example
Create a DataFrame from two Series:

import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

myvar = pd.DataFrame(data)

print(myvar)

What is a DataFrame?
A Pandas DataFrame is a 2 dimensional data structure, like a 2
dimensional array, or a table with rows and columns.

ExampleGet your own Python Server


Create a simple Pandas DataFrame:
import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

#load data into a DataFrame object:


df = pd.DataFrame(data)

print(df)

Read CSV Files


A simple way to store big data sets is to use CSV files (comma
separated files).

CSV files contains plain text and is a well know format that can
be read by everyone including Pandas.
In our examples we will be using a CSV file called 'data.csv'.
import pandas as pd

df = pd.read_csv('data.csv')

print(df.to_string())

use to_string() to print the entire DataFrame.

Read JSON
Big data sets are often stored, or extracted as JSON.

JSON is plain text, but has the format of an object, and is well
known in the world of programming, including Pandas.
Load the JSON file into a DataFrame:

import pandas as pd

df = pd.read_json('data.json')
print(df.to_string())

Dictionary as JSON
JSON = Python Dictionary

JSON objects have the same format as Python dictionaries.

If your JSON code is not in a file, but in a Python Dictionary, you


can load it into a DataFrame directly:
import pandas as pd

data = {
"Duration":{
"0":60,
"1":60,
"2":60,
"3":45,
"4":45,
"5":60
},
"Pulse":{
"0":110,
"1":117,
"2":103,
"3":109,
"4":117,
"5":102
},
"Maxpulse":{
"0":130,
"1":145,
"2":135,
"3":175,
"4":148,
"5":127
},
"Calories":{
"0":409,
"1":479,
"2":340,
"3":282,
"4":406,
"5":300
}
}

df = pd.DataFrame(data)

print(df)

You might also like