日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

机器学习入门---------numpy

發布時間:2024/4/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习入门---------numpy 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一個是學習一些python的庫。首先學習的是numpy的庫。

import numpy
vector =numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(vector)

print(matrix)

結果:

[ 5 10 15 20] [[ 5 10 15][20 25 30][35 40 45]] numpy的array可以創建向量。一個[]代表創建一緯向量,兩個[[]]兩個代表創建二維向量,以此類推。
vector = numpy.array([1,2,3,4])
print(vector.shape)
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])

print(matrix.shape)結果:

(4,) (3, 3)shape是可以用來查詢向量的基本情況,幾行幾列

numbers = numpy.array([1,2,3,4])

numbers.dtype

結果:

dtype('int32')dtype用來查詢類型

vector =numpy.array([5,10,15,20])

print(vector[0:3])結果:

[ 5 10 15]

前包后不包

matrix = numpy.array([[5,10,15],
????????????????????? [20,25,30],
????????????????????? [35,40,45]])

print(matrix[:,1])

print(matrix[:,0:2])

結果:

[10 25 40] [[ 5 10][20 25][35 40]]第一個位置的:表示所有的行,第二個位置表示列。

vector =numpy.array([5,10,15,20])

vector ==10

結果:

array([False, True, False, False], dtype=bool)

查找向量中有沒有10,有的話返回true,沒有的話返回false,后面的是類型

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]與前面一樣,然后在true的地方返回了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])

結果:

[False True False] [[20 25 30]]

這是上面的方法的應用,由列找出了行(包含true的那一行)

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.]這是類型的轉換,由String類型轉變為float

matrix = numpy.array([[5,10,15],
????????????????????? [20,25,30],
????????????????????? [35,40,45]])

matrix.sum(axis = 1)結果:

array([ 30, 75, 120])sum是求和,axis=1表示的是行,axis=0,表示的是列

import numpy as np
a=np.arange(15).reshape(3,5)
a? 結果:

array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]]) arange是一個數組,reshape是可以轉化為一個向量,但是要確保可以被切割np.arange(10,30,5)結果: array([10, 15, 20, 25]) 當不是一個變量時表示:從10開始每次加5,但是要小于30

np.random.random((2,3))

結果:

array([[ 0.84933299, 0.10524939, 0.85124946],[ 0.75002188, 0.27174962, 0.67449709]])隨機生成0~1之前的數,不包含1from numpy import pi

np.linspace( 0, 2*pi, 100 )

結果:

array([ 0. , 0.06346652, 0.12693304, 0.19039955, 0.25386607,0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866,0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126,0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385,1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644,1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903,1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162,2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421,2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 ,2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939,3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199,3.4906585 , 3.55412502, 3.61759154, 3.68105806, 3.74452458,3.8079911 , 3.87145761, 3.93492413, 3.99839065, 4.06185717,4.12532369, 4.1887902 , 4.25225672, 4.31572324, 4.37918976,4.44265628, 4.5061228 , 4.56958931, 4.63305583, 4.69652235,4.75998887, 4.82345539, 4.88692191, 4.95038842, 5.01385494,5.07732146, 5.14078798, 5.2042545 , 5.26772102, 5.33118753,5.39465405, 5.45812057, 5.52158709, 5.58505361, 5.64852012,5.71198664, 5.77545316, 5.83891968, 5.9023862 , 5.96585272,6.02931923, 6.09278575, 6.15625227, 6.21971879, 6.28318531])0~2pi之間安裝100均分A = np.array( [[1,1],
?????????????? [0,1]] )
B = np.array( [[2,0],
?????????????? [3,4]] )
print (A)
print (B)
#print A*B
print(A.dot(B))
print (np.dot(A, B))結果: [[1 1][0 1]] [[2 0][3 4]] [[5 4][3 4]] [[5 4][3 4]] A*B代表著對應位置相乘,A.dot(B)是向量的點乘,就是我們平時的向量相乘 #Return the floor of the input
a = np.floor(10*np.random.random((3,4)))
print (a)

a.shape
## flatten the array
print (a.ravel())
#a.shape = (6, 2)
#print a
#print a.T
print (a.resize((2,6)))
print (a)

#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
a.reshape(3,-1)

結果:

[[ 8. 7. 0. 2.][ 5. 8. 5. 8.][ 8. 3. 7. 7.]] [ 8. 7. 0. 2. 5. 8. 5. 8. 8. 3. 7. 7.] ravel可以把向量壓平成數組 None [[ 8. 7. 0. 2. 5. 8.][ 5. 8. 8. 3. 7. 7.]] array([[ 8., 7., 0., 2.],[ 5., 8., 5., 8.],[ 8., 3., 7., 7.]]) a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print (a)
print ('---')
print (b)
print ('---')
print (np.hstack((a,b)))

#np.hstack((a,b))

結果:

[[ 0. 7.][ 3. 1.]] --- [[ 9. 8.][ 1. 4.]] --- [[ 0. 7. 9. 8.][ 3. 1. 1. 4.]] hstack兩個向量拼接a = np.floor(10*np.random.random((2,12)))
#print a
#print np.hsplit(a,3)
#print np.hsplit(a,(3,4))?? # Split a after the third and the fourth column
a = np.floor(10*np.random.random((12,2)))

print (a)

np.vsplit(a,3)

結果:

[array([[ 5., 9.],[ 8., 0.],[ 2., 7.],[ 2., 2.]]), array([[ 3., 2.],[ 9., 1.],[ 4., 8.],[ 0., 2.]]), array([[ 8., 2.],[ 2., 3.],[ 9., 0.],[ 0., 1.]])]vsplit是安裝行切分成3分#The copy method makes a complete copy of the array and its data.
d = a.copy()
d is a
d[0,0] = 9999
print (d)

print (a)

用了copy之后,兩個向量就不會同時指向一個地址了,改變就沒有事了

a = np.arange(0, 40, 10)
b = np.tile(a, (3, 5))
print (b)結果:

[[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30][ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30][ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]生成一個3行5列的向量a = np.array([[4, 3, 5], [1, 2, 1]])
#print a
#b = np.sort(a, axis=1)
#print b
#b
#a.sort(axis=1)
#print a
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
print (j)
print (a[j])結果: [2 3 1 0] [1 2 3 4] argsort是指安裝排序后的序號:例如:1最小,1的index為2.。。




總結

以上是生活随笔為你收集整理的机器学习入门---------numpy的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。