Python Common Packages & Modules
- Package is namespace which contain multiple packages and modules itself.
- Module is a files which consisting of Python codes.
- Functions, classes and variables can be defined and implemented in a module.
To Install new package:
1 | $ pip install <package name> |
Display installed packages:
1 | $ pip list |
Numpy
NumPy is a Fundamental package for scientific computing with Python.
How to import
1 | import numpy as np |
To declare an array
Array is a data type provided by NumPy support 2D, 3D or higher dimensional arrays.
some examples
1 | x = np.array([0,1,2]) |
Accessing shape of array
1 | a = np.array( ([1,2,3],[4,5,6],[7,8,9]) ) |
Accessing elements of array
Array vs List
Array has a different Arithmetic operations with list.
Check this example you will know how:
1 | a = [1,3,4,7] |
Different ways to create an array
numpy.zeros & numpy.ones
You can use numpy.zeros
or numpy.ones
to create an array filled with 0 or 1 with the specified shape.
numpy.ones(shape, dtype=None, order='C')
numpy.zeros(shape, dtype=None, order='C')
Check these example you will know how:
1 | a = np.zeros((2,3)) |
[[0. 0. 0.]
[0. 0. 0.]]
1 | b = np.ones((3,3)) |
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
numpy.arange
Generate an array with values within a half-open interval [start, stop)
numpy.arange([start=0,]stop,[step=1,]dtype=None)
1 | a = np.arange(3) |
numpy.linspace
Create an array with evenly spaced numbers over a specified interval [start, stop]
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
1 | a = np.linspace(2.0, 3.0, 5) |
1 | a = np.linspace((0, 0, 0), (2, 4, 6), 3, axis=0) |
[[0. 0. 0.]
[1. 2. 3.]
[2. 4. 6.]]
1 | b = np.linspace((0, 0, 0), (2, 4, 6), 3, axis=1) |
[[0. 1. 2.]
[0. 2. 4.]
[0. 3. 6.]]
numpy.logspace
Create an array with numbers spaced evenly on a log scale
numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)
1 | a = np.logspace(2.0, 3.0, 4) |
Returns the indices of the maximum values along an axis
https://numpy.org/doc/stable/reference/generated/numpy.argmax.html
np.argmax(a, axis=None, out=None)
Example:
1 | a = np.arange(6).reshape(2,3) + 10 |
More about Reshape
What does -1 mean in numpy reshape?
1 | z = np.array([[1, 2, 3, 4], |
Mathematical functions
https://numpy.org/doc/stable/reference/routines.math.html
numpy.ndarray
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy-ndarray
Polynominals
Import numpy.polynomial
1 | from numpy.polynomial import polynomial as P |
Basics
Polynomial(coef[, domain, window])
1 | f = P.Polynomial( [6,-7,0,1] ) # ^The above equation |
polyval(x, c[, tensor])
- Evaluation the value of a polynomial at specific value of 𝑥.polyroots(c)
- compute roots of a polynomialpolyfromroots
- Generate a polynomial by the given roots
Fitting
polyfit(x,y, deg[, rcond, full, w])
- Least squares fit of a polynomial to data
Polynomial arithmetic
https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.polynomials.polynomial.html#algebra
polyadd(c1, c2)
- Add one polynomial to another.polysub(c1, c2)
- Subtract one polynomial from another.polymul(c1, c2)
- Multiply one polynomial by another.polydiv(c1, c2)
- Divide one polynomial by another.
Matplotlib
Matplotlib can be used to perform various 2D/3D plots with python.
How to import
1 | import matplotlib.pyplot as plt |
Plot an equation
1 | y = [1, 4, 3, 2, 8, 5, 7] |
Equation with labels
1 | y = [1, 4, 3, 2, 8, 5, 7] |
Plot multiple equations
1 | r = np.arange(0,2*np.pi,0.1) |
Latex support
Matplotlib supports special symbols for displaying equations and other mathematical expressions.
1 | t=np.arange(0,1000) |
Subplot
subplot(nrows, ncols, index, **kwargs)
nrows
- Number of rowsncols
- Number of columnsindex
- Index of the graph
1 | x=np.linspace(0,1) |
Axis configuration
plt.xticks(ticks, labels)
plt.yticks(ticks, labels)
Change the scale of x axis
1 | plt.plot(x,np.sin(x)) |
See an example combining subplot.
1 | x=np.linspace(0,2,200) |
Plot a bar chart
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html#matplotlib-pyplot-bar
bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
x
- The x coordinates of the bars.height
- The height of the bars.width
- The width of the bars.bottom
- The y coordinate of the bars bases.align
- Alignment of the bars to the x coordinates.'center'
or'edge'
Normal Example
1 | cata = [20, 35, 30, 35, 27] |
Example with bottom
1 | cata = [20, 35, 30, 35, 27] |
Plot a pie chart
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html#matplotlib-pyplot-pie
pie(x, explode=None, labels=None, colors=None, autopct=None)
x
- The wedge sizesexplode
- An array which specifies the fraction of the radius with which to offset each wedge.labels
- A sequence of strings providing the labels for each wedge.autopct
- A string or function used to label the wedges with their numeric value.
Example
1 | count = [4, 5, 6, 3, 4] |
Example with explode
1 | count = [4, 5, 6, 3, 4] |
Plot a histogram
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html#matplotlib-pyplot-hist
hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)
x
- Data to distribute among bins, specified as a vector.bins
- Number of bins of the histogram. Default bins = 10.range
- The lower and upper range of the bins.cumulative
- If True, then a histogram is computed where each bin gives the counts in that bin plus all bins for smaller values.rwidth
- The relative width of the bars as a fraction of the bin width.
Example
1 | x=np.random.randn(1000) # Generate 1000 random numbers |
More Example
1 | x=np.random.randn(1000) # Generate 1000 random numbers |
Pandas
- A fast and efficient DataFrame object for data manipulation;
- Tools for reading and writing data of different formats: CSV and text files, Microsoft Excel, SQL databases, and the fast HDF5 format;
- Intelligent data alignment and integrated handling of missing data;
- Flexible reshaping and pivoting of data sets;
- Time series-functionality;
- Python with pandas is in use in a wide variety of academic and commercial domains, including Finance, Neuroscience, Economics, Statistics, Advertising, Web Analytics, and more.
pandas deals with the following data structures:
- Series - 1D labeled array
- DataFrame - General 2D labeled, size-mutable tabular structure
How to import
1 | import pandas as pd |
pandas.Series
Not so important so I skipped it
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html#pandas-series
pandas.DataFrame
In Real life programming, we often use dataframe.
Example:
1 | staff = {'Name':['Alex', 'Bob', 'Calvin', 'David'], |
![https://i.imgur.com/xmVed5m.png]
Selection and Indexing
Drop element
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html
df.drop(self[, labels, axis, index, ...])
1 | df.drop([0]) # Drop first row by index |
Convert pandas DataFrame to NumPy array
1 | array = df.values |
Reading Data files
read_csv(filepath_or_buffer, pathlib.Path, ...)
- Read csvread_excel(io[, sheet_name, header, names, ...])
- Read Excelread_json([path_or_buf, orient, typ, dtype, ...])
- Convert a JSON string to pandas object.read_sql_table(table_name, con[, schema, ...])
- Read SQL database table into a DataFrame.read_sql_query(sql, con[, index_col, ...])
- Read SQL query into a DataFrame.read_sql(sql, con[, index_col, ...])
- Read SQL query or database table into a DataFrame.
Write to data files
https://pandas.pydata.org/pandas-docs/stable/reference/frame.html#serialization-io-conversion