Monday, December 25, 2017

Python Packages

However, adding all functions and methods that have been written up to now to the same Python distribution would be a mess. There would be tons and tons of code in there that you'll never use. Also, maintaining all of this code would be a real pain.

This is where packages come into play. You can think of package as a directory of Python scripts.
Each such script is a so-called module. These modules specify functions, methods and new Python types aimed at solving particular problems. There are thousands of Python packages available from the internet. Among them are packages for data science:
  • ·         there's numpy to efficiently work with arrays,
  • ·         matplotlib for data visualization,
  • ·         scikit-learn for machine learning, and many others.
What are Packages

If you want to install packages on your own system, you'll want to use pip, a package maintenance system for Python.
If you go to this URL, you can download the file `get-pip.py`. Next, you go to the terminal, and execute `python3 get-pip.py`. Now you can use pip to actually install a Python package of your choosing. 

Suppose we want to install the numpy package, you type `pip3 install numpy`. You have to use the commands python3 and pip3 here to tell our system that we're working with Python version 3.


Now that the package is installed, you can actually start using it in one of your Python scripts. Before you can do this, you should import the package, or a specific module of the package. You can do this with the `import` statement.

To import the entire numpy package, you can do import numpy, like this. A commonly used function in Numpy is `array()`. It takes a list as input. Simply calling the array function like this will generate an error.
To refer to the array function from the numpy package, you'll need this:
Now, instead of numpy.array(), you'll have to use np.array() to use Numpy's array function:
There are cases in which you only need one specific function of a package. Python allows you to make this explicit in your code.

Suppose that we only want to use the array() function from the Numpy package. Instead of doing import numpy, you can instead do from numpy import array, like this:

0 comments:

Post a Comment

Contact

Talk to us (+91-9738925800)