[Introduction to Python] Basic usage of the library scipy that you absolutely must know

Reference site: [Introduction to Python] Basic usage of the library scipy that you absolutely must know

[Introduction to Python] Basic usage of the library scipy that you absolutely must know

There are many useful libraries in Python, and scipy is one of them. scipy is a library for performing advanced scientific calculations. Numpy exists in a similar library, but scipy can perform array and matrix operations that can be done with numpy, as well as further calculations such as signal processing and statistics.

This time, I will explain the basic usage of scipy.

table of contents 1 Install scipy 1.1 Installing numpy 1.2 Installation of scipy 2 Use scipy 2.1 Integral 2.2 Matrix operation

Install scipy

numpy installation

You need numpy to use scipy, so let's install numpy first.

Install scipy

Then install scipy. scipy can be installed with the pip command.

pip install scipy

If you cannot install with pip, download Scipy from the following page and install the downloaded file with the pip command.

http://www.lfd.uci.edu/~gohlke/pythonlibs/

pip command

pip install the path to the file you just downloaded

There are several versions of scipy, so download the version that suits your python or OS. For example, the file "scipy-0.18.1-cp36-cp36m-win_amd64.whl" is for python3.6, 64-bit Windows.

Use scipy

After installing scipy, let's use it immediately. However, scipy has countless features, so I can't cover all of them. Here are just a few of the most commonly used features.

Integral

If you use scipy, you can easily integrate the function (definite integral). Use quad () in the scipy.integrate module for integration.

from scipy import integrate  #Must be required

Variable 1,Variable 2= integrate.quad(function,The beginning of the integration interval,End of integration interval)

quad () definitely integrates the given function at the interval [beginning of interval, end of interval] and returns two values. The result of integration is returned in variable 1, and the error in the integral calculation is returned in variable 2.

from scipy import integrate

#2x+Represents 5
def func(x):
    return 2*x + 5

result, err = integrate.quad(func, 0, 5)

print('Integral result:{0}\n error:{1}'.format(result, err))

Execution result

Integral result: 50.0 Error: 5.551115123125783e-13

Since it is difficult, I will omit it here, but you can perform advanced operations such as two-dimensional integration and differential equations as well as definite integrals.

Matrix operation

Matrix operations could be done with numpy, but scipy can be used for more advanced operations. For matrix operations, use scipy.linalg, which summarizes the functions for linear algebra.

rom scipy import linalg
import numpy as np  #use numpy

narray = np.array([[1, 2], [3, 4]])  #Representing a matrix with a numpy array
inv_narray = linalg.inv(narray)

print('Inverse matrix of narray:\n{}\n'.format(inv_narray))

result_det = linalg.det(inv_narray)

print('inv_Determinant of narray:{}\n'.format(result_det))

inv_narray_norm = linalg.norm(inv_narray)

print('inv_narray norm:{}'.format(inv_narray_norm))

Execution result

Inverse matrix of narray: [[-2. 1. ] [ 1.5 -0.5]] Determinant of inv_narray: -0.49999999999999967 norm of inv_narray: 2.73861278752583

In addition to this, scipy can easily perform advanced scientific calculations such as signal processing, image analysis, statistical processing, and Fourier transform. The number is huge and I can't explain all of them here, so if you want to use scipy more, please refer to the official documentation. Official Document

Recommended articles ➡ Reputation of Python books and reference booksMust-see for anyone who wants to learn Python! 5 Recommended Schools Summary

Recommended Posts

[Introduction to Python] Basic usage of the library scipy that you absolutely must know
[Introduction to Python] Basic usage of the library matplotlib
[Introduction to Python] Basic usage of lambda expressions
Python Note: When you want to know the attributes of an object
Introduction of "scikit-mobility", a library that allows you to easily analyze human flow data with Python (Part 1)
What you want to memorize with the basic "string manipulation" grammar of python
How much do you know the basics of Python?
Basic usage of Python f-string
I want to know the features of Python and pip
A Python script that allows you to check the status of the server from your browser
The story that the version of python 3.7.7 was not adapted to Heroku
Introduction to Python Numerical Library NumPy
How to know the internal structure of an object in Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
A story that struggled to handle the Python package of PocketSphinx
Explaining the mechanism of Linux that you do not know unexpectedly
When you want to use multiple versions of the same Python library (virtual environment using venv)
[Python] How to import the library
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] A program that rotates the contents of the list to the left
[AtCoder for beginners] A story about the amount of calculation that you want to know very roughly
Have Alexa run Python to give you a sense of the future
Basic summary of scraping with Requests that beginners can absolutely understand [Python]
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Introduction to Python] Thorough explanation of the character string type used in Python!
[Introduction to Python] What is the method of repeating with the continue statement?
I didn't know the basics of Python
Python Basic Course (at the end of 15)
[Introduction to Data Scientists] Basics of Python ♬
[Python3] Code that can be used when you want to change the extension of an image at once
python I don't know how to get the printer name that I usually use.
I tried using the Python library "pykakasi" that can convert kanji to romaji.
Free version of DataRobot! ?? Introduction to "PyCaret", a library that automates machine learning
Introduction to Quiz Statistics (1) -Mathematical analysis of question sentences to know the tendency of questions-
Visualize the timeline of the number of issues on GitHub assigned to you in Python
How to find the coefficient of the trendline that passes through the vertices in Python
When you can't uninstall the python library. Can't uninstall'hogehoge'. No files were found to uninstall.
[Introduction to Python] How to get the index of data with a for statement
How to use the C library in Python
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
Introduction to Python Let's prepare the development environment
[Introduction to Udemy Python 3 + Application] 19. Copy of list
Python & Machine Learning Study Memo ②: Introduction of Library
Introduction of Python Imaging Library (PIL) using HomeBrew
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
Introduction to Python with Atom (on the way)
This is the only basic review of Python ~ 3 ~
[Introduction to Algorithm] Find the shortest path [Python3]
From the introduction of pyethapp to the execution of contract
Software that saves the set character string to the clipboard when you click the [Python] button
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
Summary of versions of the Python standard library that are now server validated on https
[Introduction to Sound] Let's arrange the introduction to sounds of python and R ♬-Listen to the sound of the explosion of Nikkei 255-
Introduction of Python
Introduction of Python
Easy way to check the source of Python modules
python beginners tried to predict the number of criminals