1. Install Anaconda and create a virtual environment
download link: Official website link
During the installation process, it is recommended not to check the option to use python 3.9 as the default version.
1. Open Anaconda Prompt (in the start menu)
Create a virtual environment under the default path. The environment I created here is named zzy (the environment name can be arbitrary)
conda create -n zzy python=3.9 # Here the default python is set to python 3.9
After that, a prompt pops up, enter y to install.
2. Check whether the environment is installed successfully
conda info --envs
You can see the two environments of base and zzy
3. Activate the virtual environment zzy: activate zzy
4. Use the following command to switch to the specified virtual environment:
conda activate virtual environment name
5. Exit the virtual environment with the following command:
conda deactivate
2. Install jupyter and numpy in a virtual environment
Install in the virtual environment created above:
pip3 install jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple
Type the following statement to start jupyter:
jupyter-notebook
Click to open the cell
No error is reported, indicating that numpy is installed.
3. Basic exercises of numpy
1. Create a one-dimensional ndarray object with a length of 10 and all zeros, and then make the fifth element equal to 1
import numpy as np s1=np.zeros(shape=10) s1
s1[4]=1 s1
- Effect:
2. Create an ndarray object with elements from 10 to 49
The following three examples are: random elements, elements of arithmetic progression (step=10), default arithmetic progression
np.random.randint(10,50,size=10)
np.linspace(10,49,10)
a=np.arange(10,50) a
- Effect:
3. Reverse the position of all elements in question 2
Invert all elements in the last object a of the second question
a[::-1]
- Effect:
4. Use np.random.random to create a 10*10 ndarray object, and print out the largest and smallest elements
a4=np.random.random(size=(10,10)) a4
- Effect:
zmin,zmax=a4.min(),a4.max() zmin,zmax
- Effect:
5. Create a 10*10 ndarray object, and the matrix boundary is all 1, and the inside is all 0
- method one:
nd=np.zeros(shape=(10,10),dtype=np.int8) nd[[0,9]]=1 nd[:,[0,9]]=1 nd
- Effect:
- Method Two:
a5=np.ones((10,10)) a5[1:-1,1:-1]=0 a5
- Effect:
6. Create a 5*5 matrix with each row ranging from 0 to 4
l=[0,1,2,3,4] nd=np.array(l*5) nd.reshape(5,5)
- Effect:
7. Create an arithmetic sequence of length 12 in the range (0,1)
np.linspace(0,1,12)
- Effect:
8. Create a random array of length 10 and sort it
a8=np.random.random(10) np.sort(a8) a8
- Effect:
9. Create a random array of length 10 and replace the maximum value with -100
nd=np.random.randint(0,10,size=10) display(nd) index_max=nd.argmax()
nd[index_max]
all_index_max=np.argwhere(nd==nd[index_max]).reshape(-1) all_index_max
nd[all_index_max]=-100 nd
- Effect:
10. How to sort a 5*5 matrix according to the third column
n10=np.random.randint(0,100,size=(5,5)) n10
n10[:,2]
np.argsort(n10[:,2])
n10[np.argsort(n10[:,2])]
- Effect:
4. Examples of pandas and matplotlib libraries
1.pandas
1. Create a Series object for a geographic location data
A Series is an object similar to a one-dimensional array. It consists of a data and a set of data labels (ie, indexes) associated with it. The data can be any NumPy data type (integer, string, floating point, Python object wait).
Enter the following command in the cell to download the pandas library:
!pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
- code:
import pandas as pd print('------- list creation Series --------') s1=pd.Series([1,1,1,1,1]) print(s1) print('------- dictionary creation Series --------') s2=pd.Series({'Longitude':39,'Latitude':116,'Temperature':23}) print('First value in s2:',s2['Longitude']) print('------- use sequence as Series index --------') s3=pd.Series([3.4,0.8,2.1,0.3,1.5],range(5,10)) print('First value in s3:',s3[5])
- Effect:
2.DataFrame object
DataFrame is a tabular data structure that contains a set of ordered arrays. The column index corresponds to the field name of the table, the row index corresponds to the row number of the table, and the value is a two-dimensional array. Each column represents an independent attribute, and the data types of each column can be different. The following simply creates a DataFrame object:
- code:
import pandas as pd dict1={'col':[1,2,5,7],'col2':['a','b','c','d']} df=pd.DataFrame(dict1) df
- Effect:
3.Pandas variance
- code:
import numpy as np import pandas as pd a=np.arange(0,60,5) a=a.reshape(3,4) df=pd.DataFrame(a) print(df) print('-------------------') print(df.std())
- Effect:
2.matplotlib
Matplotlib is a basic 2D drawing library for Python. It provides a lot of parameters through which you can control styles, attributes, etc., and generate cross-platform publication-quality images. Using Matplotlib, complex work can be made easy, and histograms, bar charts, scatter plots, curve charts, etc. can be generated. Matplotlib can be used in Python scripts, Python, IPython, Jupyter Notebook, Web application servers, and more.
Enter the following command in the cell to download the matplotlib library:
!pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
1. Draw a simple plot table
- code:
import matplotlib.pyplot as plt fig=plt.figure() ax1=fig.add_subplot(2,2,1) ax2=fig.add_subplot(2,2,2)
- Effect:
2. Draw multiple simple lines
- code:
import matplotlib.pyplot as plt import numpy as np a=np.arange(10) plt.xlabel('x') plt.ylabel('y') plt.plot(a,a*1.5,a,a*2.5,a,a*3.5,a,a*4.5) plt.legend(['1.5x','2.5x','3.5x','4.5x']) plt.title('simple lines') plt.show()
- Effect:
3. Draw the sin(x) function image
- code:
import matplotlib.pyplot as plt import numpy as np x=np.linspace(-10,10,100) #List 100 data points y=np.sin(x) #Calculate the corresponding y plt.plot(x,y,marker="o")
- Effect:
5. What is the Turing Test
The Turing test refers to the situation where the tester is separated from the testee (a person and a machine), and randomly asks the testee questions through some devices (such as a keyboard). If, over many tests, the machine got the average participant to make more than 30 percent false positives, the machine passed the test and was deemed human-intelligent.
6. Summary
This is the first time to practice creating a virtual environment in the Anaconda environment installed in windows, install jupyter and numpy in the virtual environment, and run jupyter. Completed the basic exercises of jupyter, and completed the examples of the three libraries of numpy, pandas, and matplotlib. Generally speaking, the installation process is very simple, but some algorithms need to be pondered.
references: https://blog.csdn.net/qq_52215423/article/details/129387783?spm=1001.2014.3001.5502