Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 112,
- "metadata": {
- "scrolled": false
- },
- "outputs": [],
- "source": [
- "import pandas as pd\n",
- "import numpy as np\n",
- "from sklearn.model_selection import train_test_split, cross_val_score\n",
- "from sklearn.neighbors import KNeighborsClassifier\n",
- "import matplotlib.pyplot as plt\n",
- "test_inputs = pd.read_csv('Stockdaily%changeraw.csv', delimiter=',')\n",
- "test_inputs = np.asarray(test_inputs)\n",
- "test_inputs\n",
- "\n",
- "import warnings\n",
- "warnings.filterwarnings('ignore')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 121,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[-0.00155 0.00371 0.00399 -0.00111 -0.00094]\n",
- " [-0.00449 -0.00635 -0.00175 -0.00406 -0.00468]\n",
- " [ 0.00046 -0.00246 0.00224 0.00171 -0.00046]\n",
- " ...\n",
- " [-0.00119 -0.00086 -0.0025 -0.00078 0.00025]\n",
- " [-0.00716 -0.007 -0.00817 -0.00483 -0.00177]\n",
- " [ 0.00326 0.00073 0.00436 0.00166 0.00516]]\n"
- ]
- }
- ],
- "source": [
- "X = test_inputs[:,15:20] #data \n",
- "Y = test_inputs[:,21] #target\n",
- "X=X.astype('float')\n",
- "Y=Y.astype('float')\n",
- "\n",
- "print(X)\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 129,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0 0 1 ... 0 0 1]\n"
- ]
- }
- ],
- "source": [
- "#test_inputs[test_inputs == np.round(X)]\n",
- "#print(X)\n",
- "X_d = np.array(np.round(X*1000,decimals=0))\n",
- "Y_d = np.array(np.round(Y*1000,decimals=0))\n",
- "#print(X_d)\n",
- "#print(Y_d)\n",
- "X_d=X_d.astype('int')\n",
- "Y_d=Y_d.astype('int')\n",
- "Y_d = np.array( Y_d>=0, dtype='int')\n",
- "#print(X_d)\n",
- "print(Y_d)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 130,
- "metadata": {},
- "outputs": [],
- "source": [
- "X_train, X_test, y_train, y_test = train_test_split(X_d, Y_d)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 131,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(1510, 1132, 378)"
- ]
- },
- "execution_count": 131,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "len(X_d),len(X_train),len(X_test)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 132,
- "metadata": {},
- "outputs": [],
- "source": [
- "estimator = KNeighborsClassifier(n_neighbors=40)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 133,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
- " metric_params=None, n_jobs=None, n_neighbors=40, p=2,\n",
- " weights='uniform')"
- ]
- },
- "execution_count": 133,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "estimator.fit(X_train, y_train)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 138,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "The accuracy is 88.1%\n"
- ]
- }
- ],
- "source": [
- "y_predicted = estimator.predict(X_test)\n",
- "accuracy = np.mean(y_test == y_predicted) *100\n",
- "print(\"The accuracy is {0:.1f}%\".format(accuracy))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 139,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "The accuracy is 89.5%\n"
- ]
- }
- ],
- "source": [
- "\n",
- "from sklearn.model_selection import cross_val_score\n",
- "scores = cross_val_score(estimator, X_d, Y_d, scoring = 'accuracy')\n",
- "average_accuracy = np.mean(scores)*100\n",
- "print(\"The accuracy is {0:.1f}%\".format(average_accuracy))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.7.0"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment