python格式输出矩阵_numpy常用功能总结、python格式化输入输出

#coding:utf-8

#author:徐卜灵

#####################

#由于在各大公司笔试的时候总是会遇到一些格式化输入输出数据,今天就来总结一下。

#结合numpy来处理数据

#####################

###1.第一行输入一个数n,之后输入n个数,以空格隔开

# n = int(raw_input())

# L = [int(x) for x in raw_input().split(' ')]

# print n,L

#上下好像没什么联系

###############################################################

###2.第一行输入一个数n,之后输入n行,每行的数以空格隔开

# n = int(raw_input())

# L = [0] * n#这里一定要赋值为空

# for i in range(n):

# L[i] = [int(x) for x in raw_input().split(' ')]

# print n,L

# print type(L[2][2])

####################################################################################################

#############################################numpy知识点详解##########################################

import numpy as np

import matplotlib.pyplot as plt

##① numpy里的所有元素必须是形同类型的,每个数组都有一个shape,和一个dtype,也就是说numpy多维数组的固有的两个属性

# data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]#这里的data 是个list,下面一行代码将list转化为array

# arr0 = np.array(data)#这一行将list转化为array

# print arr0.shape,arr0.dtype,arr0.ndim

##②创建制定形状的全0和全1的array

# arr_all0 = np.zeros((3,5))#注意这里有两个小括号

# arr_all1 = np.ones((3,5))

# arr_empty =np.empty((2,3,2))#这里可以嵌套,两个二维数组,每个二维数组是2*3的。但empty返回的是未初始化的垃圾值,一般不用这个函数

# print arr_all0,'\n',arr_all1,'\n',arr_empty

# ##③np.arange,跟range(15)一样的作用,注意两者的type不一样,不没什么影响

# L1 = np.arange(15)

# L2 = range(15)

# print L1,type(L1)#注意,没有逗号分割 1 * 15

# print L2,type(L2)

# print np.eye(15)#创建一个正方形的单位阵,下同

# print np.identity(15)

# ##④dtype 可以直接修改数据类型

# arr1 = np.array([1,2,3],dtype=np.float32)

# arr2 = np.array([1.6,-2.3,3],dtype=np.int32)#小数部分被截断,强制转换的时候

# print arr1,arr1.dtype

# print arr2,arr2.dtype

# arr3 = np.array([1,2,3])

# arr3.astype('int32')#显式修改类型

# print arr3.dtype

##5.数组和标量之间的运算

# arr = np.array([[1,2,3],[4,5,6]])

# print arr * arr

# print 1.0/arr

# print arr ** 0.5

##6.索引和切片

# arr = np.arange(10)

# print arr[5]

# print arr[7:9]

# arr[7:9] = 12#将索引为7,8的赋值为12

# print arr #这里arr也发生了变化

# print arr[:] #这里arr也发生了变化

# L = range(10)

# L[7:9] = 12,12

# print L

# #二维数组的索引和切片

# arr2d = np.array([[1,2,3],[2,3,4],[1,2,3],[4,5,6]])

# print arr2d[2]

# print arr2d[2,2],arr2d[2][2]#这两种效果等价

# print arr2d[:2,1:]

# print arr2d[:,:1]#:选取整个轴

# data = np.random.rand(7,4)#利用python中numpy.random.randn()可以生成随机数

# print data

# # numpy中有一些常用的用来产生随机数的函数,randn()和rand()就属于这其中。

# # numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值。

# # numpy.random.rand(d0, d1, …, dn)的随机样本位于[0, 1)中。

#

# ##花式索引

# arrhua = np.zeros((8,4))

# for i in range(8):

# arrhua[i] = i

# print arrhua,arrhua[[4,3,0,6]],arrhua[[-3,-5,-7]]

# ##7.reshape,square,sqre,exp

# arr = np.arange(32).reshape((8,4))

# print arr

# print arr.T

# print np.dot(arr.T,arr)#矩阵点乘

# print np.square(arr)

# print np.sqrt(arr)

# print np.exp(arr)

# x = np.random.randn(8)

# y = np.random.rand(8)

# # y = np.random(8)

# print np.maximum(x,y)

# z = np.random.rand(7)*5#这个乘以5很有意思哦

# print z

# #还有一些函数

# #abs,sqrt,square,exp,log,log10,log2,log1p,sign,ceil,floor,rint

# #isnan,isfinite,isinf,cos,cosh,sin,sinh,tan,tanh

###8.利用数组进行数据预处理

# points = np.arange(-5,5,0.01)#-5到5,间隔0.01取点

# xs,ys = np.meshgrid(points,points)

# #print xs,ys

# z = np.sqrt(xs**2+ys**2)

# plt.imshow(z,cmap = plt.cm.gray)

# plt.colorbar()

# plt.show()

###9.x if condition else y

# xarr = np.array([1.0,1.1,1.2,1.3,1.4])

# yarr = np.array([2.0,2.1,2.2,2.3,2.4])

# cond = np.array([True,False,True,False,True])

# result = [(x if c else y) for x,y,c in zip(xarr,yarr,cond) ]#有个zip,注意应用

# print result

#

# rr = np.where(cond,xarr,yarr)

# print rr

# arr = np.random.randn(4,4)

# print np.where(arr>0,5,-5)#大于0修改为5小于0修改为-5

# print np.where(arr>0,5,arr)#小于0不做处理

#10.数学和统计方法

#sum(0),mean(1),std(),var()标准差方差,min(),max(),argmin,argmax(),sumsum,cumprod后面俩比较不常用

# arr = np.random.randn(5,4)#后面是维度,正态分布的数据

# print arr.mean(),np.mean(arr),arr.sum()

# print arr.mean(axis=1),np.mean(arr),arr.sum(0)#0计算行,1计算列

# print arr.cumsum(0),arr.cumprod(1)#所有元素的累计和,累计积

#11.排序

# arr_sort = np.random.randn(9)

# arr_sort.sort()

# print arr_sort

# arr_sort = np.random.randn(3,2)

# arr_sort.sort(1)

# print arr_sort

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值