Advertisement
lancernik

ASID5

May 5th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Apr 22 12:12:18 2019
  4.  
  5. @author: lancernik
  6. """
  7.  
  8. # Zadanie 1
  9.  
  10. #import numpy as np
  11. #from sklearn.linear_model import LinearRegression
  12. #from sklearn.model_selection import train_test_split
  13. #import matplotlib.pyplot as plt
  14. #
  15. #a = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90]).reshape(-1,1)
  16. #b = np.array([300,350,500,700,800,850,900,1000,1200]).reshape(-1,1)
  17. #
  18. #def regress(x,y):
  19. # model = LinearRegression()
  20. # model.fit(x,y)
  21. # model.predict([[100]])
  22. #
  23. # x_test = np.linspace(x[0],x[-1])
  24. # y_pred = model.predict(x_test[:,None])
  25. #
  26. # plt.scatter(x,y)
  27. # plt.plot(x_test,y_pred,'r')
  28. # plt.legend(['Regresja', 'Kropeczki'])
  29. # plt.show()
  30. #
  31. #regress(a,b)
  32. #
  33.  
  34.  
  35.  
  36. #Zadanie 2
  37.  
  38. import csv
  39. import os
  40. from sklearn.linear_model import LinearRegression
  41. import numpy as np
  42. import pandas as pd
  43. import matplotlib.pyplot as plt
  44.  
  45.  
  46.  
  47. def Pearson(dataset_out):
  48. c = dataset_out.corr("pearson")
  49. c = c.where(
  50. np.triu(
  51. np.ones(c.shape,dtype=np.bool),k=1)
  52. ).stack().sort_values()
  53. print(c[abs(c)>0.1])
  54.  
  55. def regress(x,y,x_name="Missing data",y_name="Missing data"):
  56. model = LinearRegression()
  57. model.fit(x,y)
  58. model.predict([[1]])
  59.  
  60. x_test = np.linspace(min(x),max(x))
  61. y_pred = model.predict(x_test[:,None])
  62.  
  63. plt.xlabel('{}'.format(x_name))
  64. plt.ylabel('{}'.format(y_name))
  65.  
  66. plt.scatter(x,y, marker='*', s=10)
  67. plt.plot(x_test,y_pred,'r')
  68. plt.legend(['Regresja', 'Kropeczki'])
  69. plt.show()
  70. print("y={}x+{}".format(round(model.coef_[0,0],3),round(model.intercept_[0],3)))
  71. print("Dopasowanie regresji: {}".format(round(model.score(x,y),5)))
  72.  
  73.  
  74.  
  75.  
  76. current_dir = os.path.abspath(os.path.dirname(__file__))
  77. data_path = os.path.join(current_dir, "Data")
  78. csv_path = os.path.join(data_path, "Advertising.csv")
  79.  
  80.  
  81.  
  82.  
  83. with open(csv_path) as csv_file:
  84. output_dict = dict()
  85. csv_reader = csv.reader(csv_file)
  86. first_row = next(csv_reader)
  87. for item in first_row:
  88. output_dict[item] = []
  89. for item in csv_reader:
  90. for i in range(len(item)):
  91. try:
  92. output_dict[first_row[i]].append(float(item[i]))
  93. except:
  94. output_dict[first_row[i]].append(item[i])
  95.  
  96. for key in output_dict.keys():
  97. try:
  98. output_dict[key] = np.array(output_dict[key], dtype=np.float)
  99. dataset = pd.DataFrame.from_dict(output_dict)
  100. except:
  101. pass
  102.  
  103.  
  104.  
  105. dataset = dataset[['TV','radio','newspaper','sales']]
  106. Pearson(dataset)
  107.  
  108.  
  109.  
  110. tv = output_dict['TV'].reshape(-1,1)
  111. sales = output_dict['sales'].reshape(-1,1)
  112. radio = output_dict['radio'].reshape(-1,1)
  113. newspaper = output_dict['newspaper'].reshape(-1,1)
  114.  
  115.  
  116.  
  117. regress(tv,sales,"Tv","Sales")
  118. regress(radio,sales)
  119. regress(newspaper, sales)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement