首页 > 其他分享 >numpy的用法-02

numpy的用法-02

时间:2023-02-06 19:32:32浏览次数:54  
标签:02 10 False 25 用法 vector array numpy


import numpy

#1.array把数组转化为矩阵
In [9]:
#it will compare the second value to each element in the vector
# If the values are equal, the Python interpreter returns True; otherwise, it returns False
vector = numpy.array([5, 10, 15, 20])
vector == 10
import numpy
#it will compare the second value to each element in the vector
# If the values are equal, the Python interpreter returns True; otherwise, it returns False
vector = numpy.array([5, 10, 15, 20])
vector == 10
Out[9]:
array([False, True, False, False], dtype=bool)

#2.二维数组
In [10]:
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix == 25
Out[10]:
array([[False, False, False],
[False, True, False],
[False, False, False]], dtype=bool)

#3.判断是否含有10,并输出
In [6]:
#Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten
vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print equal_to_ten
print(vector[equal_to_ten])
Out[6]:
[False True False False]
[10]


#4.
In [8]:
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
second_column_25 = (matrix[:,1] == 25) #输出第二列 对应的True或false
print(second_column_25)
print(matrix[second_column_25, :]) #second_column_25中为True的行全部输出
Out [8]:
[False True False]
[[20 25 30]]

#5.与操作 同时满足
In [11]:
#We can also perform comparisons with multiple conditions
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
print equal_to_ten_and_five
Out [11]:
[False False False False]

#6.或操作 其中之一满足,或都满足
In [12]:
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
Out [12]:
print equal_to_ten_or_five
[ True True False False]

#7.或操作 复制
In [13]:
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
vector[equal_to_ten_or_five] = 50
print(vector)
Out [13]:
[50 50 15 20]

#8、查看某一列是否存在某个值,并进行修改
In [12]:
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
Out [12]:
[False True False]
[[ 5 10 15]
[20 10 30]
[35 40 45]]

#9、 astype 数据的类型修改
In [14]:
#We can convert the data type of an array with the ndarray.astype() method.
vector = numpy.array(["1", "2", "3"])
print vector.dtype
print vector
vector = vector.astype(float)
print vector.dtype
print vector
Out [14]:
|S1
['1' '2' '3']
float64
[ 1. 2. 3.]

#10、 求和
In [19]:
vector = numpy.array([5, 10, 15, 20])
vector.sum()
Out[19]:
50

In [20]:
#11.sum(axis=1) 每一列求和 sum(axis=0) 每一行求和
# The axis dictates which dimension we perform the operation on
#1 means that we want to perform the operation on each row, and 0 means on each column
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=1)
Out[20]:
array([ 30, 75, 120])

In [21]:
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=0)

Out[21]:
array([60, 75, 90])

In [25]:
#replace nan value with 0
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",")
#print world_alcohol
is_value_empty = numpy.isnan(world_alcohol[:,4]) #不是数组返回nan 否则返回数字
#print is_value_empty
world_alcohol[is_value_empty, 4] = '0' #最后一列是nan 值为0
alcohol_consumption = world_alcohol[:,4]
alcohol_consumption = alcohol_consumption.astype(float)
total_alcohol = alcohol_consumption.sum()
average_alcohol = alcohol_consumption.mean()
print total_alcohol
print average_alcohol
Out [25]:
1137.78
1.14006012024


标签:02,10,False,25,用法,vector,array,numpy
From: https://blog.51cto.com/u_15955675/6040312

相关文章

  • numpy的用法-03
    #coding=utf-8importnumpyasnpimportnumpyaspia=np.arange(15).reshape(3,5)#arange����0-14������reshape���3*5�ľ���print(a)print(a.shape)#输出行和列的长度print(a.ndim)#t......
  • 函数的重载--Java基础026
    /*函数的重载:在一个类中出现两个或者两个以上的同名函数,这个称作为函数的重载。函数重载的作用:同一个函数名可以出现了不同的函数,以应对不同个数或者不同数据类型的参数。......
  • 数组的定义--Java基础027
    /*数组:数组是存储同一种数据类型数据的集合容器。数组的定义格式:数据类型[]变量名=new数据类型[长度];数组:存储同一种数据类型的集合容器.数组的特点:1.只能存储......
  • 2023-2-6 #34 我渴望的夏季 太热烈太清晰
    最近在摆,没更博!189CF1718EImpressionism谔谔题,不妨令\(n\leqslantm\)。用二分图刻画这个问题,无非是求两个带边权的特殊二分图是否同构。一个暴力的想法是对于左部......
  • 2023-02-06 初识网页设计
    网页的定义​ 功能和定位​ 功能的策划​ 设计的美化​ 操作体验​ 对外宣传​ 提升品牌形象网站的分类按对象划分可以分为TOC端和TOB端两种​ TOC端就是面向用......
  • mysql concat函数的用法
    mysql中的这个函数非常强大,可以对查出的参数进行拼接,其实这个方法在java中也有api可以进行调用。那么什么时候进行使用呢?例如,你老大叫你做一个数据库的数据采集,需要整理成......
  • Javascript(es2016) import和require用法和区别
    写个简单js文件,假设名字为:lib.js。假设内容如下:exportconstsqrt=Math.sqrt;exportfunctionsquare(x){returnx*x;}exportfunctiond......
  • 【230206-5】(抛物线与胡不归)抛物线y=-x平方+2x+3的图像交X轴于AB,交Y轴于C点,对称轴交
    ......
  • tf.split()函数的用法
    fromPILimportImageimportnumpyasnpimporttensorflowastf'''split对维度进行分割tf.split(data,数据图片(300*600*3)......
  • 亚马逊审核小书灯/阅读灯UL153测试费用周期?ISO17025实验室
    亚马逊审核小书灯/阅读灯UL153测试费用周期?ISO17025实验室咨询威❤:151-3562-9064程先生小书灯/阅读灯办理亚马逊测试标准UL153在美国亚马逊平台销售,遇到了审核的问题。......