Install Anaconda3 under Windows and use Jupyter for basic exercises

Table of contents

foreword

This blog is mainly to practice creating a virtual environment in the Anaconda environment under windows, install jupyter and numpy in the virtual environment, and run jupyter, complete no less than 10 basic exercises of numpy, and be familiar with matrix operations. Learn the basics of python, use Jupyter to complete the examples of the three libraries of numpy, pandas, and matplotlib, and understand the "Turing test".

1. Jupyter Notebook installation

1.1 Installation Prerequisites

The prerequisite for installing Jupyter Notebook is that Python (version 3.3 and above, or version 2.7) needs to be installed.

1.2 Install using Anaconda

I solved the Jupyter Notebook installation problem by installing Anaconda, because Anaconda has automatically installed Jupter Notebook and other tools for you, as well as more than 180 scientific packages and their dependencies in python. Very convenient. For detailed installation steps, see: Anaconda introduction, installation and usage tutorial
After the installation is complete, you can open Jupyter Notebook, and a webpage will pop up automatically. Just click Python3 to start practicing.
insert image description here

Two, practice

2.1, 10 basic exercises of numpy

2.1.1 Create a one-dimensional ndarray object with a length of 10 and all zeros, and then make the fifth element equal to 1

insert image description here

2.1.2 Create an ndarray object with elements from 10 to 49

insert image description here

2.1.3 Reverse the position of all elements in question 2

insert image description here

2.1.4 Use np.random.random to create a 10*10 ndarray object, and print out the largest and smallest elements

insert image description here

2.1.5 Create a 10*10 ndarray object, and the matrix boundary is all 1, and the inside is all 0

insert image description here

2.1.6 Create a 5*5 matrix with each row ranging from 0 to 4

insert image description here

2.1.7 Create an arithmetic sequence of length 12 in the range (0,1)

insert image description here

2.1.8 Create a random array of length 10 and sort it

insert image description here

2.1.9 Create a random array of length 10 and replace the maximum value with -100

insert image description here

2.1.10 How to sort a 5*5 matrix according to the third column

insert image description here

2.2. python review

2.2.1 Define Fibonacci Sequence Recommended Events

code:

import random
 
def fin_loop(n):
    listNum = []
    a,b = 0,1
    for i in range(n):
        a,b = b,a+b
        listNum.append(a)
    return listNum
 
if __name__ == "__main__":
    listPlan = ['吃零食','学习','学习','学习','看电影','学习','旅游','睡觉','学习']
    listNum = fin_loop(6)
    varIdx = random.randint(0,5)
    varRandom = listNum[varIdx]
    print("今日计划:",listPlan[varRandom])

Effect
insert image description here

2.2.2 Numeric types

code:

i=3
print(id(i))
i+=1
print(id(i))

Effect:
insert image description here
After modifying the variable value, the address value is different.

2.2.3 String access (fence)

code:

str='Picture'
print(str[1:3])
print(str[-3:-1])
print(str[3:-1])
print(str[-6:7])
print(str[2:])
print(str*2)
print(str+"TEST")

Effect:
insert image description here

2.2.4 String assignment

code:

word='Python'
print(word[0],word[5])
print(word[-1],word[-6])
word[0]='Q'

Effect
insert image description here

2.2.5 List (list) type access

code:

list=['a',56,1.13,'HelloWorld',[7,8,9]]
print(list)
print(list[4])
print(list[-2:5])
print(list[2:])

Effect:
insert image description here

2.2.6 Modification of list elements

code:

a=[1,2,3,4,5,6]
a[0]=9
print(a)
a.append(7)
print(a)
a[2:5]=[]
print(a)
a.pop(2)
print(a)

Effect:
insert image description here

2.2.7Tuple (tuple) access

the code

tuple=('Spiderman',2017,33.4,'Homecoming',14)
tinytuple=(16,'Marvel')
print(tuple)
print(tuple[0])
print(tuple[3:4])
print(tuple+tinytuple)

Effect:
insert image description here

2.2.8Tuple (tuple) modification

code:

