Aaaaa988

lab3

Nov 27th, 2020
779
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2. import pydotplus
  3. from sklearn.linear_model import LinearRegression
  4. from sklearn.linear_model import ElasticNet
  5. from sklearn.model_selection import train_test_split
  6.  
  7. all = pd.read_csv('winequalityN.csv', header=0)
  8. all.isnull().any()
  9. all = all.fillna(method='ffill')
  10.  
  11. print(all.shape[0])
  12.  
  13. mask = all['type'] == "white"
  14. white, red = all[mask], all[~mask]
  15. print(all)
  16.  
  17. df = all
  18. x = df.values[:, 1:12]
  19. y = df.values[:, 12:13]
  20.  
  21. x_norm = (x - x.min()) / (x.max() - x.min())
  22. x_norm = x_norm.astype('float')
  23. print(x_norm.round(3))
  24. y = y.astype('float')
  25. print("Все вина = ", y.shape[0])
  26.  
  27. for k in range(10):
  28.  
  29.     X_train, X_test, y_train, y_test = train_test_split(x_norm, y, test_size=0.3, random_state=0)
  30.     print("y_test = ", y_test.shape[0])
  31.     regressor = LinearRegression()
  32.     regressor.fit(X_train, y_train)
  33.  
  34.     y_pred = regressor.predict(X_test)
  35.     summ = 0
  36.     for i in range(y_pred.shape[0]):
  37.         summ = summ + abs(y_test[i] - y_pred[i])
  38.     mistake = summ / y_pred.shape[0]
  39.     good = 1 - mistake
  40.     print(good)
  41.  
  42.  
RAW Paste Data