“This is the 21st day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
Basic usage of Anaconda
According to the previous article, I believe that everyone has installed Anaconda. Some friends left a message in the comment area hoping to publish a tutorial on how to use Anaconda. In fact, the basic use of Anaconda is very simple, and almost no tutorial is required.
After installing Anaconda on Windows, you can see the following components in all applications:
-
Anaconda Navigator: Graphical interface for managing toolkits and environments.
-
Anaconda Prompt: Command line interface for managing packages and environments.
-
Jupyter Terbook: An interactive Web-based computing environment that shows the processes of data analysis and generates easy-to-read documents.
-
Spyder: Python integrated development environment, layout similar to Matlab.
Our main use for our study is the third Jupyter terbook.
Here’s a quick primer on the common Anaconda command (although I don’t use it very often either).
- View the software version
python --version # Check the Python version
conda --version # Check out conda's version
Copy the code
- Add a mirror
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
Copy the code
- Update conda
conda upgrade --all
Copy the code
- View installed packages
conda list
conda install [package name] Install package in the default Python environment
Copy the code
It is recommended for beginners to only install Anaconda, which can save a lot of unnecessary trouble. The above is the basic use of Anaconda, welcome to add in the comments area.
Numpy index and slice
Correct the mistakes of the previous article:
The correct way to import
import numpy as np
Copy the code
Numpy indexes are similar to list indexes in Python. Here we focus on indexes/slicing of normal arrays and Boolean arrays.
Index/slice of a one-dimensional array
Indexes and slicing in one-dimensional arrays are the same as lists in Python, with indexes starting at 0 and slicing left closed and right open.
import numpy as np
ar = np.arange(20)
Print the fourth value of ar
print(ar[3])
Print the first four values of ar
print(ar[:4) > > >4
[0 1 2 3]
Copy the code
Indexing/slicing of multidimensional arrays
A two-dimensional array can be interpreted as two one-dimensional arrays stacked horizontally, so you just need to take the corresponding indexes respectively.
import numpy as np
ar = np.arange(16).reshape(4.4)
The index of a two-dimensional array follows the first after column.
# select the value of the second row and second column
print(ar[2] [2])
print(ar[2.2])
# 2d array slicing
Fetch the values of the first two rows
print(ar[:2])
Extract the values of the first two rows and the last two columns
print(ar[:2.2:) > > > [[0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
10
10
[[0 1 2 3]
[4 5 6 7]]
[[2 3]
[6 7]]
Copy the code
The index and slice values of a three-digit array are similar to the evolution of a two-dimensional array.
import numpy as np
ar = np.arange(12).reshape(3.2.2)
print(ar)
The 3d array index follows dimension first, row then column
print(ar[2] [0] [1])
print(ar[2.0.1])
# section
Get the number of the first column in the first row of the first array
print(ar[:1To:1To:1[[[]) > > >0 1]
[ 2 3]]
[[ 4 5]
[ 6 7]]
[[ 8 9]
[10 11[[[]]]0]]]
9
9
Copy the code
Boolean index and slice
The use of Boolean arrays is the focus of this article.
# Briefly show what a one-dimensional number of Boolean types looks like
i = np.array([True.False.True])
j = np.array([True.True.False.False])
print(i)
print(j)
>>>
[ True False True]
[ True True False False]
Copy the code
And what we often see is this:
ar = np.arange(12).reshape(3.4)
print(ar)
print(ar>5) > > > [[0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[False False False False]
[False False True True]
[ True True True True]]
Copy the code
When we need to screen out values greater than 3 in AR, we can use Boolean values for screening, as follows:
ar = np.arange(12).reshape(3.4)
print(ar[ar>3) > > > [4 5 6 7 8 9 10 11]
Copy the code
Numpy random number
Uniform distribution and normal distribution
Generate random numbers with uniform distribution and normal distribution
# numpy.random.rand() generates a 0-1 random float or n-dimensional float -- evenly distributed
a = np.random.rand()
b = np.random.rand(4.4)
print(a)
print(b)
>>>
0.5544023939180306
[[0.46387648 0.97345876 0.12059175 0.7565951 ]
[0.30192996 0.76633208 0.20107761 0.09315875]
[0.79347118 0.26714404 0.08628158 0.72510313]
[0.06606087 0.93260038 0.90268201 0.90941348]]
Copy the code
Generate random numbers in a positively distributed manner
# numpy.random.randn() generates a 0-1 random float or n-dimensional float -- normally distributed
a = np.random.randn()
b = np.random.randn(4.4)
print(a)
print(b)
>>>
0.26901442604096687
[[ 0.40261375 -0.23541184 0.96607489 -1.11253043]
[-0.31670703 0.05841136 -0.01862511 1.72597729]
[ 0.17052799 1.03537825 -0.94375417 1.32484928]
[ 0.132761 0.44950533 0.44131534 -0.11319535]]
Copy the code
Randn () and.rand() are not clear enough.
# Average distribution
# numpy.random.rand() generates a 0-1 random float or n-dimensional float -- evenly distributed
data1 = np.random.rand(500)
data2 = np.random.rand(500)
# Normal distribution
# numpy.random.randn() generates a float or n-dimensional float -- normally distributed
data3 = np.random.randn(500)
data4 = np.random.randn(500)
import matplotlib.pyplot as plt
% matplotlib inline
plt.scatter(data1,data2)
plt.scatter(data3,data4)
Copy the code
This is a random pattern:
This is the pattern of the normal distribution:
And you can see that there’s a big difference in imaging between a normal distribution and a random distribution, but of course I’m just going to give you a little bit more insight into.randn() and.rand(), and we’ll get to that later in the visualization.
Other uses of Numpy random numbers
# random integer
print(np.random.randint(2))
Generate random integers between 2 and 10
print((np.random.randint(2.10)))
Generate 10 integers between 0 and 10
print((np.random.randint(10,size=10)))
Create a 2-d array of 10 elements between 0 and 10
print(np.random.randint(10,size=(2.5)))
Generate a 2-d array of 10 elements between 10 and 50
print(np.random.randint(10.50,size=(2.5)))
Copy the code
Strengthen practice
-
Creates two normal distributed one-dimensional arrays with 10 elements
-
Create ar as required and change the value of ar[:2,:2] to a random number of [0,1)
- Create an array according to the requirements, through the index, ar[4], AR [:2,3:], ar[3][2]
- Create an array as required, filter out values greater than 5 and generate a new array