今天是1024欸,發個貼拿個勳章
至於爲何1024這個數字很重要,由於1024是2的10次方python
補了一個系列關於這個的實例教程
機器學習參考篇: python+sklearn+kaggle機器學習
用python+sklearn(機器學習)實現天氣預報 準備
用python+sklearn(機器學習)實現天氣預報數據 數據
用python+sklearn(機器學習)實現天氣預報 模型和使用web
kaggle是一個學習ml也就是機器學習的平臺
上面會有教程教如何用python寫機器學習和各式各樣的機器學習競賽數組
經過pd(pandas)和sklearn下的split,從csv文件提取和分割數據集
例:dom
from sklearn.model_selection import train_test_split X=pd.read_csv("/kaggle/input/home-data-for-ml-course/train.csv") y=X.SalePrice X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2,random_state=0) X_train=X_train.drop(['SalePrice'],axis=1)
其中,read_csv就是從csv文件中提取數據集
train_test_split就是把一個完整的數據集和驗證集以同等的比例分紅2組不一樣的數據集和驗證集
由於saleprice
是咱們要預測的數據,因此驗證集裏就只有這個的數據,而數據集裏要剔除這個數據機器學習
在現實狀況中,一些數據集是不完整的或數據是文本,因此要先對數據集預處理svg
例:函數
from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline from sklearn.impute import SimpleImputer from sklearn.preprocessing import OneHotEncoder from sklearn.ensemble import RandomForestRegressor from sklearn.preprocessing import LabelEncoder # Preprocessing for numerical data numerical_transformer = SimpleImputer(strategy='constant') # Preprocessing for categorical data categorical_transformer = Pipeline(steps=[ ('imputer', SimpleImputer(strategy='most_frequent')), ('onehot', OneHotEncoder(handle_unknown='ignore')) ]) # Preprocessing for categorical data categorical_transformer_1 = Pipeline(steps=[ ('imputer', SimpleImputer(strategy='most_frequent')), ('LabelEncoder', LabelEncoder()) ]) object_cols=[col for col in X_train.columns if X_train[col].dtype=='object'] # Bundle preprocessing for numerical and categorical data preprocessor = ColumnTransformer( transformers=[ ('num', numerical_transformer, [col for col in X_train.columns if not X_train[col].dtype=='object']), ('cat', categorical_transformer, object_cols) ])
其中object_cols
數組是指數據類型非數字的列表學習
先選擇模型,好比XGB或者隨機樹(randomforest)
而後用fit來訓練模型
例:spa
from xgboost import XGBRegressor # Define model model = XGBRegressor(n_estimators=5000, random_state=0,learning_rate=0.01,n_jobs=4) # Bundle preprocessing and modeling code in a pipeline clf = Pipeline(steps=[('preprocessor', preprocessor), ('model', model) ]) # Preprocessing of training data, fit model clf.fit(X_train, y_train)
用predict函數
例:.net
X_test=pd.read_csv("/kaggle/input/home-data-for-ml-course/test.csv") pre=clf.predict(X_test)
用MAE(mean_absolute_error)方法算出這個模型的分數(準確度)
例:
from sklearn.metrics import mean_absolute_error a=mean_absolute_error(y_valid,pre)
在kaggle.com平臺上都有詳細的教程
其實學完後在簡單的比賽拿top10%也是挺容易的