numpy中有关机器学习的一些知识点笔记1

这是第一部分关于Numpy的一些基本操作
1. 文件的读取以及help的用法
2. numpy中的核心操作结构array,查看结构
3. 要保证array里面的元素是同一类型
4. 文件的读取后的行列的读取
5. 打印多维矩阵的某一列或某一行(从0开始)
6. 判断数组里面是否有这个值,对每一个元素进行判断
7. 数组类型的转换
8. 求数组的极值

import numpy as np
#1文件的读取以及help的用法
world_alcohol=np.genfromtxt(r"D:\Python.code(4)\1-numpy\world_alcohol.txt",delimiter=",",dtype= str)#读取文件,用genfromtxt便利txt文件的读取
print(type(world_alcohol ))#查看类型
print(world_alcohol )
print(help(np.genfromtxt) )#查看函数的用法

#2numpy中的核心操作结构array
vector=np.array([1,2,3,4])#一维,结果:(4,),解释:一维,有4个元素
print(vector.shape)#看结构
matrix=np.array([[5,10,51],[20,25,40]])#二维,结果:(2,3),解释:二维,每一维有3个元素
print(matrix .shape)

#3 ,要保证array里面的元素是同一类型
numbers=np.array([1,2,3,4])
print(numbers)
print(numbers .dtype)

#4,
world_alcohol=np.genfromtxt(r"D:\Python.code(4)\1-numpy\world_alcohol.txt",delimiter=",",dtype= str)#读取文件
uruguay_other_1986=world_alcohol[1,4]
third_country=world_alcohol [2,2]
print(uruguay_other_1986)
print(third_country)

#5 打印多维矩阵的某一列或某一行(从0开始)
matrix =np.array([
                [5,10,15],
                [10,15,20],
                [15,30,40]
                ])
print(matrix [:,0])#第一列
print(matrix [:,0:2])#第一列和第三列

#6判断数组里面是否有这个值,对每一个元素进行判断
vector2=np.array([5,10,15,202])
e10=(vector2 ==10)
print(e10 )#结果:[False  True False False]
print(vector[e10])

#7数组类型的转换
vector3=np.array([5,10,15,22])
print(vector3.dtype)#int32
print(vector3)
vector3 =vector.astype(float)
print(vector3.dtype)#float64
print(vector3)

#8求极值的操作,最大值最小值
vector4=np.array([5,10,15,22])
print(vector4 .min())

本文以唐宇迪老师的机器学习课程为基础进行笔记的记录和学习,仅用于学习!

猜你喜欢

转载自blog.csdn.net/weixin_42836427/article/details/81346339
今日推荐