Facebook
From Selenay, 9 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 364
  1. import pandas as pd
  2. import numpy as np
  3. import seaborn as sns
  4. import matplotlib.pyplot as plt
  5. from sklearn.metrics import mean_squared_error
  6. from sklearn.model_selection import train_test_split
  7. from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler, OrdinalEncoder
  8. from sklearn.model_selection import GridSearchCV
  9. from sklearn.ensemble import RandomForestRegressor
  10.  
  11. data = pd.read_csv('bitcoinweek.csv')
  12. data = data[::-1]
  13. data.head(7)
  14.  
  15. corr = data.corr(numeric_only=True)
  16. corr.round(2)
  17.  
  18. plt.figure(figsize=(10,8))
  19. _ = sns.heatmap(corr.round(2), cmap="Greens",  cbar=True, annot=True, square=True)
  20.  
  21. data['Volume'] = data['Volume'].str.replace(',', '').astype(float)
  22. data['Market Cap'] = data['Market Cap'].str.replace(',', '').astype(float)
  23.  
  24. data['Date'] = data['Date'].astype('datetime64[ns]')
  25. data['Date'] = data['Date'].apply(lambda x: x.timestamp())
  26.  
  27. # Açılış değeri sütununu ekliyoruz
  28. data['Open'] = data['Open'].str.replace(',', '').astype(float)
  29.  
  30. X = data.drop(['Market Cap', 'Opening Price'], axis=1)
  31. y = data['Market Cap']
  32.  
  33. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  34.  
  35. numerical_cols = list(data.select_dtypes(include=['int64', 'float64']).columns)
  36. numerical_cols.remove('Market Cap')
  37.  
  38. data["Market Cap"].dtype
  39.  
  40. scaler = MinMaxScaler()
  41. X_train[numerical_cols] = scaler.fit_transform(X_train[numerical_cols])
  42. X_test[numerical_cols] = scaler.transform(X_test[numerical_cols])
  43.  
  44. param_grid = {
  45.     'n_estimators': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50, 100, 200, 300]
  46. }
  47.  
  48. model = RandomForestRegressor(random_state=42)
  49.  
  50. grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, scoring='neg_mean_squared_error', n_jobs=-1)
  51.  
  52. grid_search.fit(X, y)
  53.  
  54. best_n_estimators = grid_search.best_params_['n_estimators']
  55. print(f'En iyi n_estimators değeri: {best_n_estimators}')
  56.  
  57. model = RandomForestRegressor(n_estimators=5, random_state=42)
  58.  
  59. model.fit(X_train, y_train)
  60.  
  61. y_pred = model.predict(X_test)
  62.  
  63. mse = mean_squared_error(y_test, y_pred)
  64. print(f'Mean Squared Error: {mse}')
  65.