Weitere ähnliche Inhalte
Ähnlich wie Machine Learning final project (Bitcoin Price Prediction).pdf (20)
Kürzlich hochgeladen (20)
Machine Learning final project (Bitcoin Price Prediction).pdf
- 1. 6/6/23, 8:40 AM btc price predict
localhost:8889/nbconvert/html/Bitcoin price prediction/btc price predict.ipynb?download=false 1/5
Date Open High Low Close Adj Close Volume
0
2018-
01-06
17462.099609 17712.400391 16764.599609 17527.000000 17527.000000 18314600448
1
2018-
01-07
17527.300781 17579.599609 16087.700195 16477.599609 16477.599609 15866000384
2
2018-
01-08
16476.199219 16537.900391 14208.200195 15170.099609 15170.099609 18413899776
3
2018-
01-09
15123.700195 15497.500000 14424.000000 14595.400391 14595.400391 16659999744
4
2018-
01-10
14588.500000 14973.299805 13691.200195 14973.299805 14973.299805 18500800512
5
2018-
01-11
14968.200195 15018.799805 13105.900391 13405.799805 13405.799805 16534099968
6
2018-
01-12
13453.900391 14229.900391 13158.099609 13980.599609 13980.599609 12065699840
7
2018-
01-13
13952.400391 14659.500000 13952.400391 14360.200195 14360.200195 12763599872
8
2018-
01-14
14370.799805 14511.799805 13268.000000 13772.000000 13772.000000 11084099584
9
2018-
01-15
13767.299805 14445.500000 13641.700195 13819.799805 13819.799805 12750799872
Date
2018-01-06 17462.099609
2018-01-07 17527.300781
2018-01-08 16476.199219
2018-01-09 15123.700195
2018-01-10 14588.500000
Name: Open, dtype: float64
In [81]: import pandas as pd
import numpy as np
import datetime
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
import matplotlib.pylab as plt
%matplotlib inline
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 15, 6
In [82]: df = pd.read_csv(r'C:UsersSunny MobilesDesktopBTC-USD.csv')
df.isnull().values.any()
df.head(10)
Out[82]:
In [83]: df['Date'] = pd.to_datetime(df['Date']).dt.date
group = df.groupby('Date')
Daily_Price = group['Open'].mean()
Daily_Price.head()
Out[83]:
In [84]: new_df= pd.DataFrame(list(Daily_Price), index = Daily_Price.index, columns = ["Clos
In [85]: new_df.head()
- 2. 6/6/23, 8:40 AM btc price predict
localhost:8889/nbconvert/html/Bitcoin price prediction/btc price predict.ipynb?download=false 2/5
Close
Date
2018-01-06 17462.099609
2018-01-07 17527.300781
2018-01-08 16476.199219
2018-01-09 15123.700195
2018-01-10 14588.500000
Text(0, 0.5, '$(Dollar)')
Out[85]:
In [86]: plt.plot(new_df,color='green', lw=2)
plt.title("BitCoin Data",fontsize=24)
plt.xlabel('time in days',fontsize=24)
plt.ylabel('$(Dollar)',fontsize=24)
Out[86]:
In [87]: # df = df[['Close']]
df=new_df
forecast_out = int(20)
df['Prediction'] = df[['Close']].shift(-forecast_out)
X = np.array(df.drop(['Prediction'],1))
X = preprocessing.scale(X)
X_forecast = X[-forecast_out:]
X = X[:-forecast_out]
y = np.array(df['Prediction'])
y = y[:-forecast_out]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,random_st
clf = LinearRegression()
clf.fit(X_train,y_train)
cross_validation = clf.predict(X_test)
confidence = clf.score(X_test, y_test)
print("confidence: ", confidence)
- 3. 6/6/23, 8:40 AM btc price predict
localhost:8889/nbconvert/html/Bitcoin price prediction/btc price predict.ipynb?download=false 3/5
confidence: 0.91774107868374
[27372.83496361 26816.30660188 26876.41076526 27098.65757826
26741.90101745 26844.58046004 27201.44479188 26334.90230041
26474.99913149 26713.13935422 26859.29216765 28025.24373001
27705.33356037 27662.16459469 27195.45120228 26814.17954019
27228.27980971 27056.74027082 27103.19342056 25761.14757002]
C:UsersSunny MobilesAppDataLocalTempipykernel_118364198257280.py:7: FutureW
arning: In a future version of pandas all arguments of DataFrame.drop except for t
he argument 'labels' will be keyword-only.
X = np.array(df.drop(['Prediction'],1))
4542.036550091358
2023-06-06
forecast_prediction = clf.predict(X_forecast)
print(forecast_prediction)
In [88]: import matplotlib.pyplot as plt
# print(X_test)
# print(y_test)
# plt.plot(X_test,forecast_set,color='blue', linewidth=1)
plt.scatter(X_test, y_test, color='darkorange', label='test_data')
plt.plot(X_test, cross_validation, color='green', linewidth=2,label='predicted data
# plt.plot(new_df,color='green', lw=2)
plt.title("Crossvalidation (Linear Regression)",fontsize=24)
plt.xlabel('X_test',fontsize=24)
plt.ylabel('y',fontsize=24)
# plt.plot(X_test, forecast_prediction, color='blue', linew/idth=2)
plt.show()
In [89]: from sklearn.metrics import mean_squared_error
from math import sqrt
rms = sqrt(mean_squared_error(y_test, cross_validation))
print(rms)
In [90]: last_date = new_df.iloc[-1].name
print(last_date)
In [91]: import pandas as pd
import numpy as np
from datetime import datetime, timedelta
date_today = last_date
days = pd.date_range(date_today, date_today + timedelta(19), freq='D')
print(len(days),len(forecast_prediction))
df1 = pd.DataFrame({'Date': days, 'Close': forecast_prediction})
df1 =df1.set_index('Date')
- 4. 6/6/23, 8:40 AM btc price predict
localhost:8889/nbconvert/html/Bitcoin price prediction/btc price predict.ipynb?download=false 4/5
20 20
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 20 entries, 2023-06-06 to 2023-06-25
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Close 20 non-null float64
dtypes: float64(1)
memory usage: 320.0 bytes
Close Prediction
Date
2018-01-06 17462.099609 11256.000000
2018-01-07 17527.300781 11174.900391
2018-01-08 16476.199219 11475.299805
2018-01-09 15123.700195 11755.500000
2018-01-10 14588.500000 11306.799805
Text(0, 0.5, '$(Dollar)')
df1.info()
print(df.head())
In [92]: # # df['Close'].plot()
# df1['Close'].plot()
# plt.legend(loc=4)
# plt.xlabel('Date')
# plt.ylabel('Price')
# plt.show()
plt.plot(df1,color='green', lw=2)
plt.title("Predicted Next 30 Days",fontsize=24)
plt.xlabel('time in days',fontsize=24)
plt.ylabel('$(Dollar)',fontsize=24)
Out[92]:
In [93]: # plt.figure(figsize=(12,8))
# plt.plot(df['Close'], label='Train')
# plt.plot(df1['Close'], label='Predicted')
# # plt.plot(X_test['Close'], label='Test')
# # plt.plot(y_hat_avg['avg_forecast'], label='Average Forecast')
# plt.legend(loc='best')
# plt.show()
plt.plot(df1,)
plt.plot(df['Close'],color='green', lw=2, label='Bitcoin Data')
plt.plot(df1['Close'],color='red', lw=2, label='Predicted')
plt.title("Predicted (Linear Regression 30 Days)",fontsize=24)
- 5. 6/6/23, 8:40 AM btc price predict
localhost:8889/nbconvert/html/Bitcoin price prediction/btc price predict.ipynb?download=false 5/5
Text(0, 0.5, '$(Dollar)')
plt.xlabel('time in days',fontsize=24)
plt.ylabel('$(Dollar)',fontsize=24)
Out[93]: