Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7532fdd

Browse files
Add files via upload
1 parent 88f7e5c commit 7532fdd

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

‎XGBoost.ipynb

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 21,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#import library\n",
10+
"\n",
11+
"from sklearn.datasets import load_boston\n",
12+
"import xgboost as xgb\n",
13+
"from sklearn.metrics import mean_squared_error\n",
14+
"import pandas as pd\n",
15+
"import numpy as np"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": 22,
21+
"metadata": {},
22+
"outputs": [
23+
{
24+
"name": "stdout",
25+
"output_type": "stream",
26+
"text": [
27+
"dict_keys(['data', 'target', 'feature_names', 'DESCR', 'filename'])\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"#load Data\n",
33+
"boston = load_boston()\n",
34+
"print(boston.keys())"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 23,
40+
"metadata": {},
41+
"outputs": [
42+
{
43+
"name": "stdout",
44+
"output_type": "stream",
45+
"text": [
46+
"(506, 13)\n",
47+
"['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'\n",
48+
" 'B' 'LSTAT']\n"
49+
]
50+
}
51+
],
52+
"source": [
53+
"print(boston.data.shape)\n",
54+
"print(boston.feature_names)"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 24,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"#covert data into DataFrame\n",
64+
"data = pd.DataFrame(boston.data)\n",
65+
"data.columns = boston.feature_names"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 25,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"#set depend and predictor\n",
75+
"data['PRICE'] = boston.target\n",
76+
"data.describe()\n",
77+
"X, y = data.iloc[:,:-1],data.iloc[:,-1]"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 26,
83+
"metadata": {},
84+
"outputs": [
85+
{
86+
"name": "stderr",
87+
"output_type": "stream",
88+
"text": [
89+
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/xgboost/core.py:587: FutureWarning: Series.base is deprecated and will be removed in a future version\n",
90+
" if getattr(data, 'base', None) is not None and \\\n",
91+
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/xgboost/core.py:588: FutureWarning: Series.base is deprecated and will be removed in a future version\n",
92+
" data.base is not None and isinstance(data, np.ndarray) \\\n"
93+
]
94+
}
95+
],
96+
"source": [
97+
"#xgb model\n",
98+
"data_dmatrix = xgb.DMatrix(data=X,label=y)"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 27,
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"#spilt data\n",
108+
"from sklearn.model_selection import train_test_split\n",
109+
"\n",
110+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 28,
116+
"metadata": {},
117+
"outputs": [],
118+
"source": [
119+
"#XGBRegressor model\n",
120+
"xg_reg = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,\n",
121+
" max_depth = 5, alpha = 10, n_estimators = 10)"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": 29,
127+
"metadata": {},
128+
"outputs": [
129+
{
130+
"name": "stdout",
131+
"output_type": "stream",
132+
"text": [
133+
"[16:18:05] WARNING: src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.\n"
134+
]
135+
}
136+
],
137+
"source": [
138+
"#fit model\n",
139+
"xg_reg.fit(X_train,y_train)\n",
140+
"\n",
141+
"preds = xg_reg.predict(X_test)"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 30,
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
153+
"RMSE: 10.397587\n"
154+
]
155+
}
156+
],
157+
"source": [
158+
"#check error\n",
159+
"rmse = np.sqrt(mean_squared_error(y_test, preds))\n",
160+
"print(\"RMSE: %f\" % (rmse))"
161+
]
162+
}
163+
],
164+
"metadata": {
165+
"kernelspec": {
166+
"display_name": "Python 3",
167+
"language": "python",
168+
"name": "python3"
169+
},
170+
"language_info": {
171+
"codemirror_mode": {
172+
"name": "ipython",
173+
"version": 3
174+
},
175+
"file_extension": ".py",
176+
"mimetype": "text/x-python",
177+
"name": "python",
178+
"nbconvert_exporter": "python",
179+
"pygments_lexer": "ipython3",
180+
"version": "3.7.7"
181+
}
182+
},
183+
"nbformat": 4,
184+
"nbformat_minor": 4
185+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /