SHARE
TWEET

Python - ML temperature

furas Jun 10th, 2018 (edited) 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. text = '''
  2. 2018-05-27T00:12:43Z 20 21
  3. 2018-05-27T00:17:27Z 20 22
  4. 2018-05-27T00:17:59Z 20 24
  5. 2018-05-27T00:20:01Z 20 21
  6. 2018-05-27T00:23:14Z 20 24
  7. 2018-05-28T09:39:07Z 20 22
  8. 2018-05-28T10:40:17Z 20 23
  9. 2018-05-28T20:12:47Z 20 25
  10. 2018-05-28T20:14:16Z 23 25
  11. 2018-05-30T20:29:30Z 18 24
  12. '''
  13.  
  14. import pandas as pd
  15. import numpy as np
  16.  
  17. data = []
  18. for line in text.split('\n'):
  19.     line = line.strip()
  20.     if line:
  21.         parts = line.split(' ')
  22.         data.append(parts)
  23.  
  24. df = pd.DataFrame(data, columns=['datetime', 'env', 'user'])
  25.  
  26. df['datetime'] = pd.to_datetime(df['datetime'])
  27. df['minutes'] = df['datetime'].dt.time.apply(lambda x:x.hour*60 + x.minute)
  28. df['env']  = df['env'].astype('int')
  29. df['user'] = df['user'].astype('int')
  30.  
  31. print(df.dtypes)
  32. print(df)
  33.  
  34. import sklearn.linear_model
  35. import sklearn.model_selection
  36. import sklearn.tree
  37.  
  38. X = df[['minutes', 'env']]
  39. y = df['user']
  40.  
  41. X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.2, random_state=12)
  42.  
  43. print("--- LinearRegression ---")
  44.  
  45. model = sklearn.linear_model.LinearRegression()
  46.  
  47. model.fit(X_train, y_train)
  48.  
  49. predict = model.predict(X_test)
  50.  
  51. print('diff:\n', predict - y_test)
  52.  
  53. print('MSE:', sklearn.metrics.mean_squared_error(y_test, predict))
  54.  
  55. print("--- DecisionTreeRegressor ---")
  56.  
  57. model = sklearn.tree.DecisionTreeRegressor()
  58.  
  59. model.fit(X_train, y_train)
  60.  
  61. predict = model.predict(X_test)
  62.  
  63. print('diff:\n', predict - y_test)
  64.  
  65. print('MSE:', sklearn.metrics.mean_squared_error(y_test, predict))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top