21 KiB
21 KiB
In [124]:
import pandas as pd
df=pd.read_csv("diamonds.csv",index_col=0)
df.head()Out [124]:
| carat | cut | color | clarity | depth | table | price | x | y | z | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.23 | Ideal | E | SI2 | 61.5 | 55.0 | 326 | 3.95 | 3.98 | 2.43 |
| 2 | 0.21 | Premium | E | SI1 | 59.8 | 61.0 | 326 | 3.89 | 3.84 | 2.31 |
| 3 | 0.23 | Good | E | VS1 | 56.9 | 65.0 | 327 | 4.05 | 4.07 | 2.31 |
| 4 | 0.29 | Premium | I | VS2 | 62.4 | 58.0 | 334 | 4.20 | 4.23 | 2.63 |
| 5 | 0.31 | Good | J | SI2 | 63.3 | 58.0 | 335 | 4.34 | 4.35 | 2.75 |
In [125]:
cut_class_dict={"Fair":1,"Good":2,"Very Good":3,"Premium":4,"Ideal":5}
clarity_dict={"I3":1,"I2":2,"I1":3,"SI2":4,"SI1":5,"VS2":6,"VS1":7,"VVS2":8,"VVS1":9,"IF":10}
color_dict={"J":1,"I":2,"H":3,"G":4,"F":5,"E":6,"D":7}
df["cut"]=df["cut"].map(cut_class_dict)
df["clarity"]=df["clarity"].map(clarity_dict)
df["color"]=df["color"].map(color_dict)
df.head()Out [125]:
| carat | cut | color | clarity | depth | table | price | x | y | z | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.23 | 5 | 6 | 4 | 61.5 | 55.0 | 326 | 3.95 | 3.98 | 2.43 |
| 2 | 0.21 | 4 | 6 | 5 | 59.8 | 61.0 | 326 | 3.89 | 3.84 | 2.31 |
| 3 | 0.23 | 2 | 6 | 7 | 56.9 | 65.0 | 327 | 4.05 | 4.07 | 2.31 |
| 4 | 0.29 | 4 | 2 | 6 | 62.4 | 58.0 | 334 | 4.20 | 4.23 | 2.63 |
| 5 | 0.31 | 2 | 1 | 4 | 63.3 | 58.0 | 335 | 4.34 | 4.35 | 2.75 |
In [137]:
import sklearn
from sklearn import svm,preprocessing
df=sklearn.utils.shuffle(df)
X=df.drop("price",axis=1).values
X=preprocessing.scale(X)
y=df["price"].values
test_size=200
i=-test_size
X_train=X[:i]
y_train=y[:i]
X_test=X[-test_size:]
y_test=y[-test_size:]
clf=svm.SVR(kernel="linear")
#print(clf)
clf.fit(X_train,y_train)
clf.score(X_test,y_test)Out [137]:
0.8685874537876473
In [ ]:
In [136]:
for X,y in zip(X_test,y_test):
print("Prediction:",clf.predict([X])[0]," and Result:",y)
Prediction: 1661.2509411395731 and Result: 1433 Prediction: 4420.596725225344 and Result: 4735 Prediction: 4951.516677724213 and Result: 5871 Prediction: 2687.9167840874543 and Result: 2299 Prediction: -582.8273023277629 and Result: 523 Prediction: 5248.18589438113 and Result: 4283 Prediction: 604.9768486032858 and Result: 932 Prediction: 4587.023822593639 and Result: 3920 Prediction: 1706.2484721722958 and Result: 1781 Prediction: 234.69267564662505 and Result: 568 Prediction: 396.0334750606444 and Result: 470 Prediction: 3033.1312094537084 and Result: 2420 Prediction: 4868.5638439855775 and Result: 6271 Prediction: 1052.5875108235077 and Result: 1210 Prediction: 1928.3692625794476 and Result: 1744 Prediction: 7240.637411047793 and Result: 8767 Prediction: 1880.8096097532105 and Result: 1576 Prediction: 8728.400779006957 and Result: 8770 Prediction: 4361.70384911829 and Result: 3394 Prediction: 2083.194497647127 and Result: 2046 Prediction: 462.4683869047967 and Result: 625 Prediction: 360.9164384270957 and Result: 537 Prediction: 3504.4573507118944 and Result: 3465 Prediction: 457.7158069931352 and Result: 664 Prediction: 2943.609690737274 and Result: 2729 Prediction: 825.6789219470757 and Result: 1113 Prediction: -112.22525227185588 and Result: 675 Prediction: 1156.1937509220506 and Result: 1268 Prediction: 2852.4794705287404 and Result: 2057 Prediction: 10602.183597376777 and Result: 15025 Prediction: 8459.52868920096 and Result: 11508 Prediction: 12411.479568253973 and Result: 6357 Prediction: 2048.6593904333845 and Result: 2306 Prediction: 8405.101641557047 and Result: 6727 Prediction: 807.7081404765427 and Result: 816 Prediction: -480.63681024337575 and Result: 402 Prediction: 1305.1718713882037 and Result: 1049 Prediction: 941.6407941088269 and Result: 1080 Prediction: 1411.5037436434573 and Result: 1349 Prediction: 6407.067382554254 and Result: 5834 Prediction: 5901.649661036234 and Result: 5606 Prediction: 1129.275455006827 and Result: 1197 Prediction: 8253.625896704005 and Result: 8736 Prediction: 655.4686036207731 and Result: 756 Prediction: 4698.284413505142 and Result: 5126 Prediction: 5288.783646035807 and Result: 5330 Prediction: 1728.0086850408704 and Result: 1656 Prediction: 8691.516272956587 and Result: 11565 Prediction: 3576.5409551833377 and Result: 3239 Prediction: 5785.014799113572 and Result: 9140 Prediction: 8439.423061735353 and Result: 8518 Prediction: 1054.6393766757596 and Result: 840 Prediction: 10484.203841819319 and Result: 15847 Prediction: 6489.568318572174 and Result: 6577 Prediction: 779.1030423775119 and Result: 992 Prediction: 1847.8870259525563 and Result: 1304 Prediction: 12411.867245756715 and Result: 15647 Prediction: 849.3262917850134 and Result: 786 Prediction: 1955.6073535059497 and Result: 1721 Prediction: 1997.082517213365 and Result: 1196 Prediction: 6007.982123418467 and Result: 5292 Prediction: -118.16622228719416 and Result: 461 Prediction: 1933.905691425573 and Result: 1453 Prediction: 6849.44115994767 and Result: 6685 Prediction: 3701.765025541411 and Result: 3249 Prediction: 6959.359242703711 and Result: 9399 Prediction: 10054.28852454508 and Result: 13887 Prediction: 5249.652572322821 and Result: 6771 Prediction: 3798.1355652368193 and Result: 3828 Prediction: 1855.02147761309 and Result: 1585 Prediction: 2658.8120650710007 and Result: 2006 Prediction: 6133.555784900609 and Result: 6190 Prediction: 1323.6820280297852 and Result: 1129 Prediction: 481.28731132834446 and Result: 900 Prediction: 1990.4458935895477 and Result: 1582 Prediction: 1264.9267500893693 and Result: 1374 Prediction: 2126.155639490165 and Result: 2523 Prediction: 4111.741090991058 and Result: 3584 Prediction: 2447.673851800685 and Result: 2039 Prediction: 2966.643264754959 and Result: 2612 Prediction: 2315.01234047326 and Result: 2287 Prediction: 1981.3508856051944 and Result: 1838 Prediction: 5407.134261186012 and Result: 7652 Prediction: 1700.965771766409 and Result: 1408 Prediction: 5960.961959854444 and Result: 5871 Prediction: 6643.553497423281 and Result: 6661 Prediction: 2063.5560822216107 and Result: 1827 Prediction: 2309.248170136765 and Result: 2321 Prediction: 12841.469413234849 and Result: 15706 Prediction: 375.52740309000455 and Result: 616 Prediction: 1333.3655570065803 and Result: 1373 Prediction: 1877.7961376920434 and Result: 1378 Prediction: 4447.176955208855 and Result: 5907 Prediction: 638.5710924244081 and Result: 631 Prediction: 1741.1726614998017 and Result: 1600 Prediction: 1171.711234107629 and Result: 1057 Prediction: 810.0750244594401 and Result: 873 Prediction: 580.8991196326606 and Result: 789 Prediction: 1159.6190887320645 and Result: 1170 Prediction: 2907.1612803146236 and Result: 2815 Prediction: 4828.906592938275 and Result: 5292 Prediction: 11955.658435234669 and Result: 12615 Prediction: 4508.659209678087 and Result: 4861 Prediction: 1009.8322487301512 and Result: 790 Prediction: 9269.719757616243 and Result: 14361 Prediction: 832.5840298294183 and Result: 933 Prediction: 489.8986521681991 and Result: 844 Prediction: 2860.598190173582 and Result: 2723 Prediction: 3456.547685845321 and Result: 3042 Prediction: 467.87855789608375 and Result: 900 Prediction: 13325.51365825141 and Result: 18062 Prediction: 5293.6113390393775 and Result: 6577 Prediction: 1698.2017169763228 and Result: 1752 Prediction: 812.9953315978696 and Result: 871 Prediction: -275.4396888801057 and Result: 702 Prediction: 1800.8686392452712 and Result: 1722 Prediction: 1215.2245774152652 and Result: 1277 Prediction: 1007.167620049082 and Result: 789 Prediction: 1018.1141123617026 and Result: 895 Prediction: 2477.8775632273564 and Result: 2074 Prediction: 327.26746326510374 and Result: 778 Prediction: 1285.6132113232115 and Result: 1415 Prediction: 4598.101786666508 and Result: 4588 Prediction: 585.3611007083764 and Result: 898 Prediction: 6616.032585556156 and Result: 6071 Prediction: 1922.6074968159303 and Result: 1922 Prediction: 4580.458945627719 and Result: 4374 Prediction: 2186.1501790259917 and Result: 1993 Prediction: 719.9255247087162 and Result: 772 Prediction: 1705.4194972595992 and Result: 1216 Prediction: 8003.976791659106 and Result: 7235 Prediction: 4737.526027067042 and Result: 4043 Prediction: 1862.3498147694922 and Result: 1431 Prediction: 5716.351749114134 and Result: 4919 Prediction: 5112.3750579719945 and Result: 6083 Prediction: 9379.673148486268 and Result: 15225 Prediction: 4500.3223839217935 and Result: 4490 Prediction: 8970.934568426206 and Result: 9332 Prediction: 612.2743954910566 and Result: 957 Prediction: 1257.6792359045144 and Result: 1014 Prediction: 9499.392200947746 and Result: 6857 Prediction: 799.7787903260073 and Result: 707 Prediction: 2930.099521472761 and Result: 2550 Prediction: 784.8841056627257 and Result: 816 Prediction: 8302.13851740177 and Result: 7319 Prediction: 1333.6013002430536 and Result: 1308 Prediction: 555.2396129282934 and Result: 738 Prediction: 5137.044308170544 and Result: 6283 Prediction: 5749.891028718341 and Result: 9043 Prediction: -89.11837891313462 and Result: 431 Prediction: 2784.592951303089 and Result: 2202 Prediction: 195.97080780439182 and Result: 743 Prediction: 268.32903844219254 and Result: 571 Prediction: 6089.804458187223 and Result: 6062 Prediction: 6397.515961428117 and Result: 6793 Prediction: 6168.366588613534 and Result: 5502 Prediction: 4729.060766484016 and Result: 4390 Prediction: 3245.6985136998487 and Result: 3103 Prediction: 141.01072882354083 and Result: 802 Prediction: 139.02879896539525 and Result: 693 Prediction: 285.12900444067236 and Result: 492 Prediction: 1943.6917821259044 and Result: 1749 Prediction: 14057.137963627789 and Result: 18026 Prediction: 4108.826708129414 and Result: 3750 Prediction: 8814.63355701247 and Result: 14294 Prediction: 422.04334145672374 and Result: 798 Prediction: 10008.204712876208 and Result: 11576 Prediction: 3300.444700649907 and Result: 3399 Prediction: 1131.7661452398506 and Result: 1406 Prediction: 1376.6708649755305 and Result: 1330 Prediction: 5118.806626364326 and Result: 4764 Prediction: 3411.7786101309025 and Result: 3475 Prediction: 1932.4274418473906 and Result: 1289 Prediction: 970.8452710597908 and Result: 760 Prediction: 1448.7041676897338 and Result: 1337 Prediction: 332.5427170007265 and Result: 614 Prediction: 3232.990792527339 and Result: 2827 Prediction: 4691.68121997912 and Result: 4687 Prediction: 3645.5805584025093 and Result: 3214 Prediction: 1025.828255714197 and Result: 924 Prediction: 3508.3545255756435 and Result: 2199 Prediction: 4823.637840333522 and Result: 3604 Prediction: 5175.669111504052 and Result: 4678 Prediction: 10816.521917194204 and Result: 10885 Prediction: 11950.59451865071 and Result: 13212 Prediction: 3283.222371789808 and Result: 2513 Prediction: 1662.5520620210877 and Result: 1402 Prediction: 2500.388919722649 and Result: 2399 Prediction: 715.9662325384902 and Result: 807 Prediction: 5087.389420650879 and Result: 4560 Prediction: 326.4187237859028 and Result: 596 Prediction: 1420.7393680598516 and Result: 1145 Prediction: 4348.226666530411 and Result: 4381 Prediction: 1904.1574221899157 and Result: 1712 Prediction: 3539.4796229537915 and Result: 3040 Prediction: 2928.93541993041 and Result: 2700 Prediction: 2375.9351434559253 and Result: 1821 Prediction: 12699.394167501321 and Result: 16036 Prediction: 5033.3275396781055 and Result: 6612 Prediction: 4256.575280521045 and Result: 4032
In [ ]: