Python数据分析与机器学习-NumPy_2

import numpy
# it will compare the second value to each element in the vector
# If the value are equal, the Python interpreter returns True; otherwise, it return False
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]])
# 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])
[False  True False False]
[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,:]) # return the raw corresponding to True
[False  True False]
[[20 25 30]]
# We can also perform comparisons with multipe conditions
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)
[ True  True False False]
# We can convert the date 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)
<U1
['1' '2' '3']
float64
[1. 2. 3.]
vector = numpy.array([5,10,15,20])
vector.sum()
50
# The axis dictates which dimension we perform the operator on
# 1 menas 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)
array([ 30,  75, 120])
matrix = numpy.array([[5,10,15],
                      [20,25,30],
                      [35,40,45]])
matrix.sum(axis=0)
array([60, 75, 90])

转载于:https://www.cnblogs.com/SweetZxl/p/11124173.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值