Pythoni andmekatsetused

Alustuseks lihtsamad tehted Üldotstarbeliste arendusplatvormide kursuse juures

  • Kood
  • Seletused
  • Joonised

Pythoni logo

print("tere")
 print("tere")
Juku Kati Mati
169 173 165

algne kiri

Juku vastus ja vastuse jätk

Eraldi real vastus

In [2]:
3+2
Out[2]:
5
In [9]:
import pandas as pd
#lapsed=pd.read_csv("http://www.tlu.ee/~jaagup/andmed/muu/5klass.txt")
m={'eesnimi':["Juku", "Kati", "Mati"], 'pikkus':[165, 172, 171], 
    'mass':[58, 49, 56], 'sugu':["m","n", "m"]}
lapsed=pd.DataFrame(data=m)
lapsed.head()
lapsed[lapsed.sugu=="m"]
Out[9]:
eesnimi mass pikkus sugu
0 Juku 58 165 m
2 Mati 56 171 m
In [10]:
lapsed[lapsed.pikkus>170]
Out[10]:
eesnimi mass pikkus sugu
1 Kati 49 172 n
2 Mati 56 171 m
In [12]:
lapsed.eesnimi.count()
Out[12]:
3
In [14]:
lapsed.groupby("sugu").eesnimi.count()
Out[14]:
sugu
m    2
n    1
Name: eesnimi, dtype: int64
In [15]:
lapsed["pikkusm"]=lapsed.pikkus/100
lapsed
Out[15]:
eesnimi mass pikkus sugu pikkusm
0 Juku 58 165 m 1.65
1 Kati 49 172 n 1.72
2 Mati 56 171 m 1.71
In [19]:
del lapsed["pikkusm"]
lapsed
Out[19]:
eesnimi mass pikkus sugu
0 Juku 58 165 m
1 Kati 49 172 n
2 Mati 56 171 m
In [21]:
eesnimed=lapsed.eesnimi.values.tolist()
eesnimed
Out[21]:
['Juku', 'Kati', 'Mati']
In [22]:
eesnimed[0]
Out[22]:
'Juku'
In [24]:
print(lapsed.mass.min())
print(lapsed.pikkus.mean())
lapsed.pikkus.median()
49
169.33333333333334
Out[24]:
171.0
In [25]:
lapsed.mass.cumsum()
Out[25]:
0     58
1    107
2    163
Name: mass, dtype: int64
In [47]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot([160, 180, 170])
lapsed.plot("pikkus", "mass")
lapsed.plot(x="pikkus", kind="bar")
lapsed.plot.pie(y="mass")
Out[47]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f6b6ec79b70>
In [58]:
plt.figure()
plt.xlim([155, 185])
plt.ylim([30, 50])
plt.title("Laste andmed")
plt.xlabel("pikkused")
plt.ylabel("massid")
plt.plot([160, 175, 170], [38, 35, 39], "ro")
plt.plot([166, 175, 177], [41, 38, 45], "bo")
Out[58]:
[<matplotlib.lines.Line2D at 0x7f6b6e8bb668>]
In [60]:
from azureml import Workspace
ws = Workspace()
experiment = ws.experiments['66e373b2084d4ffa9395c0e34ce9ccaa.f-id.c9bd98833fb248499b4c909fbeedfceb']
ds = experiment.get_intermediate_dataset(
    node_id='39083f31-4d5a-42c9-8ea2-2ca379c33f7a-1107',
    port_name='Results dataset',
    data_type_id='GenericCSV'
)
frame = ds.to_dataframe()
frame.head()
Out[60]:
august september
0 60 79
1 40 57
2 52 66
3 41 54
4 57 79
In [79]:
#Kuvage xY-joonis, kus on näha õunte suurused augustis ja septembris
#Lisage telgede nimetused ja pealkiri
#frame.august.values.tolist()
plt.figure()
plt.title("Õunte suurused")
plt.xlabel("august")
plt.ylabel("september")
plt.xlim([0, 100])
plt.ylim([0, 100])
plt.scatter(frame.august, frame.september, color="green")
augustkesk=frame.august.mean()
septemberkesk=frame.september.mean()
plt.plot([augustkesk, augustkesk], [0, 100], linestyle="dotted", linewidth=0.3)
plt.plot([0, 100],[septemberkesk, septemberkesk], 
          linestyle="dotted", linewidth=0.3)
#Tõmmake keskmise joon ka septembri kohta
#Muutke joon õrnemaks
import numpy as np
tous, vabaliige=np.polyfit(frame.august, frame.september, 1)
x0=frame.august.min()
y0=tous*x0+vabaliige
x1=frame.august.max()
y1=tous*x1+vabaliige
plt.plot([x0, x1], [y0, y1])
Out[79]:
[<matplotlib.lines.Line2D at 0x7f6b6d25a160>]
In [90]:
import urllib.request
import json

data = {
        "Inputs": {
                "sisend":
                [
                    {'august': "1",},
                    {'august': "50",},
                    {'august': "60",}
                ],
        },
    "GlobalParameters":  {
    }
}

body = str.encode(json.dumps(data))

url = 'https://ussouthcentral.services.azureml.net/workspaces/66e373b2084d4ffa9395c0e34ce9ccaa/services/eb1244be63a54bb9b0863642e0d30f20/execute?api-version=2.0&format=swagger'
api_key = 'H6jP3QxxDAR7QgnCVu0h0l82CZa075/6ZHDF4rELwP4hizvteG9J1UVMF52HCf7GESIPPjVcUdidVIAZhsnx9Q==' # Replace this with the API key for the web service
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

req = urllib.request.Request(url, body, headers)

try:
    response = urllib.request.urlopen(req)
    result = response.read()
    obj=json.loads(result.decode("utf-8"))
    print(obj["Results"]["ennustus"])
    m=[float(vastus["Scored Labels"]) for vastus in obj["Results"]["ennustus"]]
    print(m)
except urllib.error.HTTPError as error:
    print("The request failed with status code: " + str(error.code))

    # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
    print(error.info())
    print(json.loads(error.read().decode("utf8", 'ignore')))
    
    
[{'Scored Labels': '19.502363699487', 'august': '1'}, {'Scored Labels': '69.0209832261491', 'august': '50'}, {'Scored Labels': '79.1268239458761', 'august': '60'}]
[19.502363699487, 69.0209832261491, 79.1268239458761]