人工智能简介
1、Python基础
环境搭建
Anaconda
使用Anaconda Prompt:
conda list 列表
conda install pandas 安装库
conda update pandas 更新库
代码编辑器 Jupyter Notebook
打印命令:
days = '365'
print(days)
print(type(days))
转换
int_days = int(days)
指数乘法
print(4**3) # 64
List
months = []
months.append("January")
months.append("February")
print(months)
print(len(months))
print(months[-1])
# 结果 ['January', 'February']
print(tex[2:6]) # 包含索引2,但是不包含第6个数值
print(tex[2:]) #从索引2到最后
循环语句
cities = ["Austin","Dallas","Houston"]
for city in cities:
print(city)
i=0
while i<3:
i += 1
print(i)
for i in range(10):
print(i)
布尔类型
cat = True
dog = False
print(type(cat))
查询
animals = ["cat","dog","rabbit"]
for animal in animals:
if animal == "cat":
print("Cat found")
animals = ["cat","dog","rabbit"]
if "cat" in animals:
print("Cat found")
animals = ["cat","dog","rabbit"]
cat_found = "cat" in animals
print(cat_found)
字典
scores = {}
scores["Jim"] = 80
scores["Sue"] = 85
scores["Ann"] = 75
print(type(scores))
print(scores.keys())
print(scores["Sue"])
print("Jim" in scores) # key 是否在字典中
# <class 'dict'>
# dict_keys(['Jim', 'Sue', 'Ann'])
# 85
使用字典计数
pantry = ["apple","orange","grape","apple","orange","apple","tomato","potato","grape"]
pantry_counts = {}
for item in pantry:
if item in pantry_counts:
pantry_counts[item] = pantry_counts[item]+1
else:
pantry_counts[item] = 1
print(pantry_counts)
# {'apple': 3, 'orange': 2, 'grape': 2, 'tomato': 1, 'potato': 1}
Numpy
import numpy
vector = numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(vector)
print(matrix)
# 行向量,矩阵操作定义
vector = numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(vector.shape)
print(matrix.shape)
# 对象属性几行几列
vector = numpy.array([5,10,15,20.3])
print(vector)
print(vector.dtype)
# [ 5. 10. 15. 20.3]
# float64
# 数据类型
vector = numpy.array([5,10,15,20.3])
print(vector[0:3])
# 取三个值,从索引0开始
import numpy
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(matrix[:,1])
# 取所有矩阵中的第二列 [10 25 40]
print(matrix[:,0:2])
"""
所有样板中前两列
[[ 5 10]
[20 25]
[35 40]]
"""
判断
import numpy
vector = numpy.array([5,10,15,20])
vector == 10
"""
array([False, True, False, False])
"""
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
matrix == 25
"""
array([[False, False, False],
[False, True, False],
[False, False, False]])
"""
vector = numpy.array([5,10,10 ,15,20])
equal_to_ten = (vector == 10)
print(equal_to_ten)
print(vector[equal_to_ten])
"""
[False True False False]
[10 10]
"""
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
second_column_25 = (matrix[:,1] == 25)
print(second_column_25)
print(matrix[second_column_25,:])
"""
[False True False]
[[20 25 30]]
"""
vector = numpy.array([5,10,15,20])
equal_to_ten_and_five = (vector==10)&(vector==5)
print(equal_to_ten_and_five)
"""
[False False False False]
"""
判断并赋值
vector = numpy.array([5,10,15,20])
equal_to_ten_or_five = (vector==10)|(vector==5)
print(equal_to_ten_or_five)
vector[equal_to_ten_or_five] = 50
print(vector)
"""
[ True True False False]
[50 50 15 20]
"""
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
second_column_25 = matrix[:,1]==25
print(second_column_25)
matrix[second_column_25,1]=10
print(matrix)
"""
[False True False]
[[ 5 10 15]
[20 10 30]
[35 40 45]]
"""
类型转化
vector = numpy.array(["1","2","3"])
print(vector.dtype)
print(vector)
vector=vector.astype(float)
print(vector.dtype)
print(vector)
"""
<U1
['1' '2' '3']
float64
[1. 2. 3.]
"""
最小值
vector = numpy.array([5,10,15,20])
vector.min()
# 5
按行求值
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
matrix.sum(axis=1)
# array([ 30, 75, 120])
按列求和
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
matrix.sum(axis=0)
"""
array([60, 75, 90])
"""