SHARE
TWEET

Untitled

a guest Jan 23rd, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn import neighbors
  4. import pandas as pd
  5. import sys
  6. import os
  7.  
  8. data = pd.read_csv(
  9.     "C:/Users/Piotr/Desktop/dane.csv",
  10.     sep=';',
  11.     usecols=['X', 'Y'],
  12.     dtype={"X": np.float64, "Y": np.float64}
  13. )
  14.  
  15. X = data["X"].tolist()
  16. Y = data["Y"].tolist()
  17.  
  18. test=[]
  19. test2=[]
  20.  
  21. for elem in X:
  22.     tmp=[]
  23.     tmp.append(elem)
  24.     test.append(tmp)
  25.  
  26. for elem in Y:
  27.     tmp=[]
  28.     tmp.append(elem)
  29.     test2.append(tmp)
  30.  
  31. X = test
  32. y = test2
  33.  
  34. T = np.linspace(1900, 2020, 1000)[:, np.newaxis]
  35.  
  36. n_neighbors = 5
  37.  
  38. plt.figure(figsize=(20,20))
  39.  
  40. knn = neighbors.KNeighborsRegressor(n_neighbors, weights='uniform')
  41. y_ = knn.fit(X, y).predict(T)
  42. plt.subplot(4, 1, 1)
  43. plt.scatter(X, y, color='darkorange', label='data')
  44. plt.plot(T, y_, color='navy', label='prediction')
  45. plt.axis([1899, 2018, -0.7, 1.4])
  46. plt.legend(prop={'size': 30})
  47. plt.title("KNeighborsRegressor (k = %i, weights = '%s')" % (n_neighbors, weights), fontsize=30)
  48.  
  49. knn = neighbors.KNeighborsRegressor(n_neighbors, weights='distance')
  50. y_ = knn.fit(X, y).predict(T)
  51. plt.subplot(4, 1, 2)
  52. plt.scatter(X, y, color='darkorange', label='data')
  53. plt.plot(T, y_, color='navy', label='prediction')
  54. plt.axis([1899, 2018, -0.7, 1.4])
  55. plt.legend(prop={'size': 30})
  56. plt.title("KNeighborsRegressor (k = %i, weights = '%s')" % (n_neighbors, weights), fontsize=30)
  57.  
  58. T = np.linspace(2010, 2016, 1000)[:, np.newaxis]
  59. knn = neighbors.KNeighborsRegressor(n_neighbors, weights='uniform')
  60. y_ = knn.fit(X, y).predict(T)
  61. plt.subplot(4, 1, 3)
  62. plt.scatter(X, y, color='darkorange', label='data')
  63. plt.plot(T, y_, color='navy', label='prediction')
  64. plt.axis([2010, 2016, -0.7, 1.4])
  65. plt.legend(prop={'size': 30})
  66. plt.title("KNeighborsRegressor (k = %i, weights = '%s')" % (n_neighbors, weights), fontsize=30)
  67.  
  68. T = np.linspace(2010, 2016, 1000)[:, np.newaxis]
  69. knn = neighbors.KNeighborsRegressor(n_neighbors, weights='distance')
  70. y_ = knn.fit(X, y).predict(T)
  71. plt.subplot(4, 1, 4)
  72. plt.scatter(X, y, color='darkorange', label='data')
  73. plt.plot(T, y_, color='navy', label='prediction')
  74. plt.axis([2010, 2016, -0.7, 1.4])
  75. plt.legend(prop={'size': 30})
  76. plt.title("KNeighborsRegressor (k = %i, weights = '%s')" % (n_neighbors, weights), fontsize=30)
  77.  
  78. plt.tight_layout()
  79. plt.show()
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
Top