Guest User

Untitled

a guest
Dec 5th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 112,
  6. "metadata": {
  7. "scrolled": false
  8. },
  9. "outputs": [],
  10. "source": [
  11. "import pandas as pd\n",
  12. "import numpy as np\n",
  13. "from sklearn.model_selection import train_test_split, cross_val_score\n",
  14. "from sklearn.neighbors import KNeighborsClassifier\n",
  15. "import matplotlib.pyplot as plt\n",
  16. "test_inputs = pd.read_csv('Stockdaily%changeraw.csv', delimiter=',')\n",
  17. "test_inputs = np.asarray(test_inputs)\n",
  18. "test_inputs\n",
  19. "\n",
  20. "import warnings\n",
  21. "warnings.filterwarnings('ignore')"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 121,
  27. "metadata": {},
  28. "outputs": [
  29. {
  30. "name": "stdout",
  31. "output_type": "stream",
  32. "text": [
  33. "[[-0.00155 0.00371 0.00399 -0.00111 -0.00094]\n",
  34. " [-0.00449 -0.00635 -0.00175 -0.00406 -0.00468]\n",
  35. " [ 0.00046 -0.00246 0.00224 0.00171 -0.00046]\n",
  36. " ...\n",
  37. " [-0.00119 -0.00086 -0.0025 -0.00078 0.00025]\n",
  38. " [-0.00716 -0.007 -0.00817 -0.00483 -0.00177]\n",
  39. " [ 0.00326 0.00073 0.00436 0.00166 0.00516]]\n"
  40. ]
  41. }
  42. ],
  43. "source": [
  44. "X = test_inputs[:,15:20] #data \n",
  45. "Y = test_inputs[:,21] #target\n",
  46. "X=X.astype('float')\n",
  47. "Y=Y.astype('float')\n",
  48. "\n",
  49. "print(X)\n"
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": 129,
  55. "metadata": {},
  56. "outputs": [
  57. {
  58. "name": "stdout",
  59. "output_type": "stream",
  60. "text": [
  61. "[0 0 1 ... 0 0 1]\n"
  62. ]
  63. }
  64. ],
  65. "source": [
  66. "#test_inputs[test_inputs == np.round(X)]\n",
  67. "#print(X)\n",
  68. "X_d = np.array(np.round(X*1000,decimals=0))\n",
  69. "Y_d = np.array(np.round(Y*1000,decimals=0))\n",
  70. "#print(X_d)\n",
  71. "#print(Y_d)\n",
  72. "X_d=X_d.astype('int')\n",
  73. "Y_d=Y_d.astype('int')\n",
  74. "Y_d = np.array( Y_d>=0, dtype='int')\n",
  75. "#print(X_d)\n",
  76. "print(Y_d)"
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": 130,
  82. "metadata": {},
  83. "outputs": [],
  84. "source": [
  85. "X_train, X_test, y_train, y_test = train_test_split(X_d, Y_d)"
  86. ]
  87. },
  88. {
  89. "cell_type": "code",
  90. "execution_count": 131,
  91. "metadata": {},
  92. "outputs": [
  93. {
  94. "data": {
  95. "text/plain": [
  96. "(1510, 1132, 378)"
  97. ]
  98. },
  99. "execution_count": 131,
  100. "metadata": {},
  101. "output_type": "execute_result"
  102. }
  103. ],
  104. "source": [
  105. "len(X_d),len(X_train),len(X_test)"
  106. ]
  107. },
  108. {
  109. "cell_type": "code",
  110. "execution_count": 132,
  111. "metadata": {},
  112. "outputs": [],
  113. "source": [
  114. "estimator = KNeighborsClassifier(n_neighbors=40)"
  115. ]
  116. },
  117. {
  118. "cell_type": "code",
  119. "execution_count": 133,
  120. "metadata": {},
  121. "outputs": [
  122. {
  123. "data": {
  124. "text/plain": [
  125. "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
  126. " metric_params=None, n_jobs=None, n_neighbors=40, p=2,\n",
  127. " weights='uniform')"
  128. ]
  129. },
  130. "execution_count": 133,
  131. "metadata": {},
  132. "output_type": "execute_result"
  133. }
  134. ],
  135. "source": [
  136. "estimator.fit(X_train, y_train)"
  137. ]
  138. },
  139. {
  140. "cell_type": "code",
  141. "execution_count": 138,
  142. "metadata": {},
  143. "outputs": [
  144. {
  145. "name": "stdout",
  146. "output_type": "stream",
  147. "text": [
  148. "The accuracy is 88.1%\n"
  149. ]
  150. }
  151. ],
  152. "source": [
  153. "y_predicted = estimator.predict(X_test)\n",
  154. "accuracy = np.mean(y_test == y_predicted) *100\n",
  155. "print(\"The accuracy is {0:.1f}%\".format(accuracy))"
  156. ]
  157. },
  158. {
  159. "cell_type": "code",
  160. "execution_count": 139,
  161. "metadata": {},
  162. "outputs": [
  163. {
  164. "name": "stdout",
  165. "output_type": "stream",
  166. "text": [
  167. "The accuracy is 89.5%\n"
  168. ]
  169. }
  170. ],
  171. "source": [
  172. "\n",
  173. "from sklearn.model_selection import cross_val_score\n",
  174. "scores = cross_val_score(estimator, X_d, Y_d, scoring = 'accuracy')\n",
  175. "average_accuracy = np.mean(scores)*100\n",
  176. "print(\"The accuracy is {0:.1f}%\".format(average_accuracy))"
  177. ]
  178. },
  179. {
  180. "cell_type": "code",
  181. "execution_count": null,
  182. "metadata": {},
  183. "outputs": [],
  184. "source": []
  185. }
  186. ],
  187. "metadata": {
  188. "kernelspec": {
  189. "display_name": "Python 3",
  190. "language": "python",
  191. "name": "python3"
  192. },
  193. "language_info": {
  194. "codemirror_mode": {
  195. "name": "ipython",
  196. "version": 3
  197. },
  198. "file_extension": ".py",
  199. "mimetype": "text/x-python",
  200. "name": "python",
  201. "nbconvert_exporter": "python",
  202. "pygments_lexer": "ipython3",
  203. "version": "3.7.0"
  204. }
  205. },
  206. "nbformat": 4,
  207. "nbformat_minor": 2
  208. }
Add Comment
Please, Sign In to add comment