1python 一個.py文件如何調用另一個.py文件中的類和函數
——之前老是導入不了模塊或者包,創建python_Package就解決了
2python中 x=x[1:] 是什麼意思
X[:,0]是numpy中數組的一種寫法,表示對一個二維數組,取該二維數組第一維中的所有數據,第二維中取第0個數據,
直觀來說,X[:,0]就是取所有行的第0個數據, X[:,1] 就是取所有行的第1個數據。
3Sklearn的train_test_split用法
語法
X_train,X_test, y_train, y_test =cross_validation.train_test_split(X,y,test_size, random_state)
參數說明
Code | Text |
X | 待劃分的樣本特徵集合 |
y | 待劃分的樣本標籤 |
test_size | 若在0~1之間,爲測試集樣本數目與原始樣本數目之比;若爲整數,則是測試集樣本的數目。 |
random_state | 隨機數種子 |
X_train | 劃分出的訓練集數據(返回值) |
X_test | 劃分出的測試集數據(返回值) |
y_train | 劃分出的訓練集標籤(返回值) |
y_test | 劃分出的測試集標籤(返回值) |
代碼示例
輸入:
import numpy as np
from sklearn.model_selection import train_test_split
#創建一個數據集X和相應的標籤y,X中樣本數目爲100
X, y = np.arange(200).reshape((100, 2)), range(100)
#用train_test_split函數劃分出訓練集和測試集,測試集佔比0.33
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42)
#打印出原始樣本集、訓練集和測試集的數目
print("The length of original data X is:", X.shape[0])
print("The length of train Data is:", X_train.shape[0])
print("The length of test Data is:", X_test.shape[0])
輸出:
The length of original data X is: 100
The length of train Data is: 67
The length of test Data is: 33
4python:shape和reshape()函數
在numpy中,shape和reshape()函數很常用。二者的功能都是對於數組的形狀進行操作。
shape函數可以瞭解數組的結構;
reshape()函數可以對數組的結構進行改變。
shape:
import numpy as np
#設置一個數組
a = np.array([1,2,3,4,5,6,7,8])
print(a.shape) # '''結果:(8,)'''
print(type(a.shape)) # '''結果:tuple'''
print(a.shape[0] ) #'''結果:8'''
reshape:
import numpy as np
a = np.array([1,2,3,4,5,6,7,8])
print(a.reshape(2,4))
'''結果:array([[1, 2, 3, 4],[5, 6, 7, 8]])'''
print('---------------')
print(a.reshape(4,2))
'''結果:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
'''