{机器学习}【Python语言学习】

Numpy库(多维数组操作,矩阵运算)

'''
numpy库 使用例子1:
'''

import numpy as np

X = np.array([1,2,3.4])
Y = np.array([X,X,X])
for i in X:
    print(i)
print("Y is: \n{}".format(Y))
print("Y.flatten() is: {}".format(Y.flatten()))
print("Y[0][1] is: {}".format(Y[0][1])) #注意下标从0开始
print("X[X>1] is: {}".format(X[X>1]))

Z = np.array([[1,2],[3,4,5],[6]])
print("Z is: {}".format(Z))
print("Z[1][2] is: {}".format(Z[1][2]))
'''
1.0
2.0
3.4
Y is: 
[[1.  2.  3.4]
 [1.  2.  3.4]
 [1.  2.  3.4]]
Y.flatten() is: [1.  2.  3.4 1.  2.  3.4 1.  2.  3.4]
Y[0][1] is: 2.0
X[X>1] is: [2.  3.4]
Z is: [list([1, 2]) list([3, 4, 5]) list([6])]
Z[1][2] is: 5
'''

 

常见问题:

1. import 包(文件)名 as 别名(简称) :使用的时候需要 :别名.函数名/变量名  

    from  包(文件)名 import 需要使用的函数名变量名 : 不用加点 直接使用

2.if __name__ == '__main__'的意思是:当.py文件被直接运行时,if __name__ == '__main__'之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == '__main__'之下的代码块不被运行

'''
--------------------------------------------------
例子:类、__name__ == '__main__'、导包 
--------------------------------------------------
'''
'''
--------------------------------------------------
t0.py
--------------------------------------------------
'''
class Human:
    def __init__(self,name,height,age):
        self.name = name
        self.height = height
        self.age = age
        if name == "HanDongqi":
            print("Hdq is coming !")
    def print_height(self):
        print("{}'s height is:{} ".format(self.name,self.height))
    
if __name__ == '__main__':
    hdq = Human("HanDongqi",180,22)
    hdq.print_height()

t0_variable = 2

def t0_func(variable):
    print("variable is {} ".format(variable))
    
t0_func(666)


'''
Hdq is coming !
HanDongqi's height is:180 
variable is 666 
'''

'''
--------------------------------------------------
t1.py
--------------------------------------------------
'''
from t0 import Human,t0_variable,t0_func

tmp = Human("sb",12,1)
tmp.print_height()
t0_func(t0_variable)

'''
variable is 666 
sb's height is:12 
variable is 2  
'''

 

你可能感兴趣的:(机器学习)