tuple=([16,'Marvel'],'Spiderman',2017,33.4,'Homecoming',14)
print(tuple[0])
tuple[0][0]='Marvel'
tuple[0][1]='16'
print(tuple)

Effect:
insert image description here

2.2.9 Use of dictionaries

code:

# 字典基本操作
dict = {
    
    'Name':'jiangjiang','Class':'人工智能与机器学习','Age':10}
# 字典的访问
print("Name:",dict['Name'])
print(dict)
# 添加 add
dict['Gender'] = 'male'
print(dict)
# 修改 update
dict.update({
    
    "No":"001"})
dict['Age'] ={
    
    8,9,10}
print(dict)
# 也可以使用 update 方法添加/修改多个数据
dict.update({
    
    'Gender':'Man','Id':'001'})
print(dict)
# 删除元素
del dict['Gender']
print(dict)
dict.clear()
print(dict)

Effect:
insert image description here

2.2.10Set (collection) type

code:

# 集合基本操作
# 创建集合
var = set()
print(var,type(var))
var = {
    
    'LiLei','HanMeiMei','ZhangHua','LiLei','LiLei'}
print(var,type(var))     # 从输出中可以看出,集合中没有重复元素,与数学集合一致
# 集合成员检测
result = 'LiLei' in var
print(result)
result = 'jiangjiang' in var
print(result)
# 增删改查
var.add('jiangjiang')
print(var)
var.update('pl')        # 该方法首选拆分元素,然后一次添加
print(var)
var.remove('LiLei')     # 这里会删除所有一致元素
print(var)
# 集合的遍历
for item in var:       # 方法1
    print(item)
for item in enumerate(var): # 方法2
    print(item)
# 交集、并集、差集
var1 = set()
var1 = {
    
    'jiangjiang','002','003'}
jiaoji = var&var1      # 交集
print(jiaoji)
bingji = var|var1      # 并集
print(bingji)
chaji = var-var1       # 差集
print(chaji)

Effect
insert image description here

2.3 pandas, matplotlib library examples

2.3.1 Create a Series object for a geographic location data

code:

import pandas as pd
print('-------  列表创建Series  --------')
s1=pd.Series([1,1,1,1,1])
print(s1)
print('-------  字典创建Series  --------')
s2=pd.Series({
    
    'Longitude':39,'Latitude':116,'Temperature':23})
print('First value in s2:',s2['Longitude'])
print('------- 用序列作Series索引 --------')
s3=pd.Series([3.4,0.8,2.1,0.3,1.5],range(5,10))
print('First value in s3:',s3[5])

Effect:
insert image description here

2.3.2DataFrame object

code:

import pandas as pd
dict1={
    
    'col':[1,2,5,7],'col2':['a','b','c','d']}
df=pd.DataFrame(dict1)
df

Effect:
insert image description here

2.3.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:
insert image description here

2.3.4 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:
insert image description here

2.3.5 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:
insert image description here

2.3.6 Draw sin(x) function image

code:

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-10,10,100) #列举出100个数据点
y=np.sin(x)  #计算出对应的y
plt.plot(x,y,marker="o")

Effect:
insert image description here

3. What is the Turing Test

3.1 Definition

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.

3.2 Understanding

When humans do not know whether the other party is a human or a machine, they can judge whether it is a human or a computer by responding to various questions raised by it. Through a series of such tests, the success of computer intelligence can be measured from the probability that the computer is misjudged as a human being.
If 30% of the judges think it is human, it is said to have human intelligence.

Four. Summary

The first time I came into contact with Jupyter Notebook, I found that programming in this cell format is quite simple, convenient and practical. It is also very convenient to download. Install Anaconda directly to solve the installation problem of Jupyter Notebook, because Anaconda has automatically installed Jupter Notebook and other tools for you, as well as more than 180 scientific packages and their dependencies in python. At the same time, through this, I reviewed the Python I learned before, which laid the foundation for the later study. At the same time, I learned about numpy, pandas, matplotlib library operations and what is the Turing test.

5. References

Jupyter Notebook introduction, installation and use tutorial
20 numpy exercises
Turing test

Guess you like

Origin blog.csdn.net/asdhnkhn/article/details/129448357