Solutions.ipynb 5.35 KB
Newer Older
christian.foerster's avatar
christian.foerster committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solutions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read in some of your data with pandas, plot it and get the main statistics on the data.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "%matplotlib inline\n",
    "import cufflinks as cf\n",
    "cf.go_offline()\n",
    "\n",
    "fp=r\"M:\\Projekte\\2013_UWRM\\3_messen und daten\\Messstellen\\Pegel Kreischa\\Q_Kreischa.txt\"\n",
    "my_df=pd.read_csv(fp,parse_dates=True,index_col=0,sep=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.iplot(secondary_y=\"q\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(my_df.describe())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a class, that takes a filepath as an argument when it is initialized. Create a method, that takes a string as an argument and returns the number of occurances of that string in that file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "class StrCount():\n",
    "    def __init__(self,filepath):\n",
    "        self.filepath=filepath\n",
    "        return\n",
    "    \n",
    "    def count(self,string):\n",
    "        count=0\n",
    "        with open(self.filepath) as file:\n",
    "            for line in file:\n",
    "                count+=len(line.split(string))-1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(2,2)\n",
    "fig.text(0,0.5,r\"\\sum$\",rotation=90, verticalalignment='center')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filepath=r\"C:\\Users\\Gabi\\Documents\\Python\\NN\\Inputdata\\PCM_MEZ_good_shape.txt\"\n",
    "count=StrCount(filepath).count(\";\")\n",
    "print(count)\n",
    "\n",
    "# if this takes too long it might be the server connection... (depending on the file size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a function that takes an arbitrary number of inputs and prints them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#this function only takes list like entries\n",
    "def arbitrary_list(*arguments):\n",
    "    print(arguments)\n",
    "    \n",
    "arbitrary_list(2,3,4,5,4,3,2,4,[432],[{2:3}])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#this function only takes list like entries\n",
    "def arbitrary_dict(**dictarguments):\n",
    "    print(dictarguments)\n",
    "    \n",
    "arbitrary_dict(a=3,b=4,wer=88)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#this function can do all list arguments must come before dict arguments\n",
    "def arbitrary(*list_args,**dict_args):\n",
    "    print(list_args)\n",
    "    print(dict_args)\n",
    "    \n",
    "arbitrary(2,3,4,5,6,a=3,b=33,d=\"dd\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a function, that creates the fibonacci sequence up to the n'th term."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):\n",
    "    fibo=[1,1]\n",
    "    if n==1:\n",
    "        return fibo[0]\n",
    "    elif n==2:\n",
    "        return fibo\n",
    "    else:\n",
    "        for run in range(n-2):\n",
    "            fibo.append(fibo[-1]+fibo[-2])\n",
    "        return fibo\n",
    "\n",
    "fib(16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make python list of all files in the current directory and sort them by their file type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# list all contest\n",
    "foldercontent=os.listdir()\n",
    "\n",
    "#filter files\n",
    "files=[file for file in foldercontent if os.path.isfile(file)]\n",
    "\n",
    "#sort key makes sure i sort by the file ending\n",
    "def sortkey(x):\n",
    "    return x.split(\".\")[1]\n",
    "# sorting\n",
    "files.sort(key=sortkey)\n",
    "\n",
    "print(files)"
   ]
  }
 ],
 "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}