#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/8/9 16:51
# @Author : limingyu
# @Site :
# @File : Test_Pythona_tyd.py
# @Software: PyCharm
import numpy
from numpy import pi
#读取文件路径,文件中内容分隔符,以str类型读出,结果为矩阵形式
iris_data = numpy.genfromtxt("8.iris.data",delimiter=",",dtype=str)
flower_type = iris_data[1,4] #取出矩阵某个数
print(flower_type) #Iris-setosa
print(type(iris_data)) #numpy类型<class 'numpy.ndarray'>
print(iris_data)
print(help(numpy.genfromtxt))#查看方法使用
#向量
vector = numpy.array([5,10,15,20])
print(vector)
print(vector.shape) #查看矩阵结构
print(vector[0:3]) #左闭右开[5 10 15]
print(vector == 10) #判断[False True False False]
#求极值
print(vector.max()) #20
#矩阵
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(matrix)
print(matrix.shape) #查看矩阵结构
# :表示所有的
print(matrix[:,1]) #所有行,第一列[10 25 40]
print(matrix[:,0:2]) #所有行,第0和1列[[5 10]
#[20 25]
#[35 40]]
#按行操作axis=1
print(matrix.sum(axis=1)) #[30 75 120]
#按列操作axis=0
print(matrix.sum(axis=0)) #[60 75 90]
print(matrix == 25) #判断
#[[False False False]
# [False True False]
#[False False False]]
#array中要求数据类型相同
number = numpy.array([1,2,3,4])
print(number) #[1 2 3 4]
#如果有一个数类型不同,则更新为同一类型
numbers = numpy.array([1,2,3,4.0])
print(numbers) #[1. 2. 3. 4.]
num = numpy.array([1,2,3,'4'])
print(num) #['1' '2' '3' '4']
print(num.dtype) #<U11
#值类型转换
vector = numpy.array(["1","2","3"])
print(vector.dtype) #<U1
print(vector) #['1' '2' '3']
#将string类型转换成float
vector = vector.astype(float)
print(vector.dtype) #float64
print(vector) #[1. 2. 3.]
#创造一个向量arange()
print(numpy.arange(15))#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
#将向量转化成矩阵reshape()
a = numpy.arange(15).reshape(3,5)
print(a)
#[[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]]
#矩阵属性,用于观察矩阵
print(a.shape) #(3, 5)
print(a.ndim) #维度2
print(a.dtype.name) #类型int32
print(a.size) #元素个数 15
#初始化全为0矩阵,需用元组,默认为float类型
print(numpy.zeros((3,4)))
#[[0. 0. 0. 0.]
#[0. 0. 0. 0.]
# [0. 0. 0. 0.]]
print(numpy.ones((2,3,4),dtype=numpy.int32))#指定int类型
#[[1 1 1 1]
#[1 1 1 1]
#[1 1 1 1]]]
#创建序列
#区间范围[10,30)左闭右开,每隔5个一加
print(numpy.arange(10,30,5)) #[10 15 20 25]
#随机函数
print(numpy.random.random((2,3)))
#[[0.77922318 0.36761841 0.50061696]
#[0.10202432 0.57769901 0.09236371]]
#创造区间范围[0,2π),共100个数
print(numpy.linspace(0,2*pi,100))
#矩阵乘积与内积
A = numpy.array([[1,1],[0,1]])
B = numpy.array([[2,0],[3,4]])
print(A) #[[1 1]
#[0 1]]
print(B) #[[2 0]
#[3 4]]
#对应位置相乘
print(A*B) #[[2 0]
#[0 4]]
#矩阵乘法两种方法
print(A.dot(B)) #[[5 4]
#[3 4]]
print(numpy.dot(A,B)) #[[5 4]
#[3 4]]
#e用法exp
B = numpy.arange(3)
print(B) #[0 1 2]
print(numpy.exp(B)) #指数[1. 2.71828183 7.3890561 ]
print(numpy.sqrt(B)) #根号[0. 1. 1.41421356]
#floor向下取整
a = numpy.floor(10*numpy.random.random((3,4)))
print(a)
#[[5. 3. 0. 8.]
#[8. 7. 7. 7.]
#[1. 2. 0. 1.]]
#将矩阵拉成向量
print(a.ravel()) #[5. 3. 0. 8. 8. 7. 7. 7. 1. 2. 0. 1.]
a.shape = (6,2) #变成6行2列
print(a)
#[[5. 3.]
#[0. 8.]
#[8. 7.]
#[7. 7.]
#[1. 2.]
#[0. 1.]]
print(a.T) #转置,行和列变换
#[[5. 0. 8. 7. 1. 0.]
#[3. 8. 7. 7. 2. 1.]]
#矩阵拼接方法:按行或按列
a = numpy.floor(10*numpy.random.random((2,2)))
b = numpy.floor(10*numpy.random.random((2,2)))
print(a) #[[0. 0.]
#[3. 4.]]
print(b) #[[5. 1.]
#[ 0. 1.]]
#对矩阵a,b横着拼接(horizontal:横着)
print(numpy.hstack((a,b)))
#[[0. 0. 5. 1.]
#[3. 4. 0. 1.]]
#对矩阵a,b竖 着拼接(vertical:竖着)
print(numpy.vstack((a,b)))
#[[0. 0.]
#[3. 4.]
#[5. 1.]
#[0. 1.]]
#矩阵切分
a = numpy.floor(10*numpy.random.random((2,12)))
print(a) #[[5. 5. 7. 1. 8. 9. 3. 6. 5. 9. 0. 4.]
#[6. 9. 1. 3. 1. 5. 9. 2. 3. 7. 8. 3.]]
#对矩阵a,切成3份
print(numpy.hsplit(a,3))
#[array([[5., 5., 7., 1.],
# [6., 9., 1., 3.]]), array([[8., 9., 3., 6.],
# [1., 5., 9., 2.]]), array([[5., 9., 0., 4.],
# [3., 7., 8., 3.]])]
#按指定位置切分,在第三和第四位置各切一刀
print(numpy.hsplit(a,(3,4)))
#[array([[5., 5., 7.],
# [6., 9., 1.]]), array([[1.],
# [3.]]), array([[8., 9., 3., 6., 5., 9., 0., 4.],
# [1., 5., 9., 2., 3., 7., 8., 3.]])]
a = numpy.floor(10*numpy.random.random((12,2)))
print(a)
#[[1. 9.]
# [2. 1.]
# [4. 4.]
# [7. 5.]
# [1. 8.]
# [9. 9.]
# [5. 8.]
# [8. 2.]
# [6. 2.]
# [6. 9.]
# [6. 1.]
# [6. 0.]]
print(numpy.vsplit(a,3))
#[array([[1., 9.],
# [2., 1.],
# [4., 4.],
# [7., 5.]]), array([[1., 8.],
# [9., 9.],
# [5., 8.],
# [8., 2.]]), array([[6., 2.],
# [6., 9.],
# [6., 1.],
# [6., 0.]])]
#复制三种方式
#a和b指向同一个空间
a = numpy.arange(12)
b = a
print(b is a) #True
b.shape = 3,4
print(id(a)) #65122016
print(id(b)) #65122016
#view()浅复制,共用同一个值但不指向同一个空间
c = a.view()
print(c is a) #False
c.shape = 2,6
print(a.shape) #(3, 4)
c[0,4] = 1234
print(a) #[[ 0 1 2 3]
#[1234 5 6 7]
#[ 8 9 10 11]]
print(id(a)) #65122016
print(id(c)) #65122176
#copy(),两个指向不同位置,不同数据
d = a.copy()
print(d is a) #False
d[0,0] = 9999
print(d) #[[9999 1 2 3]
#[1234 5 6 7]
#[ 8 9 10 11]]
print(a) #[[ 0 1 2 3]
#[1234 5 6 7]
#[ 8 9 10 11]]
#操作
data = numpy.sin(numpy.arange(20)).reshape(5,4)
print(data) #[[ 0. 0.84147098 0.90929743 0.14112001]
#[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
#[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
#[-0.53657292 0.42016704 0.99060736 0.65028784]
#[-0.28790332 -0.96139749 -0.75098725 0.14987721]]
#取出每列中的最大索引
ind = data.argmax(axis=0)
print(ind) #[2 0 3 1]
#取出最大值
data_max = data[ind,range(data.shape[1])]
print(data_max) #[0.98935825 0.84147098 0.99060736 0.6569866 ]
#tile:对行和列进行扩展
a = numpy.arange(0,40,10)
print(a) #[ 0 10 20 30]
b = numpy.tile(a,(2,2)) #行列变成2倍
print(b) #[[ 0 10 20 30 0 10 20 30]
#[ 0 10 20 30 0 10 20 30]]
#矩阵排序
a = numpy.array([[4,3,5],[1,2,1]])
print(a) #[[4 3 5]
#[1 2 1]]
#排序方法一
b = numpy.sort(a,axis=1) #行排序
print(b) #[[3 4 5]
#[1 1 2]]
#排序方法二
a.sort(axis=1)
print(a) #[[3 4 5]
#[1 1 2]]
#先求最小值索引,接着求
a = numpy.array([4,3,1,2])
j = numpy.argsort(a)
print(j) #[2 3 1 0]
print(a[j]) #[1 2 3 4]