[Python of Hikari-] Chapter 08-04 Module (Installation of external library)

[Python] Chapter 08-04 Installing External Libraries

In the previous section, we described how to import and use the standard library that comes automatically when you install Python. As I explained there, some libraries are not installed by default, and I called them ** external libraries **. This time I would like to explain how to install this external library.

PyPI (Python Package Index) and pip

External libraries are libraries developed by engineers around the world. These libraries are registered in the following repository called ** PyPI **. https://pypi.org/

It's hard to find the library you need because PyPI contains information about libraries around the world. Therefore, ** pip (package management system) ** is used when installing the library (package).

This pip allows you to install packages, but pip also has the following features:

--List management of already installed packages is possible --If a package is ** dependent ** on another package, that dependent package can be installed with it --Search for installable packages

Of these, ** installing while checking package dependencies ** is an important feature of pip. For example, there is a package called ** pandas ** used for AI data analysis, but if you install this, a package called ** numpy ** that automatically calculates matrices will also be installed. In this way, it is an important function of pip to install the necessary packages when installing a certain package.

Install the library using the command pip. I will not do it this time, but </ font> You can actually install it from the following location with PyCharm by entering a command.

Click Terminal near PyCharm's ** Python Console ** image.png

The following parts, image.png You can install the library ** XXXX ** by typing the following command from.

(venv) C:\Users\***\Desktop\python>pip install XXXX

How to install external libraries

In the above, I introduced how to install an external library by command. There is another way to install it in PyCharm, so I will explain how to do it. From now on, I will use this method. Let's actually install a package for data analysis called ** pandas **.

Select [File] → [Settings]. image.png

Here you will find the currently installed packages. Press the ** "+" ** button on the right side. image.png

Search for the package name you want to install. This time, enter "pandas" in the red frame below and it will be searched, so click it and click ** "Install Package" ** of the button below. image.png

When the installation is completed, the following red frame will be displayed, so close it with ×. image.png

Make sure "Pandas" is installed. As mentioned above, packages such as "numpy" are also installed, but you can see that they are installed together because they depend on Pandas.

  • Numpy is also a package that is very often used in vector and matrix operations performed in AI programming. image.png

Importing and using external libraries (installing matplotlib)

Earlier, I installed using the example of pandas. Now let's install ** matplotlib **. Please refer to the previous procedure for the procedure. image.png

The external library used this time uses this ** matplotlib ** package. matplotlib is a library for graphing and visualizing data used in the field of AI. Like numpy and pandas earlier, this is also very often used in the field of AI.

Let's actually create a program. Create a file with the file name samp08-04-01.py </ font> in chap08 </ font>, and the following code Please write. The program described this time will draw a graph showing the change in weight.

.samp08-04-01.py


import matplotlib.pyplot as plt

data = [79.2, 79.1, 78.7, 79.4, 78.2, 78.5, 78.1, 78.3, 78.0, 77.4, 77.1, 76.9, 76.5, 76.3, 75.2]


plt.plot(data)
plt.title('Change in weight')
plt.show()

[Execution result] </ font> image.png

This time, since matplotlib is used, it is output in graph format instead of output on the console screen.

First, ** import ** imports a module called ** pyplot ** in a package called ** matplotlib **. The name is now aliased as ** plt **.

The weight data ** data ** in list format is drawn by ** plt.plot (data) **, and the title of the graph is "Change in weight". Finally, ** plt.show () ** displays the graph. (If there is no show function, the graph will not be displayed.)

Note that matplotlib has more detailed functions and modules, so please check it out. Please refer to the AI-related books for details on matplotlib.

Finally

This time, I installed numpy, pandas, and matplotlib, and tried to draw a graph using the example of matplotlib. There are various functions and modules in this package as well, and we will create them while examining them. As I mentioned somewhere before, the programs I write in practice are researched. This time, let's check the library called matplotlib and execute various processes.

Return to [Table of Contents Link]

Recommended Posts